-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetection.py
32 lines (27 loc) · 1.08 KB
/
detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cv2, numpy as np
from typing import Any, Dict
from geo import Point, Rectangle
class face_Detection_Haar:
def __init__(self, path_config_xml:str='', config_detection:Dict={}) -> None:
self.path_config_xml = path_config_xml
self.config_detection = config_detection
self.detectors = cv2.CascadeClassifier(self.path_config_xml)
def detection(self, frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faceRects = self.detectors.detectMultiScale(gray, **self.config_detection, flags=cv2.CASCADE_SCALE_IMAGE)
face_ROI = []
for (fX, fY, fW, fH) in faceRects:
rec = Rectangle([Point(x=fX, y=fY),
Point(x=fX + fW, y=fY + fH),
Point(x=fX, y=fY + fH),
Point(x=fX + fW, y=fY)
])
face_ROI.append(rec)
return face_ROI
def detection_one_face(self, frame):
face = None
face_ROI = self.detection(frame)
if len(face_ROI) != 0:
idx_max = np.argmax([rec.area for rec in face_ROI])
face = face_ROI[idx_max]
return face