/
interpreter.py
783 lines (754 loc) · 29.5 KB
/
interpreter.py
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import cv2
import csv
import numpy as np
import random
from benchmarks import benchmarks
from dsl import *
from image_utils import *
from utils import *
from typing import Any, List, Dict, Set, Tuple
map_outputs = {}
def compare_with_ground_truth(extr, img_to_environment, desc):
print(desc)
sampled_imgs = set()
num_tested = 0
num_correct = 0
while num_tested < 20 and len(sampled_imgs) < len(img_to_environment):
img_dir = random.choice(list(set(img_to_environment.keys()) - sampled_imgs))
img = cv2.imread("../imgman_v2/" + img_dir, 1)
sampled_imgs.add(img_dir)
details = img_to_environment[img_dir]['environment']
extracted_objs = eval_extractor(extr, details)
if not extracted_objs:
continue
for obj_id, details in details.items():
if obj_id not in extracted_objs:
continue
left, top, right, bottom = details["Loc"]
img_height, img_width = img.shape[0], img.shape[1]
if right > img_width or bottom > img_height:
continue
color = (0, 100, 0)
thickness = 2
keys = []
img = cv2.rectangle(img, (left, top), (right, bottom), color, thickness)
# Display image
cv2.imshow("image", img)
# Wait for a key to be pressed to exit
cv2.waitKey(0)
# Close the window
cv2.destroyAllWindows()
answer = input("Correct? y/n")
if answer == 'y':
num_correct += 1
num_tested += 1
if num_tested == 0:
return 0
return num_correct/num_tested
def partial_eval_map(map_extr, env, output_dict, eval_cache):
if partial_eval(map_extr.extractor, env, output_dict, eval_cache):
return True
elif partial_eval(map_extr.restriction, env, output_dict, eval_cache):
return True
val = set()
if map_extr.extractor.val is None or map_extr.restriction.val is None:
return False
extr_val = output_dict[map_extr.extractor.val]
rest_val = output_dict[map_extr.restriction.val]
if isinstance(map_extr.position, GetPrev):
# The idea: for each target obj we extract, we need to identify
# the obj who right boundary is as close to the target right boundary
# as possible, without being greater.
for target_obj_id in extr_val:
pos = env[target_obj_id]["ObjPosInImgLeftToRight"]
cur_obj_id = None
cur_pos = None
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if details["ObjPosInImgLeftToRight"] >= pos:
continue
if obj_id not in rest_val:
continue
if cur_pos is None or details["ObjPosInImgLeftToRight"] > cur_pos:
cur_obj_id = obj_id
cur_pos = details["ObjPosInImgLeftToRight"]
if cur_obj_id is not None:
val.add(cur_obj_id)
elif isinstance(map_extr.position, GetNext):
for target_obj_id in extr_val:
pos = env[target_obj_id]["ObjPosInImgLeftToRight"]
cur_obj_id = None
cur_pos = None
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if details["ObjPosInImgLeftToRight"] <= pos:
continue
if obj_id not in rest_val:
continue
if cur_pos is None or details["ObjPosInImgLeftToRight"] < cur_pos:
cur_obj_id = obj_id
cur_pos = details["ObjPosInImgLeftToRight"]
if cur_obj_id is not None:
val.add(cur_obj_id)
elif isinstance(map_extr.position, GetBelow):
for target_obj_id in extr_val:
target_left, target_top, target_right, _ = env[target_obj_id]["Loc"]
cur_obj_id = None
cur_top = None
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest_val:
continue
left, top, right, bottom = details["Loc"]
if top < target_top:
continue
if right < target_left or left > target_right:
continue
if cur_top is None or top < cur_top:
cur_top = top
cur_obj_id = obj_id
if cur_obj_id is not None:
val.add(cur_obj_id)
elif isinstance(map_extr.position, GetAbove):
for target_obj_id in extr_val:
target_left, target_top, target_right, target_bottom = env[target_obj_id][
"Loc"
]
cur_obj_id = None
cur_bottom = None
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest_val:
continue
left, top, right, bottom = details["Loc"]
if bottom > target_bottom:
continue
if right < target_left or left > target_right:
continue
if cur_bottom is None or bottom > cur_bottom:
cur_bottom = bottom
cur_obj_id = obj_id
if cur_obj_id is not None:
val.add(cur_obj_id)
elif isinstance(map_extr.position, GetLeft):
for target_obj_id in extr_val:
target_left, target_top, target_right, target_bottom = env[target_obj_id][
"Loc"
]
cur_obj_id = None
cur_left = None
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest_val:
continue
left, top, right, bottom = details["Loc"]
if left > target_left:
continue
if bottom < target_top or top > target_bottom:
continue
if cur_left is None or left > cur_left:
cur_left = left
cur_obj_id = obj_id
if cur_obj_id is not None:
val.add(cur_obj_id)
elif isinstance(map_extr.position, GetRight):
for target_obj_id in extr_val:
target_left, target_top, target_right, target_bottom = env[target_obj_id][
"Loc"
]
cur_obj_id = None
cur_left = None
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest_val:
continue
left, top, right, bottom = details["Loc"]
if left < target_left:
continue
if bottom < target_top or top > target_bottom:
continue
if cur_left is None or left < cur_left:
cur_left = left
cur_obj_id = obj_id
if cur_obj_id is not None:
val.add(cur_obj_id)
elif isinstance(map_extr.position, GetContains):
for target_obj_id in extr_val:
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest_val:
continue
if is_contained(details["Loc"], env[target_obj_id]["Loc"]):
val.add(obj_id)
elif isinstance(map_extr.position, GetIsContained):
for target_obj_id in extr_val:
for obj_id, details in env.items():
if details["ImgIndex"] != env[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest_val:
continue
if is_contained(env[target_obj_id]["Loc"], details["Loc"]):
val.add(obj_id)
if len(val) == 0:
return True
map_extr.val = str(val)
if str(val) not in output_dict:
output_dict[str(val)] = val
eval_cache[str(map_extr)] = val
return False
def eval_map(
map_extr: Map,
details: Dict[str, Dict[str, Any]],
rec: bool = True,
output_dict={},
eval_cache={},
) -> Set[str]:
if rec:
objs = eval_extractor(
map_extr.extractor, details, output_dict=output_dict, eval_cache=eval_cache
)
rest = eval_extractor(
map_extr.restriction,
details,
output_dict=output_dict,
eval_cache=eval_cache,
)
else:
objs = map_extr.extractor
rest = map_extr.restriction
mapped_objs = set()
if isinstance(map_extr.position, GetPrev):
# The idea: for each target obj we extract, we need to identify
# the obj who right boundary is as close to the target right boundary
# as possible, without being greater.
for target_obj_id in objs:
pos = details[target_obj_id]["ObjPosInImgLeftToRight"]
cur_obj_id = None
cur_pos = None
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if details_map["ObjPosInImgLeftToRight"] >= pos:
continue
if obj_id not in rest:
continue
if cur_pos is None or details_map["ObjPosInImgLeftToRight"] > cur_pos:
cur_obj_id = obj_id
cur_pos = details_map["ObjPosInImgLeftToRight"]
if cur_obj_id is not None:
mapped_objs.add(cur_obj_id)
elif isinstance(map_extr.position, GetNext):
for target_obj_id in objs:
pos = details[target_obj_id]["ObjPosInImgLeftToRight"]
cur_obj_id = None
cur_pos = None
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if details_map["ObjPosInImgLeftToRight"] <= pos:
continue
if obj_id not in rest:
continue
if cur_pos is None or details_map["ObjPosInImgLeftToRight"] < cur_pos:
cur_obj_id = obj_id
cur_pos = details_map["ObjPosInImgLeftToRight"]
if cur_obj_id is not None:
mapped_objs.add(cur_obj_id)
elif isinstance(map_extr.position, GetBelow):
for target_obj_id in objs:
target_left, target_top, target_right, _ = details[target_obj_id]["Loc"]
cur_obj_id = None
cur_top = None
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest:
continue
left, top, right, bottom = details_map["Loc"]
if top < target_top:
continue
if right < target_left or left > target_right:
continue
if cur_top is None or top < cur_top:
cur_top = top
cur_obj_id = obj_id
if cur_obj_id is not None:
mapped_objs.add(cur_obj_id)
elif isinstance(map_extr.position, GetAbove):
for target_obj_id in objs:
target_left, target_top, target_right, target_bottom = details[
target_obj_id
]["Loc"]
cur_obj_id = None
cur_bottom = None
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest:
continue
left, top, right, bottom = details_map["Loc"]
if bottom > target_bottom:
continue
if right < target_left or left > target_right:
continue
if cur_bottom is None or bottom > cur_bottom:
cur_bottom = bottom
cur_obj_id = obj_id
if cur_obj_id is not None:
mapped_objs.add(cur_obj_id)
elif isinstance(map_extr.position, GetLeft):
for target_obj_id in objs:
target_left, target_top, target_right, target_bottom = details[
target_obj_id
]["Loc"]
cur_obj_id = None
cur_left = None
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest:
continue
left, top, right, bottom = details_map["Loc"]
if left > target_left:
continue
if bottom < target_top or top > target_bottom:
continue
if cur_left is None or left > cur_left:
cur_left = left
cur_obj_id = obj_id
if cur_obj_id is not None:
mapped_objs.add(cur_obj_id)
elif isinstance(map_extr.position, GetRight):
for target_obj_id in objs:
target_left, target_top, target_right, target_bottom = details[
target_obj_id
]["Loc"]
cur_obj_id = None
cur_left = None
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest:
continue
left, top, right, bottom = details_map["Loc"]
if left < target_left:
continue
if bottom < target_top or top > target_bottom:
continue
if cur_left is None or left < cur_left:
cur_left = left
cur_obj_id = obj_id
if cur_obj_id is not None:
mapped_objs.add(cur_obj_id)
elif isinstance(map_extr.position, GetContains):
for target_obj_id in objs:
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest:
continue
if is_contained(details_map["Loc"], details[target_obj_id]["Loc"]):
mapped_objs.add(obj_id)
elif isinstance(map_extr.position, GetIsContained):
for target_obj_id in objs:
for obj_id, details_map in details.items():
if details_map["ImgIndex"] != details[target_obj_id]["ImgIndex"]:
continue
if obj_id == target_obj_id:
continue
if obj_id not in rest:
continue
if is_contained(details[target_obj_id]["Loc"], details_map["Loc"]):
mapped_objs.add(obj_id)
if rec:
return mapped_objs
else:
return mapped_objs
def partial_eval(extractor, env, output_dict, eval_cache, top_level=False):
if not isinstance(extractor, Extractor):
return False
if extractor.val is not None:
return False
if str(extractor) in eval_cache:
val = eval_cache[str(extractor)]
extractor.val = str(val)
if len(val) == 0:
return True
return False
faces = {obj for obj in env.keys() if env[obj]["Type"] == "Face"}
text_objects = {obj for obj in env.keys() if env[obj]["Type"] == "Text"}
objects = {obj for obj in env.keys() if env[obj]["Type"] == "Object"}
val = None
if isinstance(extractor, IsFace):
val = faces
elif isinstance(extractor, IsText):
val = text_objects
elif isinstance(extractor, GetFace):
val = set()
if not faces:
val = set()
elif not isinstance(extractor.index, int):
return False
for (obj_id, details) in env.items():
if details["Type"] != "Face":
continue
if details["Index"] == extractor.index:
val.add(obj_id)
elif isinstance(extractor, IsObject):
val = set()
if not objects:
val = set()
elif not isinstance(extractor.obj, str):
return False
for (obj_id, details) in env.items():
if details["Type"] != "Object":
continue
if details["Name"] == extractor.obj:
val.add(obj_id)
elif isinstance(extractor, MatchesWord):
val = set()
if not text_objects:
val = set()
elif not isinstance(extractor.word, str):
return False
for (obj_id, details) in env.items():
if details["Type"] != "Text":
continue
if details["Text"].lower() == extractor.word.lower():
val.add(obj_id)
elif isinstance(extractor, BelowAge):
val = set()
if not faces:
val = set()
elif not isinstance(extractor.age, int):
return False
for (obj_id, details) in env.items():
if details["Type"] != "Face":
continue
if extractor.age > details["AgeRange"]["Low"]:
val.add(obj_id)
elif isinstance(extractor, AboveAge):
val = set()
if not faces:
val = set()
elif not isinstance(extractor.age, int):
return False
for (obj_id, details) in env.items():
if details["Type"] != "Face":
continue
if extractor.age < details["AgeRange"]["High"]:
val.add(obj_id)
elif isinstance(extractor, Union):
should_prune = False
for sub_extr in extractor.extractors:
should_prune = (
partial_eval(sub_extr, env, output_dict, eval_cache) or should_prune
)
vals = []
none_vals = []
val_total = set()
for i, sub_extr in enumerate(extractor.extractors):
if sub_extr.val is None:
none_vals.append(i)
else:
vals.append(output_dict[sub_extr.val])
val_total.update(output_dict[sub_extr.val])
if len(none_vals) == 1 and extractor.output_over == extractor.output_under:
sub_extr = extractor.extractors[none_vals[0]]
output_under = output_dict[sub_extr.output_under]
output_over = output_dict[sub_extr.output_over]
new_output_under = output_over - val_total
update_output_approx(
sub_extr, new_output_under, output_over, env, output_dict
)
if vals and not none_vals:
val = set.union(*vals)
elif set(env.keys()) in vals:
val = set(env.keys())
if should_prune:
return True
elif isinstance(extractor, Intersection):
should_prune = False
for sub_extr in extractor.extractors:
should_prune = (
partial_eval(sub_extr, env, output_dict, eval_cache) or should_prune
)
vals = []
none_vals = []
val_total = set()
for i, sub_extr in enumerate(extractor.extractors):
if sub_extr.val is None:
none_vals.append(i)
else:
vals.append(output_dict[sub_extr.val])
val_total = val_total.intersection(output_dict[sub_extr.val])
if len(none_vals) == 1 and extractor.output_over == extractor.output_under:
sub_extr = extractor.extractors[none_vals[0]]
output_under = output_dict[sub_extr.output_under]
output_over = output_dict[sub_extr.output_over]
new_output_over = set(env.keys()) - (val_total - output_under)
update_output_approx(
sub_extr, output_under, new_output_over, env, output_dict
)
if vals and not none_vals:
val = set.intersection(*vals)
elif set() in vals:
val = set()
if should_prune:
return True
elif isinstance(extractor, Complement):
if partial_eval(extractor.extractor, env, output_dict, eval_cache):
return True
if extractor.extractor.val is not None:
val = set(env.keys()) - output_dict[extractor.extractor.val]
elif (
isinstance(extractor, IsPhoneNumber)
or isinstance(extractor, IsPrice)
or isinstance(extractor, IsSmiling)
or isinstance(extractor, EyesOpen)
or isinstance(extractor, MouthOpen)
):
val = {obj for obj in env if str(extractor) in env[obj]}
elif isinstance(extractor, Map):
return partial_eval_map(extractor, env, output_dict, eval_cache)
if val is not None:
extractor.val = str(val)
eval_cache[str(extractor)] = val
if str(val) not in output_dict:
output_dict[str(val)] = val
if len(val) == 0:
return True
return False
def update_output_approx(prog, output_under, output_over, env, output_dict):
if (
isinstance(prog, str)
or isinstance(prog, int)
or output_under == prog.output_under
and output_over == prog.output_over
):
return
over_str = str(output_over)
under_str = str(output_under)
if over_str not in output_dict:
output_dict[over_str] = output_over
if under_str not in output_dict:
output_dict[under_str] = output_under
prog.output_under = under_str
prog.output_over = over_str
if isinstance(prog, Union):
for sub_extr in prog.extractors:
update_output_approx(sub_extr, set(), output_over, env, output_dict)
elif isinstance(prog, Intersection):
for sub_extr in prog.extractors:
update_output_approx(
sub_extr, output_under, set(env.keys()), env, output_dict
)
elif isinstance(prog, Complement):
update_output_approx(
prog.extractor,
set(env.keys()) - output_over,
set(env.keys() - output_under),
env,
output_dict,
)
elif isinstance(prog, Map):
update_output_approx(prog.extractor, set(), set(env.keys()), env, output_dict)
update_output_approx(
prog.restriction, output_under, set(env.keys()), env, output_dict
)
elif isinstance(prog, MatchesWord):
update_output_approx(prog.word, output_under, output_over, env, output_dict)
elif isinstance(prog, GetFace):
update_output_approx(prog.index, output_under, output_over, env, output_dict)
elif isinstance(prog, IsObject):
update_output_approx(prog.obj, output_under, output_over, env, output_dict)
def eval_extractor(
extractor: Extractor,
details: Dict[str, Dict[str, Any]],
rec: bool = True,
output_dict={},
eval_cache=None,
): # -> Set[dict[str, str]]:
if output_dict and extractor.val is not None:
return output_dict[extractor.val]
if eval_cache and str(extractor) in eval_cache:
return eval_cache[str(extractor)]
if isinstance(extractor, Map):
res = eval_map(extractor, details, rec, output_dict, eval_cache)
elif isinstance(extractor, IsFace):
# list of all face ids in target image
res = {obj for obj in details.keys() if details[obj]["Type"] == "Face"}
elif isinstance(extractor, IsText):
res = {obj for obj in details.keys() if details[obj]["Type"] == "Text"}
elif isinstance(extractor, GetFace):
objs = set()
for (obj_id, obj_details) in details.items():
if "Type" not in obj_details:
print("hi")
print(details)
if obj_details["Type"] != "Face":
continue
if obj_details["Index"] == extractor.index:
objs.add(obj_id)
res = objs
elif isinstance(extractor, IsObject):
objs = set()
for (obj_id, obj_details) in details.items():
if obj_details["Type"] != "Object":
continue
if obj_details["Name"] == extractor.obj:
objs.add(obj_id)
res = objs
elif isinstance(extractor, MatchesWord):
objs = set()
for (obj_id, obj_details) in details.items():
if obj_details["Type"] != "Text":
continue
if obj_details["Text"].lower() == extractor.word.lower():
objs.add(obj_id)
res = objs
elif isinstance(extractor, BelowAge):
objs = set()
for (obj_id, obj_details) in details.items():
if obj_details["Type"] != "Face":
continue
if extractor.age > obj_details["AgeRange"]["Low"]:
objs.add(obj_id)
res = objs
elif isinstance(extractor, AboveAge):
objs = set()
for (obj_id, obj_details) in details.items():
if obj_details["Type"] != "Face":
continue
if extractor.age < obj_details["AgeRange"]["High"]:
objs.add(obj_id)
res = objs
elif isinstance(extractor, Union):
if rec:
res = set()
for sub_extr in extractor.extractors:
res = res.union(
eval_extractor(sub_extr, details, rec, output_dict, eval_cache)
)
res = res
else:
res = set()
for sub_extr in extractor.extractors:
res = res.union(sub_extr.objs)
res = res
elif isinstance(extractor, Intersection):
if rec:
res = set(details.keys())
for sub_extr in extractor.extractors:
res = res.intersection(
eval_extractor(sub_extr, details, rec, output_dict, eval_cache)
)
else:
res = set()
for sub_extr in extractor.extractors:
res = res.intersection(sub_extr.objs)
elif isinstance(extractor, Complement):
# All objs in target image except those extracted
if rec:
extracted_objs = eval_extractor(
extractor.extractor, details, rec, output_dict, eval_cache
)
res = details.keys() - extracted_objs
else:
res = details.keys() - set(extractor.extractor.objs)
elif (
isinstance(extractor, IsPhoneNumber)
or isinstance(extractor, IsPrice)
or isinstance(extractor, IsSmiling)
or isinstance(extractor, EyesOpen)
or isinstance(extractor, MouthOpen)
):
res = {obj for obj in details if str(extractor) in details[obj]}
else:
print(extractor)
raise Exception
if eval_cache:
eval_cache[str(extractor)] = res
return res
def eval_crop(extracted_objs, details_map, imgs):
img = imgs[0]
cur_coords = None
for obj_id, details in details_map.items():
if obj_id not in extracted_objs:
continue
if cur_coords is None:
cur_coords = details["Loc"]
else:
cur_coords = (
min(cur_coords[0], details["Loc"][0]),
min(cur_coords[1], details["Loc"][1]),
max(cur_coords[2], details["Loc"][2]),
max(cur_coords[3], details["Loc"][3]),
)
left, top, right, bottom = cur_coords
img = img[top:bottom, left:right]
return [img]
def eval_apply_action(
statement: Statement,
details: Dict[str, Dict[str, Any]],
imgs,
extracted_objs=None,
):
# list of all obj ids we want to apply action to
if not extracted_objs:
extracted_objs = eval_extractor(statement.extractor, details)
action = statement.action
if isinstance(action, Crop):
return eval_crop(extracted_objs, details, imgs)
for obj_id, details in details.items():
img = imgs[0]
if obj_id not in extracted_objs:
continue
apply_action_to_object(action, img, details)
return imgs
def eval_program(prog: Program, imgs, details: Dict[str, Dict[str, Any]]):
for statement in prog.statements:
imgs = eval_apply_action(statement, details, imgs)
else:
for img in imgs:
# Display image
cv2.imshow("image", img)
# Wait for a key to be pressed to exit
cv2.waitKey(0)
# Close the window
cv2.destroyAllWindows()
def test_interpreter(args):
print("hi")
# TODO
if __name__ == "__main__":
args = get_args()
test_interpreter(args)