From d7bc38ea167f54719de12373debff11761c6542e Mon Sep 17 00:00:00 2001 From: dmig Date: Fri, 7 Dec 2018 14:28:07 +0700 Subject: [PATCH] get rid of face_recognition module, support CNN --- src/cli/test.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/cli/test.py b/src/cli/test.py index cceb1a4..63af1d2 100644 --- a/src/cli/test.py +++ b/src/cli/test.py @@ -5,7 +5,7 @@ import configparser import os import time import cv2 -import face_recognition +import dlib # Get the absolute path to the current file path = os.path.dirname(os.path.abspath(__file__)) @@ -51,6 +51,14 @@ def print_text(line_number, text): """Print the status text by line number""" cv2.putText(overlay, text, (10, height - 10 - (10 * line_number)), cv2.FONT_HERSHEY_SIMPLEX, .3, (0, 255, 0), 0, cv2.LINE_AA) +use_cnn = config.getboolean('core', 'use_cnn') +if use_cnn: + face_detector = dlib.cnn_face_detection_model_v1( + path + '/../dlib-data/mmod_human_face_detector.dat' + ) +else: + face_detector = dlib.get_frontal_face_detector() + # Open the window and attach a a mouse listener cv2.namedWindow("Howdy Test") cv2.setMouseCallback("Howdy Test", mouse) @@ -86,7 +94,7 @@ try: # Grab a single frame of video - ret, frame = (video_capture.read()) + ret, frame = video_capture.read() # Make a frame to put overlays in overlay = frame.copy() @@ -100,7 +108,7 @@ try: # Fill with the overal containing percentage hist_perc = [] - # Loop though all values to calculate a pensentage and add it to the overlay + # Loop though all values to calculate a percentage and add it to the overlay for index, value in enumerate(hist): value_perc = float(value[0]) / hist_total * 100 hist_perc.append(value_perc) @@ -135,17 +143,20 @@ try: rec_tm = time.time() # Get the locations of all faces and their locations - face_locations = face_recognition.face_locations(frame) + face_locations = face_detector(frame, 1) # upsample 1 time rec_tm = time.time() - rec_tm # Loop though all faces and paint a circle around them for loc in face_locations: + if use_cnn: + loc = loc.rect + # Get the center X and Y from the rectangular points - x = int((loc[1] - loc[3]) / 2) + loc[3] - y = int((loc[2] - loc[0]) / 2) + loc[0] + x = int((loc.right() - loc.left()) / 2) + loc.left() + y = int((loc.bottom() - loc.top()) / 2) + loc.top() # Get the raduis from the with of the square - r = (loc[1] - loc[3]) / 2 + r = (loc.right() - loc.left()) / 2 # Add 20% padding r = int(r + (r * 0.2))