added face recognition + extraction
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this .gitignore file
|
||||
!.gitignore
|
||||
@@ -0,0 +1,4 @@
|
||||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this .gitignore file
|
||||
!.gitignore
|
||||
+1
-1
@@ -9,7 +9,7 @@ class Detector():
|
||||
|
||||
if self.detector is None:
|
||||
self.detector = cv2.FaceDetectorYN.create(
|
||||
"../models/face_detection_yunet_2026may.onnx",
|
||||
"./models/face_detection_yunet_2026may.onnx",
|
||||
"",
|
||||
(320,320),
|
||||
0.85,
|
||||
|
||||
+11
-11
@@ -2,18 +2,18 @@ import cv2
|
||||
|
||||
from detector import *
|
||||
|
||||
def detect_in_image(image_path:str, scale = 1):
|
||||
def detect_face_in_image(img, 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)
|
||||
imgW = img.shape[1] * scale
|
||||
imgH = img.shape[0] * scale
|
||||
|
||||
return face
|
||||
# Redimensionner l'image pour le modèle
|
||||
img_resized = cv2.resize(img, (imgW, imgH,))
|
||||
|
||||
detector.setInputSize((img.shape[1], img.shape[0]))
|
||||
|
||||
faces_raw = detector.detect(img)
|
||||
|
||||
return faces_raw
|
||||
|
||||
@@ -4,10 +4,13 @@ from recognizer import Recognizer
|
||||
|
||||
def extract_faces(faces:list, img:list, filename:str):
|
||||
|
||||
for i in faces:
|
||||
recognizer = Recognizer.get()
|
||||
|
||||
for i in range(1, len(faces)):
|
||||
face = faces[i]
|
||||
croped_img = recognizer.alignCrop(img, face[1][0])
|
||||
cv2.imwrite(f"../data/extracted/{filename}_{i}.png")
|
||||
|
||||
croped_img = recognizer.alignCrop(img, face[0])
|
||||
cv2.imwrite(f"./data/extracted/{filename}_{i}.png", croped_img)
|
||||
|
||||
|
||||
def is_same_face(
|
||||
|
||||
+24
@@ -1,5 +1,29 @@
|
||||
import cv2
|
||||
import os
|
||||
|
||||
from utils import *
|
||||
from face_detection import *
|
||||
from face_recognition import *
|
||||
|
||||
# list all files in data/raw
|
||||
files = list_all_files("./data/raw", ["jpg", "webp"])
|
||||
|
||||
# process each files
|
||||
for file_path in files:
|
||||
|
||||
filename = file_path.split("/")[-1].split(".")[0]
|
||||
|
||||
# load image
|
||||
img = cv2.imread(file_path)
|
||||
if img is None:
|
||||
print(f"ERROR: Could not read image at {file_path}")
|
||||
continue
|
||||
cv2.imshow("image1", img)
|
||||
|
||||
# detect faces file
|
||||
faces = detect_face_in_image(img, scale=1)
|
||||
|
||||
# extract faces from file
|
||||
extract_faces(faces, img, filename)
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
+2
-2
@@ -8,8 +8,8 @@ class Recognizer():
|
||||
def get(self):
|
||||
|
||||
if self.recognizer is None:
|
||||
recognizer = cv2.FaceRecognizerSF.create(
|
||||
"../models/face_recognition_sface_2021dec_int8.onnx",
|
||||
self.recognizer = cv2.FaceRecognizerSF.create(
|
||||
"./models/face_recognition_sface_2021dec_int8.onnx",
|
||||
""
|
||||
)
|
||||
|
||||
|
||||
+25
-1
@@ -1,3 +1,27 @@
|
||||
import os
|
||||
|
||||
def list_all_files(path='.', extension:list[str] = []):
|
||||
|
||||
dirs = [path]
|
||||
res = []
|
||||
while len(dirs) > 0:
|
||||
for entry in os.listdir(dirs[0]):
|
||||
|
||||
def load_image
|
||||
full_path = os.path.join(dirs[0], entry)
|
||||
|
||||
if os.path.isdir(full_path):
|
||||
dirs.append(full_path)
|
||||
continue
|
||||
|
||||
if len(extension) == 0:
|
||||
res.append(full_path)
|
||||
elif full_path.split(".")[-1] in extension:
|
||||
res.append(full_path)
|
||||
|
||||
dirs.pop(0)
|
||||
|
||||
return res
|
||||
|
||||
if __name__ == "__main__":
|
||||
files = list_all_files("./data/raw/", ["jpg", "webp"])
|
||||
print(files)
|
||||
|
||||
Reference in New Issue
Block a user