-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdriver_LineProfiler_Report
More file actions
185 lines (183 loc) · 14.1 KB
/
driver_LineProfiler_Report
File metadata and controls
185 lines (183 loc) · 14.1 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
Timer unit: 1e-06 s
Total time: 18.9348 s
File: src/driver.py
Function: main at line 56
Line # Hits Time Per Hit % Time Line Contents
==============================================================
56 @profile
57 def main(args):
58 1 4.0 4.0 0.0 logger = logging.getLogger()
59
60 1 2.0 2.0 0.0 feeder = None
61 1 3.0 3.0 0.0 if args.input_type == constants.VIDEO or args.input_type == constants.IMAGE:
62 1 4.0 4.0 0.0 extension = str(args.input).split('.')[1]
63 1 3.0 3.0 0.0 if not extension.lower() in constants.ALLOWED_EXTENSIONS:
64 logger.error('Please provide supported extension.' + str(constants.ALLOWED_EXTENSIONS))
65 exit(1)
66
67 1 13.0 13.0 0.0 if not os.path.isfile(args.input):
68 logger.error("Unable to find specified video/image file")
69 exit(1)
70
71 1 6.0 6.0 0.0 feeder = InputFeeder(args.input_type, args.input)
72 elif args.input_type == constants.IP_CAMERA:
73 if not str(args.input).startswith('http://'):
74 logger.error('Please provide ip of server with http://')
75 exit(1)
76
77 feeder = InputFeeder(args.input_type, args.input)
78 elif args.input_type == constants.WEBCAM:
79 feeder = InputFeeder(args.input_type)
80
81 1 6.0 6.0 0.0 mc = MouseController("medium", "fast")
82
83 1 27702.0 27702.0 0.1 feeder.load_data()
84
85 1 5371.0 5371.0 0.0 face_model = Face_Model(args.face, args.device, args.cpu_extension)
86 1 6864.0 6864.0 0.0 face_model.check_model()
87
88 1 1313.0 1313.0 0.0 landmark_model = Landmark_Model(args.landmarks, args.device, args.cpu_extension)
89 1 372.0 372.0 0.0 landmark_model.check_model()
90
91 1 2658.0 2658.0 0.0 gaze_model = Gaze_Estimation_Model(args.gazeestimation, args.device, args.cpu_extension)
92 1 741.0 741.0 0.0 gaze_model.check_model()
93
94 1 2397.0 2397.0 0.0 head_model = Head_Pose_Model(args.headpose, args.device, args.cpu_extension)
95 1 418.0 418.0 0.0 head_model.check_model()
96
97 1 128593.0 128593.0 0.7 face_model.load_model()
98 1 22.0 22.0 0.0 logger.info("Face Detection Model Loaded...")
99 1 31148.0 31148.0 0.2 landmark_model.load_model()
100 1 12.0 12.0 0.0 logger.info("Landmark Detection Model Loaded...")
101 1 68474.0 68474.0 0.4 gaze_model.load_model()
102 1 13.0 13.0 0.0 logger.info("Gaze Estimation Model Loaded...")
103 1 57254.0 57254.0 0.3 head_model.load_model()
104 1 13.0 13.0 0.0 logger.info("Head Pose Detection Model Loaded...")
105 1 30.0 30.0 0.0 print('Loaded')
106
107 1 2.0 2.0 0.0 try:
108 1 2.0 2.0 0.0 frame_count = 0
109 60 2621170.0 43686.2 13.8 for ret, frame in feeder.next_batch():
110 60 263.0 4.4 0.0 if not ret:
111 1 11.0 11.0 0.0 break
112
113 59 156.0 2.6 0.0 if frame is None:
114 continue
115
116 59 162.0 2.7 0.0 frame_count += 1
117 59 137.0 2.3 0.0 crop_face = None
118 59 171.0 2.9 0.0 if frame_count % 3 == 0:
119
120 19 295913.0 15574.4 1.6 crop_face, box = face_model.predict(frame.copy())
121
122 19 68.0 3.6 0.0 if crop_face is None:
123 logger.error("Unable to detect the face.")
124 continue
125
126 (lefteye_x, lefteye_y), (
127 19 53.0 2.8 0.0 righteye_x, righteye_y), eye_coords, left_eye, right_eye = landmark_model.predict(
128 19 12236.0 644.0 0.1 crop_face.copy(), eye_surrounding_area=15)
129 # imshow("left_eye", left_eye, width=100)
130 # imshow("right_eye", right_eye, width=100)
131 '''TODO dlib is better to crop eye with perfection'''
132
133 19 27592.0 1452.2 0.1 head_position = head_model.predict(crop_face.copy())
134
135 19 27308.0 1437.3 0.1 gaze, (mousex, mousey) = gaze_model.predict(left_eye.copy(), right_eye.copy(), head_position)
136 # cv2.waitKey(0)
137 # mc.move(mousex, mousey)
138
139 19 91.0 4.8 0.0 if (len(args.debug) > 0):
140 19 13358.0 703.1 0.1 debuFrame = frame.copy()
141 19 55.0 2.9 0.0 if crop_face is None:
142 continue
143
144 19 38.0 2.0 0.0 thickness = 2
145 19 39.0 2.1 0.0 radius = 2
146 19 37.0 1.9 0.0 color = (0, 0, 255)
147 19 68.0 3.6 0.0 [[le_xmin, le_ymin, le_xmax, le_ymax], [re_xmin, re_ymin, re_xmax, re_ymax]] = eye_coords
148
149 '''
150 LandMark
151 '''
152
153 19 373.0 19.6 0.0 cv2.circle(crop_face, (lefteye_x, lefteye_y), radius, color, thickness)
154 19 124.0 6.5 0.0 cv2.circle(crop_face, (righteye_x, righteye_y), radius, color, thickness)
155
156 19 65.0 3.4 0.0 if 'headpose' in args.debug:
157 19 54.0 2.8 0.0 yaw = head_position[0]
158 19 40.0 2.1 0.0 pitch = head_position[1]
159 19 38.0 2.0 0.0 roll = head_position[2]
160
161 19 251.0 13.2 0.0 sinY = math.sin(yaw * math.pi / 180.0)
162 19 103.0 5.4 0.0 sinP = math.sin(pitch * math.pi / 180.0)
163 19 96.0 5.1 0.0 sinR = math.sin(roll * math.pi / 180.0)
164
165 19 100.0 5.3 0.0 cosY = math.cos(yaw * math.pi / 180.0)
166 19 103.0 5.4 0.0 cosP = math.cos(pitch * math.pi / 180.0)
167 19 110.0 5.8 0.0 cosR = math.cos(roll * math.pi / 180.0)
168
169 19 74.0 3.9 0.0 cH, cW = crop_face.shape[:2]
170 19 44.0 2.3 0.0 arrowLength = 0.4 * cH * cW
171
172 19 74.0 3.9 0.0 xCenter = int(cW / 2)
173 19 51.0 2.7 0.0 yCenter = int(cH / 2)
174
175 # center to right
176 19 45.0 2.4 0.0 cv2.line(crop_face, (xCenter, yCenter),
177 19 62.0 3.3 0.0 (int((xCenter + arrowLength * (cosR * cosY + sinY * sinP * sinR))),
178 19 145.0 7.6 0.0 int((yCenter + arrowLength * cosP * sinR))), (186, 204, 2), 1)
179
180 # center to top
181 19 38.0 2.0 0.0 cv2.line(crop_face, (xCenter, yCenter),
182 19 59.0 3.1 0.0 (int(((xCenter + arrowLength * (cosR * sinY * sinP + cosY * sinR)))),
183 19 140.0 7.4 0.0 int((yCenter - arrowLength * cosP * cosR))), (186, 204, 2), 1)
184
185 # center to forward
186 19 47.0 2.5 0.0 cv2.line(crop_face, (xCenter, yCenter),
187 19 39.0 2.1 0.0 (int(((xCenter + arrowLength * sinY * cosP))),
188 19 112.0 5.9 0.0 int((yCenter + arrowLength * sinP))), (186, 204, 2), 1)
189
190 19 247.0 13.0 0.0 cv2.putText(crop_face, 'head pose: (y={:.2f}, p={:.2f}, r={:.2f})'.format(yaw, pitch, roll),
191 19 62.0 3.3 0.0 (0, 20), cv2.FONT_HERSHEY_SIMPLEX,
192 19 574.0 30.2 0.0 0.35, (255, 255, 255), 1)
193
194 19 53.0 2.8 0.0 if 'gaze' in args.debug:
195 19 47.0 2.5 0.0 cH, cW = crop_face.shape[:2]
196 19 38.0 2.0 0.0 arrowLength = 0.4 * cH * cW
197
198 19 173.0 9.1 0.0 gazeArrowX = gaze[0] * arrowLength
199 19 107.0 5.6 0.0 gazeArrowY = -gaze[1] * arrowLength
200
201 19 129.0 6.8 0.0 cv2.rectangle(crop_face, (re_xmin, re_ymin), (re_xmax, re_ymax), (255, 255, 255))
202 19 84.0 4.4 0.0 cv2.rectangle(crop_face, (le_xmin, le_ymin), (le_xmax, le_ymax), (255, 255, 255))
203
204 19 42.0 2.2 0.0 cv2.arrowedLine(crop_face, (lefteye_x, lefteye_y),
205 19 444.0 23.4 0.0 (int(lefteye_x + gazeArrowX), int(lefteye_y + gazeArrowY)), (184, 113, 57), 2)
206 19 39.0 2.1 0.0 cv2.arrowedLine(crop_face, (righteye_x, righteye_y),
207 19 294.0 15.5 0.0 (int(righteye_x + gazeArrowX), int(righteye_y + gazeArrowY)), (184, 113, 57), 2)
208
209 19 66.0 3.5 0.0 cv2.putText(crop_face, 'gaze angles: h={}, v={}'.format("!", "2"), (0, 10),
210 19 38.0 2.0 0.0 cv2.FONT_HERSHEY_SIMPLEX,
211 19 295.0 15.5 0.0 0.35, (255, 255, 255), 1)
212
213 19 19628.0 1033.1 0.1 imshow("face", crop_face, width=400)
214 19 287.0 15.1 0.0 cv2.moveWindow("face", 0, 0)
215 19 157490.0 8288.9 0.8 imshow("debug", debuFrame, width=400)
216 19 141.0 7.4 0.0 cv2.moveWindow("debug", cW * 2, cH)
217
218 # cv2.waitKey(0)
219 # mc.move(gaze[0],gaze[1])
220 19 36.0 1.9 0.0 try:
221 19 15325862.0 806624.3 80.9 mc.move(gaze[0], gaze[1])
222 6 18.0 3.0 0.0 except Exception as err:
223 6 848.0 141.3 0.0 logger.error("Moving cursor outside the PC not supported yet !!")
224
225 # key = cv2.waitKey(60)
226 59 91570.0 1552.0 0.5 if cv2.waitKey(1) & 0xFF == ord('q'):
227 break
228 except Exception as err:
229 logger.error(err)
230
231 1 435.0 435.0 0.0 cv2.destroyAllWindows()
232 1 965.0 965.0 0.0 feeder.close()