| import os |
| import cv2 |
| import dlib |
| import numpy as np |
| from PIL import Image |
| import gradio as gr |
| import git |
|
|
| |
| REPO_URL = "https://github.com/Moustapha224/Reconnaissance" |
| REPO_DIR = "Reconnaissance" |
| KNOWN_FACES_DIR = os.path.join(REPO_DIR, "Known_faces_clean") |
|
|
| if not os.path.exists(REPO_DIR): |
| print(f"🔄 Clonage du dépôt GitHub depuis {REPO_URL} ...") |
| git.Repo.clone_from(REPO_URL, REPO_DIR) |
|
|
| |
| face_detector = dlib.get_frontal_face_detector() |
| shape_predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") |
| face_rec_model = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat") |
|
|
| |
| known_face_encodings = [] |
| known_face_names = [] |
|
|
| |
| def get_face_encoding(image): |
| if image is None or len(image.shape) != 3: |
| raise ValueError("Image invalide (None ou pas RGB)") |
| if image.shape[2] == 4: |
| image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB) |
| elif image.shape[2] != 3: |
| image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) |
|
|
| dets = face_detector(image, 1) |
| if len(dets) == 0: |
| return None |
|
|
| shape = shape_predictor(image, dets[0]) |
| face_descriptor = face_rec_model.compute_face_descriptor(image, shape) |
| return np.array(face_descriptor) |
|
|
| |
| def load_known_faces(): |
| print("🔍 Encodage des visages connus...") |
| for person in os.listdir(KNOWN_FACES_DIR): |
| person_path = os.path.join(KNOWN_FACES_DIR, person) |
| if not os.path.isdir(person_path): |
| continue |
| for file in os.listdir(person_path): |
| file_path = os.path.join(person_path, file) |
| try: |
| img = Image.open(file_path).convert("RGB") |
| rgb = np.array(img) |
| encoding = get_face_encoding(rgb) |
| if encoding is not None: |
| known_face_encodings.append(encoding) |
| known_face_names.append(person) |
| else: |
| print(f"⚠️ Aucun visage détecté dans {file_path}") |
| except Exception as e: |
| print(f"❌ Erreur avec {file_path}: {e}") |
|
|
| load_known_faces() |
|
|
| |
| def recognize_faces(image): |
| if image is None: |
| return None |
| try: |
| img = cv2.cvtColor(np.array(image), cv2.COLOR_RGBA2RGB if image.mode == "RGBA" else cv2.COLOR_RGB2BGR) |
| except: |
| return None |
|
|
| dets = face_detector(img, 1) |
| if len(dets) == 0: |
| return image |
|
|
| for d in dets: |
| shape = shape_predictor(img, d) |
| face_descriptor = face_rec_model.compute_face_descriptor(img, shape) |
| encoding = np.array(face_descriptor) |
|
|
| distances = np.linalg.norm(known_face_encodings - encoding, axis=1) |
| min_distance = np.min(distances) |
| name = "Inconnu" |
|
|
| if min_distance < 0.6: |
| name = known_face_names[np.argmin(distances)] |
|
|
| |
| x1, y1, x2, y2 = d.left(), d.top(), d.right(), d.bottom() |
| cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) |
| cv2.putText(img, name, (x1, y2 + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) |
|
|
| return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) |
|
|
| |
| iface = gr.Interface( |
| fn=recognize_faces, |
| inputs=gr.Image(label="Image à reconnaître"), |
| outputs=gr.Image(label="Résultat"), |
| title="Reconnaissance Faciale", |
| description="Téléversez une image contenant un visage pour l'identifier à partir des visages connus." |
| ) |
|
|
| iface.launch() |
|
|