diff --git a/data/extracted/.gitignore b/data/extracted/.gitignore new file mode 100644 index 0000000..28e94f5 --- /dev/null +++ b/data/extracted/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this .gitignore file +!.gitignore \ No newline at end of file diff --git a/data/results/.gitignore b/data/results/.gitignore new file mode 100644 index 0000000..28e94f5 --- /dev/null +++ b/data/results/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this .gitignore file +!.gitignore \ No newline at end of file diff --git a/data/results/.gitkeep b/data/results/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/detector.py b/src/detector.py index bd5cb8c..1dbeeaf 100644 --- a/src/detector.py +++ b/src/detector.py @@ -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, diff --git a/src/face_detection.py b/src/face_detection.py index 31da5fe..9db197f 100644 --- a/src/face_detection.py +++ b/src/face_detection.py @@ -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 \ No newline at end of file + # 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 diff --git a/src/face_recognition.py b/src/face_recognition.py index bdffc70..93c53f2 100644 --- a/src/face_recognition.py +++ b/src/face_recognition.py @@ -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( diff --git a/src/main.py b/src/main.py index 6c08a72..0464b81 100644 --- a/src/main.py +++ b/src/main.py @@ -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() \ No newline at end of file diff --git a/src/recognizer.py b/src/recognizer.py index b4bff79..1b26512 100644 --- a/src/recognizer.py +++ b/src/recognizer.py @@ -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", "" ) diff --git a/src/utils.py b/src/utils.py index e994258..d7e91e8 100644 --- a/src/utils.py +++ b/src/utils.py @@ -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 \ No newline at end of file + 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)