import face_recognition import cv2 import numpy as np # Load an image file image_path = "principal.jpg" image = face_recognition.load_image_file(image_path) # Find all face locations and encodings in the image face_locations = face_recognition.face_locations(image) face_encodings = face_recognition.face_encodings(image, face_locations) # Ensure only one face is found if len(face_encodings) == 1: # Save the encoding for later comparison known_face_encoding = face_encodings[0] else: print("Error: Expected exactly one face in the image.") # Load the image with an unknown face unknown_image_path = "2_principal.jpg" unknown_image = face_recognition.load_image_file(unknown_image_path) # Find all face locations and encodings in the unknown image unknown_face_locations = face_recognition.face_locations(unknown_image) unknown_face_encodings = face_recognition.face_encodings(unknown_image, unknown_face_locations) # Initialize OpenCV window window_name = "Recognized Person" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) # Draw a red rectangle around the recognized face and count the matches recognized_count = 0 for (top, right, bottom, left) in unknown_face_locations: cv2.rectangle(unknown_image, (left, top), (right, bottom), (0, 0, 255), 2) results = face_recognition.compare_faces([known_face_encoding], unknown_face_encodings[recognized_count]) if results[0]: recognized_count += 1 print(recognized_count) # Display the image with the rectangle cv2.imshow(window_name, unknown_image) cv2.waitKey(0) cv2.destroyAllWindows()