Ask Your Question
0

python frame grabbing from ip camera and process in a different thread

asked 2017-02-15 04:43:37 -0500

paolo.rota gravatar image

Hi,

When I grab frames with VideoCapture, the stream slows down while grabbing, so if in the beginning it runs smoothly, after a minute it gets super slow. I believe this happens because of some sort of buffer where the VideoCapture stores the images, while the processing piece of code is doing its magic. (speed grabber > speed processing).

I am thinking to have a thread that is solely grabbing the frames from the camera and another that is fetching the current frame, does some processing and browse the processed image. However, passing this image through threads does not seem quite easy. Does anyone have an idea how to put me on the right way?

PS: I'm using python interface

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-02-22 13:52:58 -0500

updated 2017-02-22 13:53:42 -0500

This is an implementation that I used once

python has a thread safe queue which can be used for this purpose

hope this will help

import threading
import time
import Queue
import cv2
frames = Queue(10)

class ImageGrabber(threading.Thread):
    def __init__(self, ID):
        threading.Thread.__init__(self)
        self.ID=ID
        self.cam=cv2.VideoCapture(ID)

    def Run(self):
        global frames
        while True:
            ret,frame=self.cam.read()
            frames.put(frame)
            time.sleep(0.1)


class Main(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global frames
        while True:
            if(not frames.empty()):
                self.Currframe=frames.get()
            ##------------------------##
            ## your opencv logic here ##
            ## -----------------------##


grabber = ImageGrabber(0)
main = Main()

grabber.start()
main.start()
main.join()
grabber.join()
edit flag offensive delete link more

Comments

In the ImageGrabber class should the method name be "run" or "Run" ?

biranchi125 gravatar imagebiranchi125 ( 2018-01-27 17:42:53 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-02-15 04:43:37 -0500

Seen: 6,532 times

Last updated: Feb 22 '17