forked from luxonis/depthai-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
92 lines (84 loc) · 3.06 KB
/
script.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
import time
bboxes = [] # List of face BBs
l = [] # List of images
# So the correct frame will be the first in the list
# For this experiment this function is redundant, since everything
# runs in blocking mode, so no frames will get lost
def remove_prev_frames(seq):
if len(l) == 0:
return
for rm, frame in enumerate(l):
if frame.getSequenceNum() == seq:
# node.warn(f"List len {len(l)} Frame with same seq num: {rm},seq {seq}")
break
for i in range(rm):
l.pop(0)
def find_frame(seq):
for frame in l:
if frame.getSequenceNum() == seq:
return frame
def correct_bb(bb):
if bb.xmin < 0: bb.xmin = 0.0
if bb.ymin < 0: bb.ymin = 0.0
if bb.xmax > 1: bb.xmax = 0.999
if bb.ymax > 1: bb.ymax = 0.999
return bb
while True:
time.sleep(0.001)
preview = node.io['preview'].tryGet()
if preview is not None:
# node.warn(f"New frame {preview.getSequenceNum()}, size {len(l)}")
l.append(preview)
# Max pool size is 10.
if 18 < len(l):
l.pop(0)
face_dets = node.io['face_det_in'].tryGet()
if face_dets is not None:
# node.warn(f"New detection start")
passthrough = node.io['face_pass'].get()
seq = passthrough.getSequenceNum()
# node.warn(f"New detection {seq}")
if len(l) == 0:
continue
img = find_frame(seq) # Matching frame is the first in the list
if img is None:
continue
for det in face_dets.detections:
bboxes.append(det) # For the rotation
cfg = ImageManipConfig()
correct_bb(det)
cfg.setCropRect(det.xmin, det.ymin, det.xmax, det.ymax)
cfg.setResize(60, 60)
cfg.setKeepAspectRatio(False)
node.io['manip_cfg'].send(cfg)
node.io['manip_img'].send(img)
headpose = node.io['headpose_in'].tryGet()
if headpose is not None:
# node.warn(f"New headpose")
passthrough = node.io['headpose_pass'].get()
seq = passthrough.getSequenceNum()
# node.warn(f"New headpose seq {seq}")
# Face rotation in degrees
r = headpose.getLayerFp16('angle_r_fc')[0] # Only 1 float in there
bb = bboxes.pop(0) # Get BB from the img detection
correct_bb(bb)
# remove_prev_frame(seq)
remove_prev_frames(seq)
if len(l) == 0:
continue
img = l.pop(0) # Matching frame is the first in the list
# node.warn('HP' + str(img))
# node.warn('bb' + str(bb))
cfg = ImageManipConfig()
rr = RotatedRect()
rr.center.x = (bb.xmin + bb.xmax) / 2
rr.center.y = (bb.ymin + bb.ymax) / 2
rr.size.width = bb.xmax - bb.xmin
rr.size.height = bb.ymax - bb.ymin
rr.angle = r # Rotate the rect in opposite direction
# True = coordinates are normalized (0..1)
cfg.setCropRotatedRect(rr, True)
cfg.setResize(112, 112)
cfg.setKeepAspectRatio(True)
node.io['manip2_cfg'].send(cfg)
node.io['manip2_img'].send(img)