Show model label detected

This commit is contained in:
Alberto Fanjul 2021-01-31 11:00:05 +01:00
parent 39272035af
commit 108ddd5e94

View file

@ -2,11 +2,14 @@
# Import required modules # Import required modules
import configparser import configparser
import builtins
import os import os
import json
import sys import sys
import time import time
import dlib import dlib
import cv2 import cv2
import numpy as np
from recorders.video_capture import VideoCapture from recorders.video_capture import VideoCapture
# Get the absolute path to the current file # Get the absolute path to the current file
@ -59,6 +62,22 @@ if use_cnn:
else: else:
face_detector = dlib.get_frontal_face_detector() face_detector = dlib.get_frontal_face_detector()
pose_predictor = dlib.shape_predictor(path + "/../dlib-data/shape_predictor_5_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1(path + "/../dlib-data/dlib_face_recognition_resnet_model_v1.dat")
encodings = []
models = None
try:
user = builtins.howdy_user
models = json.load(open(path + "/../models/" + user + ".dat"))
for model in models:
encodings += model["data"]
except FileNotFoundError:
print("No face model known for the user " + user + ", please run:")
print("\n\tsudo howdy -U " + user + " add\n")
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
# Open the window and attach a a mouse listener # Open the window and attach a a mouse listener
@ -97,7 +116,7 @@ try:
sec_frames = 0 sec_frames = 0
# Grab a single frame of video # Grab a single frame of video
_, frame = video_capture.read_frame() orig_frame, frame = video_capture.read_frame()
frame = clahe.apply(frame) frame = clahe.apply(frame)
# Make a frame to put overlays in # Make a frame to put overlays in
@ -155,6 +174,23 @@ try:
if use_cnn: if use_cnn:
loc = loc.rect loc = loc.rect
color = (0, 0, 230)
if models:
face_landmark = pose_predictor(orig_frame, loc)
face_encoding = np.array(face_encoder.compute_face_descriptor(orig_frame, face_landmark, 1))
# Match this found face against a known face
matches = np.linalg.norm(encodings - face_encoding, axis=1)
# Get best match
match_index = np.argmin(matches)
match = matches[match_index]
percent = match * 100
label = models[match_index]["label"]
color = (230, 0, 0)
cv2.putText(overlay, "{} {}(%)".format(label, percent), (width - 68, 32), cv2.FONT_HERSHEY_SIMPLEX, .3, (0, 255, 0), 0, cv2.LINE_AA)
# Get the center X and Y from the rectangular points # Get the center X and Y from the rectangular points
x = int((loc.right() - loc.left()) / 2) + loc.left() x = int((loc.right() - loc.left()) / 2) + loc.left()
y = int((loc.bottom() - loc.top()) / 2) + loc.top() y = int((loc.bottom() - loc.top()) / 2) + loc.top()
@ -165,7 +201,7 @@ try:
r = int(r + (r * 0.2)) r = int(r + (r * 0.2))
# Draw the Circle in green # Draw the Circle in green
cv2.circle(overlay, (x, y), r, (0, 0, 230), 2) cv2.circle(overlay, (x, y), r, color, 2)
# Add the overlay to the frame with some transparency # Add the overlay to the frame with some transparency
alpha = 0.65 alpha = 0.65