Skip to content

Instantly share code, notes, and snippets.

@softdream1981
Created December 2, 2022 14:10
Show Gist options
  • Select an option

  • Save softdream1981/7fe3a56ec2237cccd07f7896b281b345 to your computer and use it in GitHub Desktop.

Select an option

Save softdream1981/7fe3a56ec2237cccd07f7896b281b345 to your computer and use it in GitHub Desktop.
face-unique
import cv2
import face_recognition
# Open the video file
video = cv2.VideoCapture("video.mp4")
# Create an empty list to store the extracted faces
faces = []
# Loop over the frames of the video
while True:
# Grab the current frame
ret, frame = video.read()
# If we are at the end of the video, break the loop
if not ret:
break
# Convert the frame to RGB
rgb_frame = frame[:, :, ::-1]
# Use the face_recognition library to find all the faces in the frame
face_locations = face_recognition.face_locations(rgb_frame)
# Loop over the face locations
for face_location in face_locations:
# Extract the face from the frame and add it to the list of faces
face = rgb_frame[face_location[0]:face_location[2], face_location[3]:face_location[1]]
faces.append(face)
# Convert the list of faces to a NumPy array
faces = np.array(faces)
# Save the array of faces to a file
np.save("faces.npy", faces)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment