Skip to content

Commit 948eecc

Browse files
committed
added face detection
Signed-off-by: Mpho Mphego <mpho112@gmail.com>
1 parent d3d38b3 commit 948eecc

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ def main(args):
136136

137137
video_feed = InputFeeder(input_file=args.input)
138138

139+
# Add source width and height for face detection.
140+
face_detection._init_image_w = video_feed.source_width
141+
face_detection._init_image_h = video_feed.source_height
142+
139143
for frame in video_feed.next_frame():
144+
predict_end_time, pred_result = face_detection.predict(frame,draw=True)
145+
text = f"Inference time: {predict_end_time:.2f}ms"
146+
face_detection.add_text(text, frame, (15, face_detection._init_image_h - 50))
147+
140148
if args.debug:
141149
video_feed.show(frame)
142150

src/input_feeder.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ def progress_bar(self):
7878
self._progress_bar = tqdm(total=int(self.video_len - self.fps + 1))
7979
return self._progress_bar
8080

81+
def resize(self,frame):
82+
return cv2.resize(frame, (self.source_width - 200, self.source_height - 200))
83+
8184
def show(self, frame, frame_name="video"):
82-
cv2.imshow(frame_name, frame)
85+
cv2.imshow(frame_name, self.resize(frame))
8386

8487
def write_video(self, output_path=".", filename="output_video.mp4"):
8588
out_video = cv2.VideoWriter(
@@ -100,15 +103,14 @@ def next_frame(self, quit_key="q"):
100103
flag, frame = self.cap.read()
101104

102105
if not flag:
103-
break
106+
break
104107
yield frame
105108

106109
key = cv2.waitKey(1) & 0xFF
107110
# if `quit_key` was pressed, break from the loop
108111
if key == ord(quit_key):
109112
break
110113

111-
112114
def close(self):
113115
"""Closes the VideoCapture."""
114116
if "image" in self._input_type:

src/model.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,30 @@ def predict(self, image, request_id=0, draw=False):
9292
)
9393
status = self.exec_network.requests[request_id].wait(-1)
9494
if status == 0:
95+
predict_start_time = time.time()
9596
pred_result = self.exec_network.requests[request_id].outputs[
9697
self.output_name
9798
]
98-
return pred_result
99+
predict_end_time = (time.time() - predict_start_time) * 1000
100+
if draw:
101+
self.preprocess_output(pred_result, image, show_bbox=draw)
102+
return (predict_end_time, pred_result)
99103

100104
@abc.abstractmethod
101-
def preprocess_output(self, inference_results, image):
105+
def preprocess_output(self, inference_results, image, show_bbox=False):
102106
"""Draw bounding boxes onto the frame."""
103107
pass
104108

105109
@staticmethod
106110
@abc.abstractmethod
107-
def draw_output(coords, image):
111+
def draw_output(image, ):
108112
pass
109113

114+
def add_text(self, text, frame, position, font_size=0.75, color=(255, 255, 255)):
115+
cv2.putText(
116+
frame, text, position, cv2.FONT_HERSHEY_COMPLEX, font_size, color, 1,
117+
)
118+
110119
def preprocess_input(self, image):
111120
"""Helper function for processing frame"""
112121
p_frame = cv2.resize(image, (self.input_shape[3], self.input_shape[2]))
@@ -122,7 +131,7 @@ class Face_Detection(Base):
122131
def __init__(self, model_name, device="CPU", threshold=0.60, extensions=None):
123132
super().__init__(model_name, device="CPU", threshold=0.60, extensions=None)
124133

125-
def preprocess_output(self, inference_results, image):
134+
def preprocess_output(self, inference_results, image, show_bbox=False):
126135
"""Draw bounding boxes onto the frame."""
127136
if not (self._init_image_w and self._init_image_h):
128137
raise RuntimeError("Initial image width and height cannot be None.")
@@ -136,10 +145,13 @@ def preprocess_output(self, inference_results, image):
136145
xmax = int(box[5] * self._init_image_w)
137146
ymax = int(box[6] * self._init_image_h)
138147
coords.append((xmin, ymin, xmax, ymax))
148+
if show_bbox:
149+
self.draw_output(image, xmin, ymin, xmax, ymax)
139150
return coords, image
140151

141-
def draw_output(coords, image):
142-
label = "Person"
152+
@staticmethod
153+
def draw_output(image, xmin, ymin, xmax, ymax):
154+
label = "Person's Face"
143155
bbox_color = (0, 255, 0)
144156
padding_size = (0.05, 0.25)
145157
text_color = (255, 255, 255)

0 commit comments

Comments
 (0)