-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
325 lines (272 loc) · 10.7 KB
/
parser.py
File metadata and controls
325 lines (272 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from lark.lark import Lark
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import math
from functools import partial
from img_modifier import img_helper
from img_modifier import color_filter
from PIL import ImageQt
from yolov3 import Yolo
from agender import Agender
from face_classification.src.emotion import Emotion
from food_detection.food_detection import Food
from PSPNET.inference import MyPSPNET
ipl_grammar = """
start: statement+
statement: NUMBER -> number
| TUNEOP + "the" + ATTRIBUTE + "by" + NUMBER -> tuning
| "apply a filter" + FILTER -> set_filter
| "flip the image" + FLIPDIR -> flip
| "detect this image" -> detect
| "how many" + OBJECTS + "in this image" -> how_many
| "tag the" + OBJECTS + "in this image" -> tag_objects
| "what are in this image?" -> show_statistics
| "detect food in this image" -> detect_food
| "semantic segmentation" -> image_segmentation
MATHOP: "+"|"-"|"*"|"/"
IMAGE: (LETTER|NUMBER|"_")+"."LETTER+
IDENTIFIER: LETTER(LETTER|NUMBER)*
TUNEOP: "increase" | "decrease"
ATTRIBUTE: "brightness" | "contrast" | "sharpness"
FILTER: "sepia" | "negative" | "black_white"
FLIPDIR: "horizontally" | "vertically"
OBJECTS: LETTER+
%import common.LETTER
%import common.INT -> NUMBER
%import common.WS
%ignore WS
"""
class IPDSL():
def __init__(self):
self.parser = Lark(ipl_grammar)
self.yolo = None
self.agender = None
self.emotion = None
self.food = None
self.pspnet = None
self.original_img = None
self.img_path = ''
self.msg = ''
self.persons = []
self.objects = []
pass
def interpreter(self, s, img, img_path = ''):
if s.data == 'tuning':
tmp = 1 if s.children[0] == 'increase' else -1
attr = s.children[1]
val = int(s.children[2])
print(tmp, attr, val)
elif s.data == 'set_filter':
filter_name = s.children[0]
return img_helper.color_filter(img, filter_name)
elif s.data == 'flip':
flip_dir = s.children[0]
if flip_dir == 'vertically':
return img_helper.flip_top(img)
elif flip_dir == 'horizontally':
return img_helper.flip_left(img)
elif s.data == 'detect':
print(img_path)
if self.yolo is None:
self.yolo = Yolo()
self.agender = Agender()
self.emotion = Emotion()
if img_path != self.img_path:
self.img_path = img_path
self.persons = []
self.objects = []
objects = self.yolo.detect(img_path)
faces = self.agender.detect(img_path)
emotions = self.emotion.detect(img_path)
font_path = "/Users/woffee/www/language_design/prototype/font_consolas/CONSOLA.TTF"
font = ImageFont.truetype(font_path, 20)
fw = 11
fh = 16
msg = []
draw = ImageDraw.Draw(img)
for i, box in enumerate(objects):
l = box['left']
t = box['top']
b = box['bottom']
r = box['right']
label = box['class']
self.objects.append({
'left': l,
'top': t,
'bottom': b,
'right': r,
'class': label
})
if label == 'person':
self.persons.append({
'left': l,
'top': t,
'bottom': b,
'right': r,
'gender': '',
'age':0,
'emotion':''
})
else:
draw.rectangle(((l, t), (r, b)), outline='blue')
txt_width = fw * len(label)
draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="blue")
draw.text((l, t), label, font=font)
for i, box in enumerate(faces):
l = box['left']
t = box['top']
b = box['bottom']
r = box['right']
gender = 'male' if box['gender'] <0.5 else 'female'
age = box['age']
label = gender + ", %.2f" % age
print(" * Agender: " + label)
# msg.append(" * Agender: " + label)
score = 0
for i, p in enumerate(self.persons):
area = self.computeArea(l, t, r, b, p['left'], p['top'], p['right'], p['bottom'])
s = area / ( (r-l) * (b-t) )
if s > 0.5:
self.persons[i]['age'] = age
self.persons[i]['gender'] = gender
for i, box in enumerate(emotions):
l = box['left']
t = box['top']
b = box['bottom']
r = box['right']
emo = box['emotion']
print(" * Emotion: " + emo)
# msg.append(" * Emotion: " + emo)
for i, p in enumerate(self.persons):
area = self.computeArea(l, t, r, b, p['left'], p['top'], p['right'], p['bottom'])
if (r-l) * (b-t) > 0:
s = area / ( (r-l) * (b-t) )
if s > 0.5:
self.persons[i]['emotion'] = emo
# draw.rectangle(((l, t), (r, b)), outline='yellow')
# txt_width = fw * len(emo)
# draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="black")
# draw.text((l, t), emo, font=font)
for i, box in enumerate(self.persons):
l = box['left']
t = box['top']
b = box['bottom']
r = box['right']
draw.rectangle(((l, t), (r, b)), outline='blue')
gender_age = box['gender']
if box['age'] > 0:
gender_age = gender_age + ", age %.2f" % box['age']
emotion = box['emotion']
txt_width = fw * len(gender_age)
draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="blue")
draw.text((l, t), gender_age, font=font)
txt_width = fw * len(emotion)
draw.rectangle(((l, t + fh), (l + txt_width, t + fh * 2)), fill="blue")
draw.text((l, t + fh), emotion, font=font)
self.msg = " * done"
elif s.data == 'how_many':
str = s.children[0]
str = self.remove_s(str)
num = 0
for obj in self.objects:
if obj['class'] == str:
num += 1
self.msg = " * %d %s(s)" % (num, str)
elif s.data == 'tag_objects':
img = self.original_img.copy()
str = s.children[0]
str = self.remove_s(str)
msg = []
draw = ImageDraw.Draw(img)
font_path = "/Users/woffee/www/language_design/prototype/font_consolas/CONSOLA.TTF"
font = ImageFont.truetype(font_path, 20)
fw = 11
fh = 16
for i, box in enumerate(self.objects):
if str == box['class']:
l = box['left']
t = box['top']
b = box['bottom']
r = box['right']
label = box['class']
print(" * Yolo: " + label)
msg.append(" * Yolo: " + label)
draw.rectangle(((l, t), (r, b)), outline='blue')
txt_width = fw * len(label)
draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="blue")
draw.text((l, t), label, font=font)
self.msg = "\n".join(msg)
elif s.data == 'show_statistics':
statistics = {}
for o in self.objects:
if o['class'] not in statistics.keys():
statistics[o['class']] = 1
else:
statistics[o['class']] += 1
msg = []
for k in statistics.keys():
v = statistics[k]
msg.append( " * %d %s(s)" % (v, k) )
self.msg = "\n".join(msg)
elif s.data == 'detect_food':
self.detect_food(img_path)
elif s.data == 'image_segmentation':
img = self.image_segmentation(img_path)
return img
def process(self, img, cmd, img_path):
ast = self.parser.parse(cmd)
print(ast.pretty())
for s in ast.children:
img = self.interpreter(s, img, img_path)
return img
def computeArea(self, A, B, C, D, E, F, G, H):
width = min(C, G) - max(A, E)
height = min(D, H) - max(B, F)
# width = width if width > 0 else 0
# height = height if height > 0 else 0
# return (C-A)*(D-B) + (G-E)*(H-F) - width*height
return width * height
def remove_s(self, str):
dic = {
'persons': 'person',
'dogs': 'dog',
'cats': 'cat',
'bicycles': 'bicycle',
'birds': 'bird',
'boats': 'boat',
'cars': 'car',
'chairs': 'chair',
'cows': 'cow',
'diningtables': 'diningtable',
'horses': 'horse',
'motorbikes': 'motorbike',
'pottedplants': 'pottedplant',
'sheep': 'sheep',
'sofas': 'sofa',
'trains': 'train',
'tvmonitors': 'tvmonitor',
'trucks': 'truck',
}
if str in dic.keys():
str = dic[str]
return str
def detect_food(self, img_path):
if self.food is None:
self.food = Food()
score, name = self.food.detect(img_path)
if score > 0.1:
self.msg = " * " + name
else:
self.msg = " * no food"
def image_segmentation(self, img_path):
if self.pspnet is None:
self.pspnet = MyPSPNET()
arr = self.pspnet.detect(img_path)
arr = arr.astype(np.uint8)
img = Image.fromarray( arr )
self.msg = " * done"
return img
if __name__ == '__main__':
p = IPDSL()
# p.process('increase the brightness by 20')
# p.process('apply a filter black_white')