从 youtube 视频中提取独特的面孔

问题描述 投票:0回答:0

我得到以下错误

Saved 0 unique faces
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024f505224c0] moov atom not found

尝试使用下面的代码从 YouTube 视频中提取独特的面孔,该代码旨在下载 YouTube 视频并将独特的面孔提取到名为 faces 的文件夹中。我有一个空的视频和文件夹。请检查下面的 Python 代码

import os
import urllib.request
import cv2
import face_recognition
import numpy as np

# Step 1: Download the YouTube video
video_url = "https://www.youtube.com/watch?v=JriaiYZZhbY&t=4s"
urllib.request.urlretrieve(video_url, "video.mp4")

# Step 2: Extract frames from the video
cap = cv2.VideoCapture("video.mp4")
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frames = []
for i in range(frame_count):
    cap.set(cv2.CAP_PROP_POS_FRAMES, i)
    ret, frame = cap.read()
    if ret:
        frames.append(frame)
cap.release()

# Step 3: Detect faces in the frames
detected_faces = []
for i, frame in enumerate(frames):
    face_locations = face_recognition.face_locations(frame)
    for j, location in enumerate(face_locations):
        top, right, bottom, left = location
        face_image = frame[top:bottom, left:right]
        cv2.imwrite(f"detected_{i}_{j}.jpg", face_image)
        detected_faces.append(face_image)

# Step 4: Save the faces as separate images
if not os.path.exists("faces"):
    os.makedirs("faces")
known_faces = []
for i in range(len(detected_faces)):
    face_image = detected_faces[i]
    face_encoding = face_recognition.face_encodings(face_image)[0]
    known_faces.append(face_encoding)
    cv2.imwrite(f"faces/face_{i}.jpg", face_image)
print("Saved", len(known_faces), "unique faces")
python numpy opencv urllib face-recognition
© www.soinside.com 2019 - 2024. All rights reserved.