有人知道如何使用视频编写器吗?我有一个简单的脚本,可以使用笔记本电脑网络摄像头通过haar_cascades检测面部,我试图弄清楚如何将检测到的面部周围的框写入输出到视频文件中。用于haar级联的.xml文件保存到目录中,并且程序以循环格式运行。我在下面评论的是#cv2.VideoWriter.write(frame)
,它不起作用,因为视频编写器不希望使用numpy数组。
import numpy as np
import cv2, time
import matplotlib.pyplot as plt
haar_face_cascade = cv2.CascadeClassifier('opencv-master/opencv-master/data/haarcascades/haarcascade_frontalface_alt.xml')
video = cv2.VideoCapture(0)
a = 0
while True:
a = a + 1
check, frame = video.read()
print(check)
print(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = haar_face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5);
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.imshow("Face Detector", frame)
#cv2.VideoWriter.write(frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
print(a)
video.release()
cv2.destroyAllWindows()
答案 0 :(得分:0)
所以我做了一些研究并弄清楚了... https://docs.opencv.org/3.1.0/dd/d43/tutorial_py_video_display.html
import numpy as np
import cv2
haar_face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml')
video = cv2.VideoCapture(0)
a = 0
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while True:
a = a + 1
check, frame = video.read()
print(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = haar_face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5);
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
out.write(frame)
cv2.imshow("Face Detector", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
print(a)
video.release()
out.release()
cv2.destroyAllWindows()