Flask freezes with OpenCV Flask freezes with OpenCV flask flask

Flask freezes with OpenCV


You can't instance two CamerasPool objects since you only have one camera. But what you can do is:

  • Change the implementation of CamerasPool so that it becomes its own thread and doesn't block the execution of the python application. The purpose of this class is to simply read frames from the camera, make a copy of each frame and put() them in two separate queue objects. The queues should be global in the program so that they are accessible to the other threads that will need to run concurrently to process the data and stream them.
  • Create a new class VideoStream to be responsible to get() frames from a particular queue, process it and stream it. Processing means anything you'd like to do with the frame before it gets streamed to the network: convert to grayscale, draw rectangles, detect faces, etc. This class will also need to run in a separate thread and the constructor will need to receive two parameters: the first to indicate which of the two global queues it should use; the second parameter to indicate whether the frames should be processed before they are streamed;

If you are looking for code examples on how to use multithreading to retrieve frames from a camera and store them in a queue, check this answer, specially the section that says:

How to capture at the closest maximum fps supported by the camera? A threading and queue example.

Keep in mind that your application will have 4 threads:

  • the main thread;
  • the one that belongs to CamerasPool;
  • and the other 2 threads are from each of the VideoStream objects that will be instanced;