0

I am fairly new to Python and Computer Vision but I've managed to setup a basic script to open my CCTV camera's at home. It works great but I have a slight issue is is that it loops over each camera one after the other, so instead of the camera image updating every second, it updates around every 5 seconds - the time it takes to complete a loop.

What I want is to be able to use multiprocessing / multi threading so that when I call the function for each of my cameras it open ins a new pool, running parallel to each other, therefore updating each camera every second.

As you can see in the below code I call each of the cameras using the same function, but with different argurments. I've read up on Process and have tried a few variations but I don't seem to be able to get it working.

I'm sure its a simple one and it would be great if someone could point me in the right direction.

Here's the code:

# 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.

Thanks in advance! Chris

Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
Walshie1987
  • 443
  • 2
  • 6
  • 19
  • Possible duplicate of [Python Multiprocessing with a single function](https://stackoverflow.com/questions/21959355/python-multiprocessing-with-a-single-function) – muru Oct 03 '19 at 09:22
  • [`WebcamVideoStream`](https://github.com/jrosebr1/imutils/blob/master/imutils/video/webcamvideostream.py) already seems to use multiple threads for reading frames. are you sure it's only running around the loop every 5 seconds, or just getting the same frame 5 times in a row? maybe insert some print or timing statements to figure out what it's doing – Sam Mason Oct 05 '19 at 10:52

0 Answers0