我有多个OpenCV Videocapture来源,并且希望将其分别加载到不同的线程中。现在,图像和电影的加载很好,但是网络摄像头却给了我空的图像。仅当我在线程中执行此操作时才会发生这种情况。在我使用相同代码而没有线程的情况下,我从网络摄像头接收图像。 OpenCV给我以下错误:
[ERROR:0] VIDEOIO(makePtr<VideoCapture_DShow>(index)): raised unknown C++ exception!
有人可以帮我解决这个问题吗?
class render(threading.Thread):
def __init__ (self, _scene):
threading.Thread.__init__(self)
self.scene = _scene
def renderWebcam(self,src):
camera = cv2.VideoCapture(src+cv2.CAP_DSHOW)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,720)
while True:
try:
rc,image = camera.read()
img = cv2.resize(image,(640,360))
cv2.imwrite('renderfiles/Totaal.png', img)
crop = image[100:460, 100:740]
img = cv2.resize(crop,(640,360))
cv2.imwrite('renderfiles/Ingezoomd.png', img)
except KeyboardInterrupt:
break;
camera.release();
cv2.destroyAllWindows()
我在Windows 10和Logitech网络摄像头上使用python 2.7
更新: 发生此错误的最小设置是:
from threading import Thread
import cv2
def renderWebcam(src,name):
camera = cv2.VideoCapture(src+cv2.CAP_DSHOW)
while True:
try:
rc,image = camera.read()
img = cv2.resize(image,(640,360))
cv2.imwrite('renderfiles/'+name+'.png', img)
except KeyboardInterrupt:
break;
camera.release();
cv2.destroyAllWindows()
thread = Thread(target=renderWebcam, args=(0,'Webcam1'))
thread.start()
thread = Thread(target=renderWebcam, args=(1,'Webcam2'))
thread.start()