现在有一个收集人脸的需求,每一张照片只有一个人。我的解决办法是用收集录制一段视频,然后上传到PC上。在PC上使用OpenCV将图片中的每一帧保存为JPG图片。
以下是代码:
import time
import cv2
if __name__ == '__main__':
# 填写视频的绝对路径
vidcap = cv2.VideoCapture('/home/shushi/video/猎场.mp4')
success, image = vidcap.read()
start_time = time.time()
print(start_time)
while success:
end_time = time.time()
file_name = str(end_time).replace('.', '')
# 每隔三秒截屏
if 3 == int(end_time - start_time):
start_time = end_time
# 保存JGP 的绝对路径
cv2.imwrite('/home/shushi/video/' + file_name + ".jpg", image) # save frame as JPEG file
success, image = vidcap.read()
if cv2.waitKey(10) == 27: # exit if Escape is hit
break
将视频放到与Python文件同级目录下,然后运行程序,你就会得到视频帧对应的JPG图片了: