我已經寫下面的代碼從相機讀取視頻來顯示和保存。蟒蛇opencv無法顯示視頻,而使用第三方相機
當我使用VideoCapture(0)中的選項0運行下面的代碼時,它工作正常並顯示我的網絡攝像頭視頻,當我在VideoCapture(1)中將其更改爲1以獲取來自第三方相機的視頻時,錯誤。
我使用的是第三方的攝像頭,與他們的軟件它播放視頻,我需要用我的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,石灰89,在cv2.imshow( 'images',frame)
eror:........ \ opencv \ modules \ hihggui \ src \ window.cpp:error:( - 215)size.width> 0 & & size.height> 0 in cv :: imshow
在Linux系統上'VideoCapture'默認使用'libv4l2'作爲視頻輸入設備。安裝'v4l-utils'並執行'v4l2-ctl --all'。看看它是否顯示你的相機。 – zindarod
我想在windows上 – user3607698
對於windows,它最可能使用DShow。用你的顯示攝像頭,運行'cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)',如果它有效,那麼它使用'DSHOW'。 – zindarod