简体   繁体   English

如何使用Python从多个IP摄像机流式传输视频

[英]How to stream videos from multiple IP cameras using Python

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   

this is the code i am running.but the result is lagging. 这是我正在运行的代码。但是结果是滞后的。 i want a faster way to read multiple video streams at a time. 我想要一次更快的方式一次读取多个视频流。

Try this out : 试试看:

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

Note : Code is not tested , please ignore any typos , but this should solve the purpose . 注意:代码未经测试,请忽略任何拼写错误,但这应该可以解决目的。 Happy coding :) 快乐的编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM