我目前正在使用opencv和python来运行两个网络摄像头。这些网络摄像头将分别拍照。我想在不同的线程上运行它们,以便尽可能地同步图片。我正在使用videocapture :: grab()和videocapture :: retreive(),但我不确定我是否正确使用它们,因为当我注释掉抓取功能时,它似乎可以自行检索。 以下是我的代码。
import threading
import time
import sys
import cv2
from multiprocessing.pool import ThreadPool
count = 0
video_capture1 = cv2.VideoCapture(1)
video_capture2 = cv2.VideoCapture(2)
#camera1
class myThread1(threading.Thread):
def __init__(self, threadID, name, device):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print "Starting " + self.name
# Get lock to synchronize threads
# threadLock.acquire()
camCapture(self.name, 1)
# Free lock to release next thread
# threadLock.release()
def camCapture(threadName, device):
if video_capture1.grab():
print "frame grabbed1"
#camera2
class myThread2 (threading.Thread):
def __init__(self, threadID, name, device):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print "Starting " + self.name
# Get lock to synchronize threads
# threadLock.acquire()
camCapture2(self.name, 2)
# Free lock to release next thread
# threadLock.release()
def camCapture2(threadName, device):
if video_capture2.grab():
print "frame grabbed2"
# Create new threads
thread1 = myThread1(1, "Thread-1", count)
thread2 = myThread2(2, "Thread-2", count)
# Start new Threads
thread1.start()
thread2.start()
ret, image1 = video_capture1.retrieve()
ret, image2 = video_capture2.retrieve()
cv2.imwrite("C:\Users\Me\Desktop\camOne" + str(count) + ".bmp", image1)
cv2.imwrite("C:\Users\Me\Desktop\camTwo" + str(count) + ".bmp", image2)
我是否使用抓取和检索命令的正确语法?我只想在测试这些相机之前确定一下。
我也将这些照片保存为Bitmap图像,因为我希望它们尽可能接近原始格式。这是我最好的选择吗?