Python-具有功能的多重处理

时间:2019-10-03 08:48:12

标签: python multiprocessing python-multiprocessing python-multithreading

我对Python和计算机视觉相当陌生,但是我设法设置了一个基本脚本来在家打开CCTV摄像机。效果很好,但我有一个小问题是,它一个接一个地循环遍历每个摄像机,因此,代替每秒更新一次摄像机图像,它大约每5秒钟更新一次-完成一个循环所花费的时间。

我想要的是能够使用多处理/多线程,以便当我为我的每个摄像机调用该函数时,它会打开一个新的池,彼此并行运行,因此每秒更新每个摄像机。

您可以在下面的代码中看到,我使用相同的功能(但参数不同)调用每个摄像机。我已经阅读了Process,并尝试了一些变体,但似乎无法正常工作。

我敢肯定,这很简单,如果有人可以将我指向正确的方向,那将是很好的选择。

代码如下:

# import libraries
from threading import Thread
import imutils
import cv2, time
import argparse
import numpy as np
import datetime
from imutils.video import WebcamVideoStream
from multiprocessing import Process


stream = WebcamVideoStream('rtsp://mylink1./' ).start() # To open any valid video file stream/network stream. In this case 'test.mp4' file.
stream2 = WebcamVideoStream('rtsp://mylink2./' ).start()
stream3 = WebcamVideoStream('rtsp://mylink3./' ).start()
stream4 = WebcamVideoStream('rtsp://mylink4./' ).start()
stream5 = WebcamVideoStream('rtsp://mylink5./' ).start()


def checkimage(stream,camname):

    global lastmessage

    try:
        frame = stream.read()

        (h, w) = frame.shape[:2]

        cv2.imshow(camname, frame)

        print('[INFO]Checked ' + str(camname) + ' at ' +  datetime.datetime.now().strftime("%H:%M:%S")  + '...')

    except AttributeError:
        pass

# infinite loop
while True:

    checkimage(stream,"Back Door Camera")
    checkimage(stream2,"Conservatory Camera")
    checkimage(stream3,"Front Door Camera")
    checkimage(stream4,"Garage Camera")
    checkimage(stream5,"Shed Camera")

    key = cv2.waitKey(1) & 0xFF

    # check for 'q' key-press
    if key == ord("q"):
        if 'q' key-pressed break out
        break

cv2.destroyAllWindows()
# close output window

stream.stop()
# safely close video stream.

提前谢谢! 克里斯

0 个答案:

没有答案