import cv2
import os
cam = cv2.VideoCapture(r"C:/Users/User/Desktop/aayfryxljh.mp4")
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
result = cv2.VideoWriter('C:/Users/User/Desktop/new.mp4',cv2.VideoWriter_fourcc(*'mp4v'),30,(112,112))
while (True):
# reading from frame
ret, frame = cam.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
size=(frame.shape[1],frame.shape[0])
c = cv2.waitKey(1)
# if video is still left continue creating images
for (x, y, w, h) in faces:
cropped = frame[y: y + h, x: x + w]
cv2.imshow('frame', cropped)
result.write(cropped)
# Release all space and windows once donecam.release()
result.release()
应该保存裁剪的人脸视频。我想将其保存为.mp4格式。它只是显示了一个空的.mp4文件,我无法理解这个问题。代码执行时没有任何错误
1条答案
按热度按时间4nkexdtk1#
正如我在注解中提到的,while循环永远不会结束,因此
result.release()
永远不会被调用。看起来代码需要一种方法来结束while循环。也许:请参阅以下位置的示例:https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#saving-a-video