diff --git a/.DS_Store b/.DS_Store new file mode 100755 index 0000000..89c8aa1 Binary files /dev/null and b/.DS_Store differ diff --git a/._.DS_Store b/._.DS_Store new file mode 100755 index 0000000..4be07d1 Binary files /dev/null and b/._.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..a5fd33c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# env folder +env/ +faces/ \ No newline at end of file diff --git a/app.py b/app.py index 63b5a1b..2695cfa 100644 --- a/app.py +++ b/app.py @@ -2,42 +2,31 @@ import face_recognition import cv2 import os -image_to_scan = 'image1.jpg' -image_dataset_folder = 'faces' +image_load = 'image1.jpg' +# image_data = 'faces/rika.png' +image_data = 'faces/rika.png' -# Load the image to scan -image = face_recognition.load_image_file(image_to_scan) +# Load the images +image_load_path = os.path.join(os.getcwd(), image_load) +image_data_path = os.path.join(os.getcwd(), image_data) +image_load = face_recognition.load_image_file(image_load_path) +image_data = face_recognition.load_image_file(image_data_path) -# Detect the faces in the image -face_locations = face_recognition.face_locations(image) +# Find the face locations and encodings in each image +image_load_face_locations = face_recognition.face_locations(image_load) +image_load_face_encodings = face_recognition.face_encodings(image_load, image_load_face_locations) +image_data_face_locations = face_recognition.face_locations(image_data) +image_data_face_encodings = face_recognition.face_encodings(image_data, image_data_face_locations) -# Draw a rectangle around each detected face using OpenCV -for (top, right, bottom, left) in face_locations: - cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2) +# Compare the face encodings +matches = face_recognition.compare_faces(image_data_face_encodings, image_load_face_encodings[0]) -# Display the image with the detected faces in a window -cv2.imshow('Faces', image) +# Draw boxes around the faces in the image_load image +for (top, right, bottom, left), match in zip(image_load_face_locations, matches): + if match: + cv2.rectangle(image_load, (left, top), (right, bottom), (0, 0, 255), 2) + +# Display the image with the boxes around the matching faces +cv2.imshow('Image', image_load) cv2.waitKey(0) - -# Load the images from the 'faces' folder and store them in a list -images = [] -for filename in os.listdir(image_dataset_folder): - # if the file name ends with '.jpg' or '.png' - if filename.endswith('.jpg') or filename.endswith('.png'): - images.append(face_recognition.load_image_file(os.path.join(image_dataset_folder, filename))) - -# Loop through the list of images and detect the faces in each image using face_recognition -for image in images: - # Detect the faces in the image - face_locations = face_recognition.face_locations(image) - - # Draw a rectangle around each detected face using OpenCV - for (top, right, bottom, left) in face_locations: - cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2) - - # Display the images with the detected faces in a window - cv2.imshow('Faces', image) - cv2.waitKey(0) - -# Wait for the user to press a key to exit the program cv2.destroyAllWindows() \ No newline at end of file diff --git a/app2.py b/app2.py new file mode 100755 index 0000000..e51df36 --- /dev/null +++ b/app2.py @@ -0,0 +1,35 @@ +import face_recognition +import cv2 +import os + +image_load = 'image1.jpg' +image_data_folder = 'faces/' + +# Load the images in the image_data_folder and save their face encodings in a list +image_dataset = [] +for filename in os.listdir(image_data_folder): + image_data_path = os.path.join(os.getcwd(), image_data_folder, filename) + image_data = face_recognition.load_image_file(image_data_path) + image_data_encoding = face_recognition.face_encodings(image_data)[0] + image_dataset.append((filename, image_data_encoding)) + +# Load the image_load and find the face locations and encodings +image_load_path = os.path.join(os.getcwd(), image_load) +image_load = face_recognition.load_image_file(image_load_path) +image_load_face_locations = face_recognition.face_locations(image_load) +image_load_face_encodings = face_recognition.face_encodings(image_load, image_load_face_locations) + +# Compare the face encodings in image_load to the face encodings in image_dataset +for (top, right, bottom, left), face_encoding in zip(image_load_face_locations, image_load_face_encodings): + matches = face_recognition.compare_faces([x[1] for x in image_dataset], face_encoding, tolerance=0.55) + if True in matches: + # Find the name of the matching face + name = image_dataset[matches.index(True)][0] + # Draw a box around the face and show the name on the top right of the box + cv2.rectangle(image_load, (left, top), (right, bottom), (0, 0, 255), 2) + cv2.putText(image_load, name, (right, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) + +# Display the image with the boxes and names around the matching faces +cv2.imshow('Image', image_load) +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/image1.jpg b/image1.jpg new file mode 100755 index 0000000..87dfb2a Binary files /dev/null and b/image1.jpg differ diff --git a/image2.jpg b/image2.jpg new file mode 100755 index 0000000..39fa8f7 Binary files /dev/null and b/image2.jpg differ diff --git a/image3.jpg b/image3.jpg new file mode 100755 index 0000000..2046ef1 Binary files /dev/null and b/image3.jpg differ