Skip to content
Advertisement

multi-threading, what is wrong with my code

I was trying to make faster my frames in opencv, it was so slow using it normal, so I decided to ask it here Make faster videocapture opencv the answer was to use multi threading to make it faster, so I code it like this

JavaScript

it give me no errors, but compared to my other code that is on the other question, the webcam open and on this one no, any idea?

Advertisement

Answer

Your VideoStream class’s init looks ok, but I think you might have better luck creating a cv2 VideoCapture object in the init as well:

JavaScript

I’m not really as familiar with webcam.set() but if you want to incorporate that, I’m sure you can.

Here you have grabbed the initial frames:

JavaScript

(Or using the new self.stream variable):

JavaScript

Yet this will only grab a frame when it’s initialized, not in a loop. To achieve a loop, you have to make a few more class methods. One will be for continuously getting frames (I added a self.stopped attribute, although it’s not in your code. It might be a good idea to have a True/False stop flag):

JavaScript

Then if you want to use multithreading, you can make a thread pointing to the read_stream method:

JavaScript

You will have to call the start() method on the VideoStream before you start your CV2 imshow() loop.

JavaScript

Hopefully this helps getting the CV2 display to show. Whether your GFR class or face detection is grabbing the right frames from the VideoStream class… that’s something else, and I can’t debug all that code.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement