Ask Your Question

Revision history [back]

Run openCV application concurrently to main program

I've written an application with OpenCV and python 3.6 which I'd now like to integrate within my main program. My main application loops at approximately twice per second whist the openCV should runs at 24 fps. The two programs only share one variable.

What's the best way to do this? Should I use multi-processing, multi-threading or just implement it as another loop in my my program?

Thanks in advance, Charles

Run openCV application concurrently to main program

I've written an application with OpenCV and python 3.6 which I'd now like to integrate within my main program. My main application loops at approximately twice per second whist the openCV should runs at 24 fps. The two programs only share one variable.

What's the best way to do this? Should I use multi-processing, multi-threading or just implement it as another loop in my my program?

I'd rather avoid the latter solution as I don't want to mess with the timing of the main program.

Thanks in advance, Charles

Run openCV application concurrently to main program

I've written an application with OpenCV and python 3.6 which I'd now like to integrate within my main program. My main application loops at approximately twice per second whist the openCV should runs at 24 fps. The two programs only share one variable.

What's the best way to do this? Should I use multi-processing, multi-threading or just implement it as another loop in my my program?

I'd rather avoid the latter solution as I don't want to mess with the timing of the main program.

Thanks in advance, Charles

Edit:

I couldn't find minimal example so here's one that seems to work fine:

from threading import Thread
import cv2
import time

class PlayOnThread:


    def __init__(self, src=0):
        self.stream = cv2.VideoCapture(src)
        (self.grabbed, self.frame) = self.stream.read()
        self.stopped = False

    def start(self):    
        Thread(target=self.get, args=()).start()
        return self

    def get(self):

        while not self.stopped:

            (self.grabbed, self.frame) = self.stream.read()

            if not self.grabbed:
                self.stop()

            if cv2.waitKey(100) == 27:
                self.stop()
                break

            cv2.imshow("Video", self.frame)

    def stop(self):
        self.stopped = True
        self.stream.release()
        cv2.destroyAllWindows()


if __name__ == "__main__":

    video_getter = VideoGet("data/Record_01.avi").start()

    while not video_getter.stopped:
        print('Running Concurrently')
        time.sleep(1)

Run openCV application concurrently to main program

I've written an application with OpenCV and python 3.6 which I'd now like to integrate within my main program. My main application loops at approximately twice per second whist the openCV should runs at 24 fps. The two programs only share one variable.

What's the best way to do this? Should I use multi-processing, multi-threading or just implement it as another loop in my my program?

I'd rather avoid the latter solution as I don't want to mess with the timing of the main program.

Thanks in advance, Charles

Edit:

I couldn't find minimal example so here's one that seems to work fine:

from threading import Thread
import cv2
import time

class PlayOnThread:


    def __init__(self, src=0):
        self.stream = cv2.VideoCapture(src)
        (self.grabbed, self.frame) = self.stream.read()
        self.stopped = False

    def start(self):    
        Thread(target=self.get, args=()).start()
        return self

    def get(self):

        while not self.stopped:

            (self.grabbed, self.frame) = self.stream.read()

            if not self.grabbed:
                self.stop()

            if cv2.waitKey(100) == 27:
                self.stop()
                break

            cv2.imshow("Video", self.frame)

    def stop(self):
        self.stopped = True
        self.stream.release()
        cv2.destroyAllWindows()


if __name__ == "__main__":

    video_getter = VideoGet("data/Record_01.avi").start()
threaded_video = PlayOnThread("data/Record_01.avi").start()

    while not video_getter.stopped:
threaded_video.stopped:
        print('Running Concurrently')
        time.sleep(1)