[Python] Face recognition project "pip install...

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I've been developing a face recognition project with ChatGPT. It gave me some code I tried, but it told me to install pip install git+https://github.com/ageitgey/face_recognition_models But I did, still, when I ran the code, it was the same for this project. I installed CMake and the Visual Studio installer. I checked, it there was no problem with it

    I was trying to run it, but the error didn't go away

    my code:

    import os
    import cv2
    import face_recognition


    KNOWN_FACES_DIR = "known_faces"


    known_face_encodings = []
    known_face_names = []

    print("[INFO] Loading known faces...")

    for person_name in os.listdir(KNOWN_FACES_DIR):
    person_folder = os.path.join(KNOWN_FACES_DIR, person_name)

    if not os.path.isdir(person_folder):
    continue

    for filename in os.listdir(person_folder):
    filepath = os.path.join(person_folder, filename)

    # Load image
    image = face_recognition.load_image_file(filepath)
    encodings = face_recognition.face_encodings(image)

    if len(encodings) > 0:
    known_face_encodings.append(encodings[0])
    known_face_names.append(person_name)
    print(f"[LOADED] {filename} for {person_name}")
    else:
    print(f"[SKIPPED] No face found in {filename}")

    print("[DONE] All known faces loaded.\n")


    video = cv2.VideoCapture(0)

    print("[INFO] Starting webcam. Press 'q' to quit.")

    while True:
    ret, frame = video.read()
    if not ret:
    break

    # Resize frame for faster processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)


    face_locations = face_recognition.face_locations(rgb_small_frame)
    face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
    matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.5)
    name = "Unknown"

    if True in matches:
    first_match_index = matches.index(True)
    name = known_face_names[first_match_index]

    # Scale back up face locations
    top *= 4
    right *= 4
    bottom *= 4
    left *= 4

    # Draw rectangle and name
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
    cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 0), cv2.FILLED)
    cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)

    cv2.imshow("Face Recognition", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
    break

    video.release()
    cv2.destroyAllWindows()

    ```

    Continue reading...

Compartilhe esta Página