PyImageSearch / imutils

A series of convenience functions to make basic image processing operations such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.
MIT License
4.51k stars 1.03k forks source link

Attention, use "VideoStream" may be harmful to you (especially ML researcher) #211

Open yelbuod opened 3 years ago

yelbuod commented 3 years ago

if you want to make a VideoStream from imutils to read a video and put every frame to your ML model and get the result, you might be careful. because in the source code of VideoStream, which is in "imutils/imutils/video/webcamvideostream.py" , you may notice that the author of imutils use another thread to read the video in line 21: ` def start(self):

start the thread to read frames from the video stream

    t = Thread(target=self.update, name=self.name, args=())
    t.daemon = True
    t.start()
    return self `

However, your ML model to process each frame may spend some time. THEN, the problem arise, the thread of reading your video won't wait for your ML model. During the time of processing the frame of your model, The thread may SKIP SEVERAL FRAMES so that NOT EVERY FRAMES in your video can put in your model.

jaysonph commented 3 years ago

@MINUS-1ONE That is true. But I think that is not a problem specific to the codes here. I think this problem arises when you put your model and the VideoStream object in the same main process. If they are in different processes and multiprocessing is used, this problem can be relieved. With all due respect, please correct me if I am wrong.

yelbuod commented 3 years ago

@MINUS-1ONE That is true. But I think that is not a problem specific to the codes here. I think this problem arises when you put your model and the VideoStream object in the same main process. If they are in different processes and multiprocessing is used, this problem can be relieved. With all due respect, please correct me if I am wrong.

I dont really got the answer, do you mean vs = VideoStream(src='path') and vs.read() use another thread which different from the model? if it is not your real mean, can you show me the code? i sincerely thank you.

jaysonph commented 3 years ago

What I mean is, inside the python program, if you simply write out and run the codes, they are all running in the main process. a CPU worker execute all the codes in sequence. If you are running the model in this main process (not on cuda), then possibly this will slow down other work (e.g. missing many frames as you said). I think this also depends your number of cpu workers. I think a better way is to implement multiprocessing, having your VideoStream run in a child process and your model in another process. For details, I think you can go to: https://docs.python.org/2/library/multiprocessing.html

If something above is wrong, please give me some advice. Thanks

frankier commented 2 years ago

This is a bug. Surely the only sane behaviour is to have a finite buffer of a few frames. Just dropping old frames is really odd and only appropriate for live use with a webcam.