Step 1: Install Required Libraries
First, you'll need to install the necessary libraries. You can do this using pip:
bashpip install opencv-python dlib face_recognition numpy
Step 2: Import Libraries
Next, import the necessary libraries in your Python script.
pythonimport cv2
import face_recognition
import numpy as np
Step 3: Load and Encode Faces
Load images of the faces you want to recognize and encode them. Encoding converts the facial features into a numerical representation that can be compared with other faces.
python# Load a sample picture and learn how to recognize it.
image_of_person1 = face_recognition.load_image_file("person1.jpg")
person1_encoding = face_recognition.face_encodings(image_of_person1)[0]
# Load a second sample picture and learn how to recognize it.
image_of_person2 = face_recognition.load_image_file("person2.jpg")
person2_encoding = face_recognition.face_encodings(image_of_person2)[0]
# Create arrays of known face encodings and their names
known_face_encodings = [
person1_encoding,
person2_encoding
]
known_face_names = [
"Person 1",
"Person 2"
]
Step 4: Initialize Video Capture
Set up video capture to use your webcam or a video file.
pythonvideo_capture = cv2.VideoCapture(0) # Use 0 for webcam, or replace with a video file path
Step 5: Recognize Faces in Video Stream
Process each frame from the video capture to recognize faces.
pythonwhile True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
# Loop through each face found in the frame
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# See if the face is a match for the known faces
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# If a match was found in known_face_encodings, use the first one.
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
Explanation:
- Load and Encode Faces: Load images of the people you want to recognize and encode their faces.
- Initialize Video Capture: Start capturing video from your webcam.
- Recognize Faces in Video Stream: For each frame, find faces, encode them, compare with known faces, and display the results.
If you like this then please give feedback.
0 Comments