[英]Python 3 OpenCV unable to record and save video
我正在使用 cv2 版本 4.2.0,我正在尝试录制和保存视频,但出现错误。
编码
import numpy as np
import os
import cv2
filename = 'myvid.avi'
frames_per_second = 24.0
res = '720p'
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
STD_DIMENSIONS = {
"480p": (640, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
def get_dims(cap, res='1080p'):
width, height = STD_DIMENSIONS["480p"]
if res in STD_DIMENSIONS:
width,height = STD_DIMENSIONS[res]
## change the current caputre device
## to the resulting resolution
change_res(cap, width, height)
return width, height
VIDEO_TYPE = {
'avi': cv2.VideoWriter_fourcc(*'XVID'),
#'mp4': cv2.VideoWriter_fourcc(*'H264'),
'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}
def get_video_type(filename):
filename, ext = os.path.splitext(filename)
if ext in VIDEO_TYPE:
return VIDEO_TYPE[ext]
return VIDEO_TYPE['avi']
cap = cv2.VideoCapture(0)
out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))
while True:
ret, frame = cap.read()
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
错误
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback
我尝试按照其他答案的指示添加完整目录,但也失败了,并向我显示了相同的错误。 我正在关注视频教程,相同的代码似乎对教师来说效果很好。
您正在使用cv2.VideoCapture(0)
从相机捕获视频,代码可能无法正常工作,因为您没有连接的相机。
如果您确实有连接的相机,则代码可能因其他原因无法工作。
如果您没有相机,您可以下载并安装虚拟相机应用程序(例如捕获显示)。
我建议您先尝试从文件中读取视频。
下面的代码示例:
这是代码:
import numpy as np
import cv2
import os
intput_filename = 'input_vid.avi'
# Generate synthetic video to be used as input:
###############################################################################
width = 640
height = 480
n_frames = 250
# Use motion JPEG codec (for testing)
synthetic_out = cv2.VideoWriter(intput_filename, cv2.VideoWriter_fourcc(*'MJPG'), 25, (width, height))
for i in range(n_frames):
img = np.full((height, width, 3), 60, np.uint8)
cv2.putText(img, str(i), (width//2-100*len(str(i)), height//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (30, 255, 30), 20)
#cv2.imshow('img',img)
#cv2.waitKey(100)
synthetic_out.write(img)
synthetic_out.release()
###############################################################################
filename = 'myvid.avi'
frames_per_second = 24.0
res = '720p'
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
STD_DIMENSIONS = {
"480p": (640, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
def get_dims(cap, res='1080p'):
width, height = STD_DIMENSIONS["480p"]
if res in STD_DIMENSIONS:
width,height = STD_DIMENSIONS[res]
## change the current capture device
## to the resulting resolution
change_res(cap, width, height)
return width, height
VIDEO_TYPE = {
'avi': cv2.VideoWriter_fourcc(*'XVID'),
#'mp4': cv2.VideoWriter_fourcc(*'H264'),
'mp4': cv2.VideoWriter_fourcc(*'mp4v'),
}
def get_video_type(filename):
filename, ext = os.path.splitext(filename)
if ext in VIDEO_TYPE:
return VIDEO_TYPE[ext]
return VIDEO_TYPE['avi']
# Read video from file instead of from camera.
cap = cv2.VideoCapture(intput_filename)
#cap = cv2.VideoCapture(0)
fourcc = get_video_type(filename)
# Get resolution of input video
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))
# The second argument of VideoWriter is FOURCC code
# Set the size of the output video to be same as the input (get_dims is not working).
out = cv2.VideoWriter(filename, fourcc, frames_per_second, (width, height))
while True:
ret, frame = cap.read()
if not ret:
# Break loop if ret is False
break;
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
请让我知道该示例是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.