Skip to content

Commit 0318d02

Browse files
committed
feat: added logging feature
1 parent 923ffed commit 0318d02

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ optional arguments:
244244
suitable plugin for device specified (CPU by default)
245245
9. `-pt` Probability threshold for face detections filtering(0.5 by
246246
default)
247+
10. `-pr` Set to True if you want to preview visualizations on person face.
247248
## Benchmarks
248249

249250
### Model Loading Time

src/face_detector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def predict(self, image):
8282
# Start Async Inference Request
8383
infer_request_handle = self.exec_network.start_async(
8484
request_id=0, inputs={self.input_blob: input_img})
85-
85+
8686
# wait for the output and return.
8787
if infer_request_handle.wait(-1) == 0:
8888
model_output = infer_request_handle.outputs[self.output_blob]
@@ -130,7 +130,7 @@ def preprocess_output(self, outputs, threshold, image, image_w, image_h):
130130
(x_min, y_min) = (int(det[3] * image_w), int(det[4] * image_h))
131131
(x_max, y_max) = (int(det[5] * image_w), int(det[6] * image_h))
132132
cropped_face = image[y_min:y_max, x_min:x_max]
133-
return cropped_face
133+
return cropped_face, ((x_min, y_min), (x_max, y_max))
134134

135135
def get_input_shape(self):
136136
""" Return the shape of the input layer """

src/landmark_detector.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,11 @@ def preprocess_output(self, outputs, image):
121121

122122
x_left_eye, y_left_eye = outputs[0][0][0][0], outputs[0][1][0][0]
123123
x_right_eye, y_right_eye = outputs[0][2][0][0], outputs[0][3][0][0]
124-
125-
left_eye = self.crop_eyes(x_left_eye, y_left_eye, image)
126-
right_eye = self.crop_eyes(x_right_eye, y_right_eye, image)
127-
# print(left_eye.shape, right_eye.shape)
128-
# cv2.imwrite("left.png", left_eye)
129-
# cv2.imwrite("right.png", right_eye)
130-
return left_eye, right_eye
124+
# make cropped eye and its coordinates
125+
left_eye, left_coords = self.crop_eyes(x_left_eye, y_left_eye, image)
126+
right_eye, right_coords = self.crop_eyes(x_right_eye, y_right_eye,
127+
image)
128+
return left_eye, left_coords, right_eye, right_coords
131129

132130
def crop_eyes(self, x_axis, y_axis, image):
133131
w, h = image.shape[1], image.shape[0]
@@ -137,7 +135,7 @@ def crop_eyes(self, x_axis, y_axis, image):
137135
y_max = int(y_axis * h) + 30
138136

139137
cropped_eye = image[y_min:y_max, x_min:x_max]
140-
return cropped_eye
138+
return cropped_eye, ((x_min, y_min), (x_max, y_max))
141139

142140
def get_input_shape(self):
143141
""" Return the shape of the input layer """

src/main.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)