mti840-projet/edge/edge/imgproc.py

40 lines
1.1 KiB
Python

import cv2
import numpy as np
def vectorize_face(image, box, embedder):
face = extract_rectangle(image, box)
face_blob = cv2.dnn.blobFromImage(
face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False)
embedder.setInput(face_blob)
return embedder.forward().flatten()
def detect_faces(image, detector, confidence=0.5):
(height, width) = image.shape[:2]
full_box = np.array([width, height, width, height])
image_blob = cv2.dnn.blobFromImage(
cv2.resize(image, (300, 300)), 1.0, (300, 300),
(104.0, 177.0, 123.0), swapRB=False, crop=False)
detector.setInput(image_blob)
detections = detector.forward()
if len(detections) > 0:
for i in np.argsort(detections[0, 0, :, 2])[::-1]:
detection_confidence = detections[0, 0, i, 2]
if detection_confidence < confidence:
break
(x0, y0, x1, y1) = box = detections[0, 0, i, 3:7] * full_box
if x1 - x0 > 20 and y1 - y0 > 20:
yield box.astype('int')
def extract_rectangle(image, rect):
(x0, y0, x1, y1) = rect
return image[y0:y1, x0:x1]