【发布时间】:2018-03-04 23:33:42
【问题描述】:
我已经编写了下面的代码来从相机读取视频以显示和保存。
当我在 VideoCapture(0) 中使用选项 0 运行以下代码时,它可以正常工作并显示我的网络摄像头视频,当我在 VideoCapture(1) 中将其更改为 1 以从第三方摄像头获取视频时,我得到错误。
我正在使用 3rd 方相机,通过他们的软件播放视频,我需要使用我的 python 代码进行捕捉..
使用apbase code qt 示例也可以播放视频
我无法使用以下 python 代码玩游戏
import cv2
import numpy as np
import time
def nothing(x):
pass
cv2.namedWindow('images')
switch = 'Recording'
cv2.createTrackbar(switch, 'images',0,1,nothing)
cap = cv2.VideoCapture(0)
def writeVideo(frmae):
pass
switchstatus = 0
currentpos = 0
fourccs = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('sample.avi', fourccs, 20.0, (640,480))
created = 0
startrecord = 0
def RecordVideo(frame):
global out
global created;
global startrecord
print "In the Record video" ,created, startrecord
if created == 0 and startrecord ==1:
filename ='test.avi'
filename=time.strftime("%Y-%m-%d-%H-%M-%S")+'.avi'
print "filename", filename
out = cv2.VideoWriter(filename,fourccs, 20.0, (640,480))
created = 1;
out.write(frame)
elif created == 1 and startrecord ==1:
out.write(frame)
def positionChanged(s):
global currentpos
global created
global startrecord
print "position changed", s
currentpos = s
if s==1:
startrecord = 1
created = 0
else:
startrecord = 0
if created==1:
created =0
out.release()
def switchchanged(s):
global switchstatus;
if switchstatus != s:
switchstatus = s
positionChanged(s)
while(1):
ret, frame = cap.read()
RecordVideo(frame)
cv2.imshow('images',frame)
s = cv2.getTrackbarPos(switch,'images')
switchchanged(s)
if cv2.waitKey(1) & 0xFF == ord('q'):
out.release()
cv2.destroyAllWindows()
break
错误
文件“C:\Python32Bit\video.py,lime 89, in cv2.imshow('images',frame)
错误:........\opencv\modules\hihggui\src\window.cpp:error:(-215) size.width>0 && size.height>0 in function cv::imshow
【问题讨论】:
-
在 Linux 系统上
VideoCapture默认使用libv4l2用于视频输入设备。安装v4l-utils并执行v4l2-ctl --all。看看它是否显示你的相机。 -
我在 windows 上试试
-
对于 windows,它很可能使用
DShow。使用您的显示网络摄像头,运行cap = cv2.VideoCapture(0+cv2.CAP_DSHOW),如果它工作,那么它正在使用DSHOW。 -
0+cv2.CAP_DSHOW 正在为我的网络摄像头工作,而 1+cv2.CAP_DSHOW 给出了同样的错误。出于测试目的,我交叉检查,安装了该相机“aptina m031”的驱动程序
-
好的,让我们再试一次。
cap = cv2.VideoCapture(0+cv2.CAP_FFMPEG)。顺便说一句,你是从源代码还是从 opencv_python 绑定安装 OpenCV 的?