在通过blog尝试通过python通过OpenCV运行视频列表时,我可以运行网络摄像头,但是我试图对无法运行的摄像机列表运行它,
import threading
import cv2
import time
class VideoCaptureAsync:
def __init__(self, src=0, width=640, height=480):
self.src = src
self.cap = cv2.VideoCapture(self.src)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self.grabbed, self.frame = self.cap.read()
self.started = False
self.read_lock = threading.Lock()
def set(self, var1, var2):
self.cap.set(var1, var2)
def start(self):
if self.started:
print('[!] Asynchroneous video capturing has already been started.')
return None
self.started = True
self.thread = threading.Thread(target=self.update, args=())
self.thread.start()
return self
def update(self):
while self.started:
grabbed, frame = self.cap.read()
with self.read_lock:
self.grabbed = grabbed
self.frame = frame
def read(self):
with self.read_lock:
frame = self.frame.copy()
grabbed = self.grabbed
return grabbed, frame
def stop(self):
self.started = False
self.thread.join()
def __exit__(self, exec_type, exc_value, traceback):
self.cap.release()
def test(n_frames=500, width=1280, height=720, async=False):
if async:
cap = VideoCaptureAsync(0)
else:
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
if async:
cap.start()
t0 = time.time()
i = 0
while i < n_frames:
_, frame = cap.read()
cv2.imshow('Frame', frame)
cv2.waitKey(1) & 0xFF
i += 1
print('[i] Frames per second: {:.2f}, async={}'.format(n_frames / (time.time() - t0), async))
if async:
cap.stop()
cv2.destroyAllWindows()
if __name__ == '__main__':
test(n_frames=500, width=1280, height=720, async=False)
test(n_frames=500, width=1280, height=720, async=True)
我想针对多个IP摄像机运行它
为多个IP摄像机运行此代码的任何建议将非常有帮助
最佳答案
您需要在VideoCaptureAsync
函数中多次启动test
。此外,您不需要多次设置高度和宽度,因为您定义的类已经将其作为参数。我对测试功能做了一些修改,以显示一个示例,用于同时运行两个网络摄像头:
def test(n_frames=500, width=1280, height=720, async_flag=False):
if async_flag:
cap = VideoCaptureAsync(src=0, width=width, height=height) #<----Change src to ip camera
cap1 = VideoCaptureAsync(src=1, width=width, height=height) #<----Change src to ip camera 2
else:
cap = cv2.VideoCapture(0)
if async_flag:
cap.start()
cap1.start()
t0 = time.time()
i = 0
while i < n_frames:
_, frame = cap.read()
_, frame1 = cap1.read()
cv2.imshow('Frame', frame)
cv2.imshow('Frame 1', frame1)
cv2.waitKey(1) & 0xFF
i += 1
print('[i] Frames per second: {:.2f}, async={}'.format(n_frames / (time.time() - t0), async_flag))
if async_flag:
cap.stop()
cap1.stop()
cv2.destroyAllWindows()
if __name__ == '__main__':
# test(n_frames=500, width=1280, height=720, async_flag=False)
test(n_frames=500, width=1280, height=720, async_flag=True)
关于python - 在python中使用opencv流式传输的视频的异步列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60501795/