base function (face detetion + face recognition)
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
import cv2
|
||||||
|
|
||||||
|
class Detector():
|
||||||
|
|
||||||
|
detector = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(self):
|
||||||
|
|
||||||
|
if self.detector is None:
|
||||||
|
self.detector = cv2.FaceDetectorYN.create(
|
||||||
|
"../models/face_detection_yunet_2026may.onnx",
|
||||||
|
"",
|
||||||
|
(320,320),
|
||||||
|
0.85,
|
||||||
|
0.3,
|
||||||
|
5000
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.detector
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import cv2
|
||||||
|
|
||||||
|
from detector import *
|
||||||
|
|
||||||
|
def detect_in_image(image_path:str, scale = 1):
|
||||||
|
|
||||||
|
img = cv2.imread(image_path)
|
||||||
|
|
||||||
|
imgW = img1.shape[1] * scale
|
||||||
|
imgH = img1.shape[0] * scale
|
||||||
|
|
||||||
|
img = cv2.resize(img, (imgH, imgW))
|
||||||
|
|
||||||
|
detector = Detector.get()
|
||||||
|
detector.setInputSize((imgH, imgW))
|
||||||
|
|
||||||
|
face = detector.detect(img)
|
||||||
|
|
||||||
|
return face
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import cv2
|
||||||
|
|
||||||
|
from recognizer import Recognizer
|
||||||
|
|
||||||
|
def extract_faces(faces:list, img:list, filename:str):
|
||||||
|
|
||||||
|
for i in faces:
|
||||||
|
face = faces[i]
|
||||||
|
croped_img = recognizer.alignCrop(img, face[1][0])
|
||||||
|
cv2.imwrite(f"../data/extracted/{filename}_{i}.png")
|
||||||
|
|
||||||
|
|
||||||
|
def is_same_face(
|
||||||
|
face1_img:list,
|
||||||
|
face2_img:list,
|
||||||
|
cosine_similarity_threshold:float = 0.363,
|
||||||
|
l2_similarity_threshold:float = 1.128
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
recognizer = Recognizer.get()
|
||||||
|
|
||||||
|
faces1_features = recognizer.feature(face1_img)
|
||||||
|
faces2_features = recognizer.feature(face2_img)
|
||||||
|
|
||||||
|
cosine_score = recognizer.match(faces1_features, faces2_features, cv2.FaceRecognizerSF_FR_COSINE)
|
||||||
|
l2_score = recognizer.match(faces1_features, faces2_features, cv2.FaceRecognizerSF_FR_NORM_L2)
|
||||||
|
|
||||||
|
return cosine_score >= cosine_similarity_threshold and l2_score <= l2_similarity_threshold
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import cv2
|
||||||
|
|
||||||
|
from face_detection import *
|
||||||
|
from face_recognition import *
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import cv2
|
||||||
|
|
||||||
|
class Recognizer():
|
||||||
|
|
||||||
|
recognizer = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(self):
|
||||||
|
|
||||||
|
if self.recognizer is None:
|
||||||
|
recognizer = cv2.FaceRecognizerSF.create(
|
||||||
|
"../models/face_recognition_sface_2021dec_int8.onnx",
|
||||||
|
""
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.recognizer
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
|
def load_image
|
||||||
Reference in New Issue
Block a user