@@ -87,6 +87,11 @@ def build_argparser():
8787 default = 0.5 ,
8888 help = "Probability threshold for face detections filtering"
8989 "(0.5 by default)" )
90+ parser .add_argument (
91+ "-pr" ,
92+ "--preview" ,
93+ action = 'store_true' ,
94+ help = "Use this flag if you want to preview visualizations on person face" )
9095 return parser
9196
9297
@@ -192,8 +197,8 @@ def infer_on_stream(args):
192197 ### Detect Face in Frame
193198 output = face_detector .predict (frame )
194199 ## Crop Face
195- face = face_detector .preprocess_output (output , args . prob_threshold ,
196- frame , initial_w , initial_h )
200+ face , face_coords = face_detector .preprocess_output (
201+ output , args . prob_threshold , frame , initial_w , initial_h )
197202
198203 # skip frame if face not found
199204 if not np .any (face ):
@@ -208,7 +213,7 @@ def infer_on_stream(args):
208213 landmarks = landmark_detector .predict (face )
209214 logger .info ("Face Landmarks detected" )
210215 ## Crop left and right Eye
211- left_eye , right_eye = landmark_detector .preprocess_output (
216+ left_eye , left_eye_coords , right_eye , right_eye_coords = landmark_detector .preprocess_output (
212217 landmarks , face )
213218
214219 ## Skip frame if any eye is not cropped correctly
@@ -223,6 +228,46 @@ def infer_on_stream(args):
223228 ## Get mouse coords (x, y)
224229 mouse_coords = gaze_estimator .preprocess_output (gaze , head_pose )
225230 logger .info ("New mouse coordinates: {}" .format (mouse_coords ))
231+ # Show Preview of input with drawn predictions
232+ if (args .preview ):
233+ # function draw rectangel around eye
234+ def rectange_eyes (frame , face_coords , eye_coords ):
235+ """Draw bounding box around Eye"""
236+ eye_start = (
237+ (face_coords [0 ][0 ] + eye_coords [0 ][0 ]), # x_min + x_min
238+ (face_coords [0 ][1 ] + eye_coords [0 ][1 ])) # y_min _ y_min
239+ eye_end = (
240+ (face_coords [0 ][0 ] + eye_coords [1 ][0 ]), # x_min + x_max
241+ (face_coords [0 ][1 ] + eye_coords [1 ][1 ])) # y_min + y_max
242+
243+ return cv2 .rectangle (frame , eye_start , eye_end , (0 , 0 , 255 ), 2 )
244+
245+ # draw box around face
246+ image = cv2 .rectangle (frame , face_coords [0 ], face_coords [1 ],
247+ (0 , 0 , 255 ), 2 )
248+ # draw box around left eye
249+ image = rectange_eyes (image , face_coords , left_eye_coords )
250+ # draw box around right eye
251+ image = rectange_eyes (image , face_coords , right_eye_coords )
252+ # show head pose values on image
253+ cv2 .putText (
254+ image ,
255+ "Head Pose: Yaw: {:.2f}, Pitch: {:.2f}, Roll: {:.2f}" .format (
256+ head_pose ["angle_y_fc" ][0 ][0 ],
257+ head_pose ["angle_y_fc" ][0 ][0 ],
258+ head_pose ["angle_y_fc" ][0 ][0 ],
259+ ), (40 , 40 ), cv2 .FONT_HERSHEY_COMPLEX , 0.8 , (0 , 255 , 0 ), 1 )
260+ # show head pose values on image
261+ cv2 .putText (
262+ image ,
263+ "Gaze: X-axis: {:.2f}, Y-axis: {:.2f}, Z-axis: {:.2f}" .format (
264+ gaze [0 ][0 ], gaze [0 ][1 ], gaze [0 ][2 ]), (40 , 70 ),
265+ cv2 .FONT_HERSHEY_COMPLEX , 0.8 , (0 , 255 , 0 ), 1 )
266+
267+ cv2 .imshow ('Preview | press q to close' , image )
268+ # break loop if q is pressed on output window
269+ if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
270+ break
226271
227272 print ("New mouse coordinates: {}\n \n " .format (mouse_coords ))
228273 ### Move Mouse
0 commit comments