gpt4 book ai didi

python - 在检测到人脸时捕获屏幕截图

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:49 25 4
gpt4 key购买 nike

我必须制作一个代码,当在视频中检测到人脸时捕获屏幕截图,以便该图像可用于图像识别数据集

我制作了一个捕捉所有帧的程序,但我需要让它只在检测到人脸时捕捉

import cv2
cap = cv2.VideoCapture('test.mp4')
count = 0
while cap.isOpened():
ret,frame = cap.read()
cv2.imshow('window-name',frame)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.imwrite("frame%d.jpg" % count, frame)
count = count + 1
if cv2.waitKey(10) & 0xFF == ord('q'):
break


cap.release()
cv2.destroyAllWindows() # destroy all the opened windows

最佳答案

我刚试过你的代码,它有 1 个小错误。您没有正确指定 XML 分类器路径。我使用 XML 文件所在的完整路径修复了路径并成功运行。

import cv2
import numpy as np

cap = cv2.VideoCapture('test.mp4')
#cap = cv2.VideoCapture(0) # I tried using webcam and works
count = 0
while cap.isOpened():
ret,frame = cap.read()
cv2.imshow('window-name',frame)
# Below you have to insert the full path of XML file, below is mine
face_cascade = cv2.CascadeClassifier('C:/ProgramData/Anaconda2/pkgs/opencv-3.2.0-np111py27_0/Library/etc/haarcascades/haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.imwrite("frame%d.jpg" % count, frame)
count = count + 1
if cv2.waitKey(10) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows() # destroy all the opened windows

关于python - 在检测到人脸时捕获屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55584975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com