From 7a6ce35ef2557f667b50b8c7684f51272e33e91d Mon Sep 17 00:00:00 2001 From: Naufalakram Date: Fri, 29 Sep 2023 18:06:50 +0800 Subject: [PATCH] added app3.py and coba.py --- app.py | 0 app2.py | 2 +- app3.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ coba.py | 19 +++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) mode change 100644 => 100755 app.py create mode 100755 app3.py create mode 100755 coba.py diff --git a/app.py b/app.py old mode 100644 new mode 100755 diff --git a/app2.py b/app2.py index e51df36..fa295c1 100755 --- a/app2.py +++ b/app2.py @@ -2,7 +2,7 @@ import face_recognition import cv2 import os -image_load = 'image1.jpg' +image_load = 'image2.jpg' image_data_folder = 'faces/' # Load the images in the image_data_folder and save their face encodings in a list diff --git a/app3.py b/app3.py new file mode 100755 index 0000000..a4bde28 --- /dev/null +++ b/app3.py @@ -0,0 +1,45 @@ +import face_recognition +import cv2 +import os + +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)) + +# Initialize the webcam +video_capture = cv2.VideoCapture(-1) + +while True: + # Capture a single frame from the webcam + ret, frame = video_capture.read() + + # Find the face locations and encodings in the frame + face_locations = face_recognition.face_locations(frame) + face_encodings = face_recognition.face_encodings(frame, face_locations) + + # Compare the face encodings in the frame to the face encodings in image_dataset + for (top, right, bottom, left), face_encoding in zip(face_locations, 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(frame, (left, top), (right, bottom), (0, 0, 255), 2) + cv2.putText(frame, name, (right, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) + + # Display the resulting image + cv2.imshow('Video', frame) + + # Exit the loop if the 'q' key is pressed + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +# Release the webcam and close all windows +video_capture.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/coba.py b/coba.py new file mode 100755 index 0000000..fd75d45 --- /dev/null +++ b/coba.py @@ -0,0 +1,19 @@ +import cv2 + +# Initialize the webcam +video_capture = cv2.VideoCapture(-1) + +while True: + # Capture a single frame from the webcam + ret, frame = video_capture.read() + + # Display the resulting image + cv2.imshow('Video', frame) + + # Exit the loop if the 'q' key is pressed + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +# Release the webcam and close all windows +video_capture.release() +cv2.destroyAllWindows() \ No newline at end of file