【问题标题】:How to stream videos from multiple IP cameras using Python如何使用 Python 从多个 IP 摄像机流式传输视频
【发布时间】:2017-12-04 10:11:23
【问题描述】:
url='http://192.168.0.103:8080/shot.jpg'
url2='http://192.168.0.102:8080/shot.jpg'
url3='http://192.168.0.3:8080/shot.jpg'

while True:
 imgResp=urllib.urlopen(url)
 imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
 img=cv2.imdecode(imgNp,-1)

 imgResp2=urllib.urlopen(url2)
 imgNp2=np.array(bytearray(imgResp2.read()),dtype=np.uint8)
 img2=cv2.imdecode(imgNp2,-1)

 imgResp3=urllib.urlopen(url3)
 imgNp3=np.array(bytearray(imgResp3.read()),dtype=np.uint8)
 img3=cv2.imdecode(imgNp3,-1)

 cv2.imshow('IPWebcam',img)
 cv2.imshow('IPWebcam2',img2)
 cv2.imshow('IPWebcam3',img3)
 if cv2.waitKey(1) & 0xFF == ord('q'):
    break   

这是我正在运行的代码。但结果滞后。我想要一种更快的方式来一次读取多个视频流。

【问题讨论】:

  • 您的 URL 是否经常刷新以获得新图像,还是始终是静态图像?
  • 只要我只连接一个 IP 摄像头,我就能获得良好的视频输出而没有任何延迟。但每当我连接多个摄像头时,生成的视频就会滞后。
  • 你有没有试过用一个线程来实现它,所有的凸轮都在一个单独的线程中运行

标签: python opencv video-streaming video-processing


【解决方案1】:

试试这个:

def ShowCam(url,CameraNumber =1):
    imgResp = urllib.urlopen(url)
    imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
    img = cv2.imdecode(imgNp, -1)
    camName = 'IPWebcam' + str(CameraNumber)
    return camName,img
result = True


from multiprocessing.pool import ThreadPool

# do some other stuff in the main process

while result:
    pool = ThreadPool(processes=3)
    async_result1 = pool.apply_async(ShowCam, (url, 1))  # tuple of args for     foo
    rval1 = async_result1.get()
    cv2.imshow(rval1[0],rval1[1])
    async_result2 = pool.apply_async(ShowCam, (url2, 2))  # tuple of args for foo
    rval2 = async_result2.get()
    cv2.imshow(rval2[0],rval2[1])
    async_result3 = pool.apply_async(ShowCam, (url3, 3))  # tuple of args for foo
    rval3 = async_result3.get()
    cv2.imshow(rval3[0],rval3[1])
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

注意:代码未经测试,请忽略任何拼写错误,但这应该可以解决目的。快乐编码:)

【讨论】:

  • 代码工作了一段时间,然后出现以下错误。 Traceback(最近一次调用最后一次):文件“stack.py”,第 21 行,在 pool = ThreadPool(processes=2) File "W:\Tools\Python27\lib\multiprocessing\pool.py",第 732 行,在 init Pool.__init__(self, processes, initializer, initargs)文件“W:\Tools\Python27\lib\multiprocessing\pool.py”,第 187 行,在 init self._result_handler.start() 文件“W:\Tools\Python27\lib\threading. py",第 736 行,在 start _start_new_thread(self.__bootstrap, ()) thread.error: can't start new thread
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多