Video Editing
Video Editing
Navigation
Have you ever worked with a video file via OpenCVs cv2.VideoCapture function and
found that reading frames just felt slow and sluggish?
Your entire video processing pipeline crawls along, unable to process more than one or
two frames per second even though you arent doing any type of computationally
expensive image processing operations.
Why is that?
1 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Depending on your video file type, the codecs you have installed, and not to mention, the
physical hardware of your machine, much of your video processing pipeline can actually be
consumed by reading and decoding the next frame in the video file.
In the remainder of todays blog post, Ill demonstrate how to use threading and a queue
data structure to improve your video file FPS rate by over 52%!
First, you instantiate your cv2.VideoCapture object by passing in the path to your input
video file.
Free 21-day crash course
Then you start a loop, calling the .read method of cv2.VideoCapture to poll the next
onit incomputer
frame from the video file so you can process your pipeline. vision &
image search engines
The problem (and the reason why this method can feel slow and sluggish) is that
youre both reading and decoding the frame in your
Interested in main processing
computer thread!
vision and image search
engines, but don't know where to start? Let me
As Ive mentioned in previous posts, the .read method
help. I've is aablocking
created operation
free, 21-day crashcourse that
the main thread of your Python + OpenCVisapplication
hand-tailored to giveblocked
is entirely you the(i.e.,
beststalled)
possible
until
introduction to computer vision. Sound
the frame is read from the video file, decoded, and returned to the calling function. good?
Enter your email below to start your journey to
becoming
By moving these blocking I/O operations to a computer
a separate vision
thread and master.a queue of
maintaining
decoded frames we can actually improve our FPS processing rate by over 52%!
This increase in frame processing rate (and therefore our overall video processing
pipeline) comes from dramatically reducing LET'S
latency
DO
IT!we dont have to wait for the .read
method to finish reading and decoding a frame; instead, there is always a pre-decoded
2 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
To accomplish this latency decrease our goal will be to move the reading and decoding of
video file frames to an entirely separate thread of the program, freeing up our main thread
to handle the actual image processing.
But before we can appreciate the faster, threaded method to video frame processing, we
first need to set a benchmark/baseline with the slower, non-threaded version.
To start, open up a new file, name it read_frames_slow.py , and insert the following code:
Line 15 opens a pointer to the --video file using the cv2.VideoCapture class
while Line 16 starts a timer that we can use to measure FPS, or more specifically, the
LET'S DO IT!
throughput rate of our video processing pipeline.
3 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Lines 40-42 display the frame to our screen and update our FPS counter.
The final code block handles computing the approximate FPS/frame rate throughput of our
pipeline, releasing the video stream pointer,LET'S DO IT!
and closing any open windows:
4 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
45 fps.stop()
46 print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
47 print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
48
49 # do a bit of cleanup
50 stream.release()
51 cv2.destroyAllWindows()
To execute this script, be sure to download the source code + example video to this blog
post using the Downloads section at the bottom of the tutorial.
For this example well be using the first 31 seconds of the Jurassic Park trailer (the .mp4
file is included in the code download):
Free
Lets go ahead and obtain a baseline for frame 21-day
processing crash
throughput course
on this example
video:
on computer vision &
Faster video file FPS with image
cv2.VideoCapture search engines
and OpenCV Shell
1 $ python read_frames_slow.py --video videos/jurassic_park_intro.mp4
Interested in computer vision and image search
engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
LET'S DO IT!
5 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Figure 1: The slow, naive method to read frames from a video file using Python and OpenCV.
As you can see, processing each individual frame of the 31 second video clip takes
approximately 47 seconds with a FPS processing rate of 20.21.
These results imply that its actually taking longer to read and decode the individual frames
than the actual length of the video clip!
To see how we can speedup our frame processing throughput, take a look at the technique
I describe in the next section.
Free 21-day crash course
on computer vision &
image search engines
Interested in computer vision and image search
engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
6 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
main Python script that
is solely responsible for reading frames from the video file and maintaining a queue.
Since Pythons Queue data structure is thread safe, much of the hard work is done for us
already we just need to put all the pieces together.
Ive already implemented the FileVideoStream class in imutils but were going to review the
code so you can understand whats going on under the hood:
Lines 2-4 handle importing our required Python packages. The Thread class is used to
create and start threads in the Python programming language.
We need to take special care when importing the Queue data structure as the name of the
queue package is different based on which Python version you are using (Lines 7-12).
7 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
To kick off the thread, well next define the start method:
This method simply starts a thread separate from the main thread. This thread will call the
.update method (which well define in the next code block).
The update method is responsible for reading and decoding frames from the video file,
along with maintaining the actual queue data structure:
If the stopped indicator is set, we exit the thread (Lines 37 and 38).
LET'S DO IT!
If our queue is not full we read the next frame from the video stream, check to see if we
8 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
The read method will handle returning the next frame in the queue:
Well create a convenience function named more that will return True if there are still
more frames in the queue (and False otherwise):
And finally, the stop method will be called if we want to stop the thread prematurely (i.e.,
before we have reached the end of the video file):
on computer
Faster video file FPS with cv2.VideoCapture and OpenCV vision & Python
1 # import the necessary packages
image search engines
2 from imutils.video import FileVideoStream
3 from imutils.video import FPS
4 import numpy as np Interested in computer vision and image search
5 import argparse engines, but don't know where to start? Let me
6 import imutils
7 import time help. I've created a free, 21-day crash course that
8 import cv2 is hand-tailored to give you the best possible
9
introduction to computer vision. Sound good?
10 # construct the argument parse and parse the arguments
11 ap = argparse.ArgumentParser() Enter your email below to start your journey to
12 ap.add_argument("-v", "--video", required=True,
becoming a computer vision master.
13 help="path to input video file")
14 args = vars(ap.parse_args())
15
16 # start the file video stream thread and allow the buffer to
17 # start to fill
18 print("[INFO] starting video file thread...")
LET'S DO IT!
19 fvs = FileVideoStream(args["video"]).start()
20 time.sleep(1.0)
9 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Lines 2-8 import our required Python packages. Notice how we are using the
FileVideoStream class from the imutils library to facilitate faster frame reads with
OpenCV.
Lines 11-14 parse our command line arguments. Just like the previous example, we only
need a single switch, --video , the path to our input video file.
We then instantiate the FileVideoStream object and start the frame reading thread (Line
19).
Our next section handles reading frames from the FileVideoStream , processing them,
and displaying them to our screen:
The last code block computes our FPS throughput rate and performs a bit of cleanup:
LET'S DO IT!
Faster video file FPS with cv2.VideoCapture and OpenCV Python
44 # stop the timer and display FPS information
10 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
48
49 # do a bit of cleanup
50 cv2.destroyAllWindows()
51 fvs.stop()
To see the results of the read_frames_fast.py script, make sure you download the
source code + example video using the Downloads section at the bottom of this tutorial.
Figure 3: Utilizing threading with cv2.VideoCapture and OpenCV leads to higher FPS and a larger throughput
rate.
Free
As we can see from the results we were able 21-day
to process crash
the entire course
31 second video clip
in 31.09 seconds thats an improvement of 34% from the slow, naive method!
on computer vision &
image
The actual frame throughput processing rate search
is much faster, engines
clocking in at 30.75 frames
per second, an improvement of 52.15%.
Interested in computer vision and image search
engines, but don't know where to start? Let me
Threading can dramatically improve the speed of your video processing pipeline use it
help. I've created a free, 21-day crash course that
whenever you can.
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
What about built-in webcams,Enter
USB cameras,
your and
email below theyour
to start Raspberry
journey to
Pi? What do I do then? becoming a computer vision master.
This post has focused on using threading to improve the frame processing rate of video
files.
LET'S DO IT!
If youre instead interested in speeding up the FPS of your built-in webcam, USB camera,
11 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Summary
In todays tutorial I demonstrated how to use threading and a queue data structure to
improve the FPS throughput rate of your video processing pipeline.
By placing the call to .read of a cv2.VideoCapture object in a thread separate from the
main Python script we can avoid blocking I/O operations that would otherwise dramatically
slow down our pipeline.
Finally, I provided an example comparing threading with no threading. The results show
that by using threading we can improve our processing pipeline by up to 52%.
However, keep in mind that the more steps (i.e., function calls) you make inside your
while loop, the more computation needs to be done therefore, your actual frames per
second rate will drop, but youll still be processing faster than the non-threaded version.
To be notified when future blog posts are published, be sure to enter your email
address in the form below!
Downloads:
Freethe21-day
If you would like to download crash
code and images usedcourse
in this post,
please enter your email address in the form below. Not only will you get
on computer vision &
a .zip of the code, Ill also send you a FREE 11-page Resource Guide
image
on Computer Vision and search
Image Search Engines,engines
including exclusive
techniques that I dont post on this blog! Sound good? If so, enter your
Interested in computer vision and image search
email address and Ill send you but
engines, the code
don't immediately!
know where to start? Let me
Email address: help. I've created a free, 21-day crash course that
12 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Enter your email address below to get my free 11-page
Image Search Engine Resource Guide PDF. Uncover
exclusive techniques that I don't publish on this blog and
start building image search engines of your own!
A day in the life of a Adrian Rosebrock: computer vision researcher, developer, and entrepreneur.
Recognizing digits with OpenCV and Python
REPLY
Steve Goldsmith February 6, 2017 at 11:14 am #
This didnt work for me using a CHIP SoC. I saw exactly the same frame rate. A
simpler method is to move the UVC code into a separate process (mjpg-streamer) and use
a simple socket based client thus removingFree 21-day
the need crash
for VideoCapture. course
I get precise FPS
and better overall performance this way. See my project https://github.com/sgjava/opencv-
on computer vision &
chip which includes the Python code and performance tests.
image search engines
Interested in computer vision and image search
REPLY
Letme
Adrian Rosebrock Februaryengines, butam
7, 2017 at 9:09 don't
# know where to start?
help. I've created a free, 21-day crash course that
Thanks for sharing Steve! is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
REPLY
ghanendra February 6, 2017 at 12:08 pm #
13 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
REPLY
Adrian Rosebrock February 7, 2017 at 9:08 am #
I actually demonstrate how to use OpenCV and TKinter in this blog post. The
post assumes youre reading from a video stream, but you can swap it out for a file
video easily.
REPLY
Ghanendra February 13, 2017 at 10:11 am #
When I read frames from a video using cv2.VideoCapture, speed of reading the
frames is much faster than the normal speed of the video. How to read frames
from the video at constant rate?
Hi Adrian, Im using TCP protocol over network to receive frames, fps is very low.
What can I do for that?
REPLY
Adrian Rosebrock February 13, 2017 at 1:34 pm #
OpenCV doesnt actually care what the true FPS of the video is. The
goal, in the case of OpenCV, is to read and process the frames as fast as
possible. If you want to display frames at a constant rate youll need to insert
time.sleep calls into the main Free loop. 21-day
Again, OpenCVcrash course
isnt directly
made for
video playback.
on computer vision &
As or transmitting frames overimage search
a TCP protocol, engines
that is more network overhead
which will certainly reduce your overall FPS. You should look into gstreamer
and FFMPEG to speedup theInterested
streaming process.
in computer vision and image search
engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
Ghanendra February
introduction to computer
16, 2017 at 10:51 pm # vision. Sound good?
Enter your email below to start your journey to
Thank a lot Adrian.
becoming a computer vision master.
REPLY
Luis February 6, 2017 at 1:01 pm #
LET'S DO IT!
Hey Adrian, good work as always, i have implemented this in a real time
14 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
the raw h264 frames from camera using opencv. Do you have any experience with this?
Im sure this would increase even more my fps since no decoding-enconding would be
requiered.
REPLY
Adrian Rosebrock February 7, 2017 at 9:07 am #
Unfortunately, I do not have any direct experience with this. I would likely look
into some combination of FFMPEG and gstreamer to see if you could stream the
frames directly over your network.
REPLY
skrtu June 3, 2017 at 9:15 am #
https://mike632t.wordpress.com/2014/11/04/video-streaming-using-netcat/
Free 21-day crash course
on computer vision &
image search engines
Interested in computer vision and image search
engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
LET'S DO IT!
15 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Hi Adrian,
I think there may be a problem with the python3 implementation:
on computer
Hi Florent I just double-checked vision
on my system. My Python &
2.7 fast
method does seem to be slightly faster than the Python 3.5 fast method. Perhaps
image search engines
there is a difference in the Queue data structure between Python versions that I am
not aware of (or maybe the threading?).
Interested in computer vision and image search
engines,
However, I am not able to replicate your slower but don't
method know
being where to start?
substantially Let me
speedier
than the fast, threaded method (Pythonhelp.
3.5):I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
$ python read_frames_slow.py video videos/jurassic_park_intro.mp4
introduction to computer vision. Sound good?
[INFO] elasped time: 44.06
Enter your email below to start your journey to
[INFO] approx. FPS: 21.70
becoming a computer vision master.
$ python read_frames_fast.py video videos/jurassic_park_intro.mp4
[INFO] starting video file thread
[INFO] elasped time: 38.52
[INFO] approx. FPS: 24.82 LET'S DO IT!
16 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
REPLY
Frederik Kratzert February 9, 2017 at 9:42 am #
I tried the same on two different environments (Ubuntu 16.04 with python 3.5,
Windows 10 with python 3.5) and got comparable results as Florent.
I noticed, that once the video has reached the end and the Queue function is not
loading more images, the fps rate increases dramatically (like i suppose it should
be normally with the threading).
REPLY
Adrian Rosebrock February 10, 2017 at 12:21 pm #
That is very weird. The slow version is more than 2x faster. Quite Ironic!
LET'S DO IT!
17 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
REPLY
Wim Valcke April 22, 2017 at 5:39 am #
Hi Adrian,
Free
The fast version after the change
21-day crash course
[INFO] elasped time: 8.13 on computer vision &
[INFO] approx. FPS: 102.72
image search engines
Its a bit faster than the non thread version, reason is the overlapping time of
reading a frame and processing Interested indifferent
a frame in computer vision and image search
threads.
engines, but don't know where to start? Let me
Moral of the story is that tight help.
while I've
loopscreated
are never a good
a free, idea when
21-day crashusing
course that
threading. is hand-tailored to give you the best possible
This solution should work alsointroduction
for python 2.7.
to computer vision. Sound good?
Enter
I just have another remark. The mainyour email
thread belowthat
assumes to start
if the your
queuejourney
is to
empty, the video is at the end.becoming a computer
If we would vision
have a faster master.
consumer than a
producer this is a problem.
My suggestion is to add a method to FileVideoStream
def running(self):
# indicate that the thread is still LET'S DO IT!
running
18 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
The current implementation changes self.stopped to True is the video file is at
the end.
In the main application can be changed like this. Also the sleep of 1 second at
the beginning (which was there to let the queue fill up) can be left out.
You nicely see the video showing and you see the queue size filling up in a
few secs to max.
while fvs.running():
# grab the frame from the threaded video file stream, resize
# it, and convert it to grayscale (while still retaining 3
# channels)
frame = fvs.read()
frame = imutils.resize(frame, width=450)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = np.dstack([frame, frame, frame])
\Wim.
Hi Wim thanks for sharing. I would like to point out that the
Queue data structure is thread safe in both Python 2.7 and Python 3. Its
also strange that the slowdown doesnt happen with Python 2.7 it only
seems to happen with Python 3. Your time.sleep call seems to do the
Free
trick as it likely prevents the 21-day
semaphores crash
from constantly course
being
polled, but
again, it still seems to be Python 3 specific.
on computer vision &
image search engines
Interested in computer vision and image search
REPLY
Jon February 7, 2017 at 2:00 pm #
engines, but don't know where to start? Let me
Hi Adrian, help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
Would there be a way to use this technique to process videos much quicker? Say I had a
introduction to computer vision. Sound good?
video that was 1 hour long, could I then use threading to process the video in less than an
Enter your email below to start your journey to
hour? What happens if I try to use the VideoWriter to save the frames from the thread? Will
becoming a computer vision master.
the resulting video file play at a much faster speed than the original input video?
REPLY
Adrian Rosebrock February 10,LET'S
2017 DO IT! pm #
at 2:18
19 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
CPU and have them process the frames in parallel. This will dramatically speedup your
throughput.
REPLY
Kenny February 8, 2017 at 11:26 am #
Thanks Adrian! As always, theres always something new to learn from you and
fellow enthusiasts on your web!
REPLY
Adrian Rosebrock February 10, 2017 at 12:21 pm #
Thanks Kenny!
REPLY
TomKom February 9, 2017 at 8:52 am #
Hi Adrian,
Do you think its feasible to change queue length dynamically?
Ive got a script that does detection (1st frame) and then tracking (remaining 24 frames), in
an endless loop, taking images from camera.
The problem Im facing is that during detection some frames are lost and the tracker is not
able to pick up the object.
Free 21-day crash course
When using the queue its a bit more random as queue fills up quickly (quicker than the
script takes them away from the front/bottom onof computer vision
the queue) so its constantly full.&
When
detection happens, new frames are not added image search
to the queue engines
so theyre lost, though in a
more random order now.
Do you have any recommendations for this issue? in computer vision and image search
Interested
Thank you, engines, but don't know where to start? Let me
Tom help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
LET'S DO IT!
20 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
You can certainly change the queue length dynamically, but you would have
to implement a thread-safe queue with a dynamic size yourself. Actually, Im not sure
about this, but if you instantiate Queue() without any data parameters it might allow
for the queue to shrink and expand as needed.
REPLY
Brandon February 11, 2017 at 11:38 am #
cap = cv2.VideoCapture(vidfile)
I then get things like file fps or number of frames using various cap.get calls, example:
fps = cap.get(cv2.CAP_PROP_FPS)
Is there a way to use get and set on the videoCapture object which with this method would
be in FileVideoStream?
Thanks,
Brandon
REPLY
Adrian Rosebrock February 13, 2017 at 1:49 pm #
Free 21-day crash course
on computer
Great question Brandon. I personally vision
dont like using the cap.get &
and
get.set calls as they can be extremely temperamental based on your OpenCV
image search engines
version, codecs installed, or if youre working with a webcam versus file on disk.
Hi Adrian,
Good article as always ! Recently, I came up with the exact same need, separate
processing and input/output for a small script.
LET'S DO IT!
I guess that your example has a few drawbacks that I would like to point out :
21 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
condition, you have to fill it before. I would suggest another approach where reading is
performed by the main thread and the processing by another thread. You could use the
Queue.join() Queue.task_done() as a way to synchronize threads. Usually this pattern is
best achieved with a last message enqueued to kill the processing thread.
Threading in python comes with some limitations. One of them is GIL (Global Interpreter
Lock) which means that even if you are using many threads only one of them is running at
once. This is a major drawback using threading in python. Obviously, if you are only relying
on bindings (opencv here) you can overcome it (the GIL should be released in a c/c++
bindings). As an alternative, I would recommend the multiprocessing module.
Depending on the application, I would consider using a smaller queue but more
processing threads. Obviously, you rely on a tradeoff between reading time and processing
time.
Thanks,
Gilles.
REPLY
Adrian Rosebrock February 13, 2017 at 1:40 pm #
22 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
REPLY
Adrian Rosebrock February 14, 2017 at 1:27 pm #
Thanks for the tips Giles. Ive always defaulted to threads for I/O heavy tasks
and then processes for computation heavy tasks. Ill start playing around with multi-
processing for other tasks as well now.
REPLY
Raghav February 18, 2017 at 11:57 am #
Hey Adrian! cool post as always. Just a typo though.. in the line feeing up our
main thread, freeing is misspelt
REPLY
Kim Willem van Woensel Kooy February 20, 2017 at 9:00 am #
Hey Adrian! I have learned much from your blog posts. Im also looking for ways
to speed up my VideoCapture-functions, so this post was excellent. But Im wondering if it
is possible to skip frames in a video file? Im trying to detect motion with a simple pixel
based matching (thresholding), and I want to make an if statement telling the program to
skip the next 24 frames if no motion is detected. If motion is detected I want to process
every frame until no motion is detected. See my problem?
Im using VideoCapture.read() for looping through the frames.
Free 21-day crash course
on computer vision &REPLY
Adrian Rosebrock February 22, 2017 at 1:48 pm #
image search engines
Instead of using .read() to read and decode each frame, you could actually
use .grab which is much faster. ThisInterested
would allowinyou
computer
to seek N vision and
frames image
into search
the video
engines,
without having to read and decode each but don'tNknow
of the previous whereIve
1 frames. to heard
start? its
Let me
also possible to use the .set methodhelp. I'vebut
as well, created a free,
I havent 21-day
personally crash
tried this.course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master. REPLY
Chandramauli Kaushik February 28, 2017 at 2:09 pm #
23 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
(cv) Chandramaulis-MacBook-Pro:increaseFPS Mauli$ python read_frames_fast.py video
videos/jurassic_park_intro.mp4
[INFO] starting video file thread
2017-03-01 00:32:21.473 python[43433:374316] !!! BUG: The current event queue and the
main event queue are not the same. Events will not be handled correctly. This is probably
because _TSGetMainThread was called for the first time off the main thread.
Terminated: 15
REPLY
Chandramauli Kaushik February 28, 2017 at 2:11 pm #
REPLY
Adrian Rosebrock March 2, 2017 at 6:57 am #
def read(self):
# return next frame in the queue
return self.Q.get(block=True, timeout=2.0)
def more(self): LET'S DO IT!
# return True if there are still frames in the queue
24 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
This also means you can remove the time.sleep(1.0) line from the main
REPLY
Adrian Rosebrock March 6, 2017 at 3:35 pm #
REPLY
ap April 28, 2017 at 5:41 pm #
SLOW=60.29 FPS
FAST=16.28 FPS
REPLY
Adrian Rosebrock May 1, 2017 at 1:49 pm #
REPLY
Adrian Rosebrock May 3, 2017 at 6:03 pm #
25 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
REPLY
Igor May 1, 2017 at 2:37 pm #
Oh, and I have read all your posts, thanks! Very easy to read and understand for
noob like me
May I have advise from you about this questions:
1. What is the best way to detect traffic signals? (I am using color tracking through HSV
threshold and then detecting shape through HoughCircles . But I am getting a lot of false
positives when red car is crossing or someone brake lights is on
2. What is the best way to detect standing/moving cars? Train Haar?
Thank you!
REPLY
Adrian Rosebrock May 3, 2017 at 6:02 pm #
2. This really depends on the data that you are working with. HOG + Linear SVM
detectors might work here if the video is fixed and non-moving, but it will be harder to
generalize across datasets. In that case you might consider a more advanced
approach such as deep learning.
Free 21-day crash course
on computer vision &
Cooper June 21, 2017 at 8:41 pm # image search enginesREPLY
I switched my script over from theInterested
usual VideoCapture to your
in computer threaded
vision method
and image search
and am only getting a 1-2% better FPS : /engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
REPLY
Adrian Rosebrock June 22, Enter
2017 at 9:27
your amemail
# below to start your journey to
becoming a computer vision master.
Which version of OpenCV and Python are oyu using?
26 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
I ended up playing with a few file formats instead and learned the following from
benchmarking via the FPS class.
Test Files:
101MB 1080p .mp4 (Drone raw file)
26MB 1080p .mov (Quicktime re-export from raw file)
18MB 1080p .m4v (Quicktime re-export from raw file)
Learnings:
The smaller .mov file processed just as slowly as the raw .mp4 file.
The smallest .m4v file processed 2x faster than the raw .mp4 file.
REPLY
Frank July 3, 2017 at 7:05 am #
Hi, Adrian Rosebrock, very good works! I would like to asking you one question, is
cv2.videocapture.read( ) method I/O bound or CPU bound? As far we know, decoding
video is slow, so the read( ) method is blocked. If it is CPU bound, should we use
multiprocessing?
REPLY
Lee July 15, 2017 at 10:31 pm #
Leave a Reply
LET'S DO IT!
27 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Name (required)
Website
SUBMIT COMMENT
Click the button below to get my free 11-page Image Search Engine Resource
Guide PDF. Uncover exclusive techniques that I don't publish on this blog and start
building image search engines of your own.
Free 21-day crash course
Deep Learning for Computer Vision with Python Book
on computer vision &
image search engines
Interested in computer vision and image search
engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
LET'S DO IT!
28 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
You're interested in deep learning and computer vision, but you don't know how to get started. Let me
help. My new book will teach you all you need to know about deep learning.
Free 21-day crash course
on computer vision &
image search engines
Interested in computer vision and image search
engines, but don't know where to start? Let me
Are you interested in detecting faces in images & video? But tired of Googling for tutorials that
help. I've created a free, 21-day crash course that
never work? Then let me help! I guarantee that my new book will turn you into a face detection ninja by
is hand-tailored to give you the best possible
the end of this weekend. Click here to give it a shot yourself.
introduction to computer vision. Sound good?
Enter your email below to start your journey to
CLICK HERE TO MASTER FACE DETECTION
becoming a computer vision master.
LET'S DO IT!
29 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
The PyImageSearch Gurus course is now enrolling! Inside the course you'll learn how to perform:
Click the button below to learn more about the course, take a tour, and get 10 (FREE) sample
lessons.
I'm an entrepreneur and Ph.D who has launched two successful image search
engines, ID My Pill and Chic Engine. I'm here to share my tips, tricks, and hacks I've
learned along the way.
Free 21-day crash course
on computer vision &
image search engines
Learn computer vision in a single weekend.
Interested in computer vision and image search
engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
LET'S DO IT!
30 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Want to learn computer vision & OpenCV? I can teach you in a single weekend. I know. It sounds
crazy, but its no joke. My new book is your guaranteed, quick-start guide to becoming an OpenCV
Ninja. So why not give it a try? Click here to become a computer vision ninja.
Never miss a post! Subscribe to the PyImageSearch RSS Feed and keep up to date with
my image search engine tutorials, tips, and tricks
POPULAR
31 of 32 8/2/17, 12:32 PM
Faster video le FPS with cv2.VideoCapture an... http://www.pyimagesearch.com/2017/02/06/fast...
34
Search
Search...
Free 21-day crash course
on computer vision &
image search engines
Interested in computer vision and image search
engines, but don't know where to start? Let me
help. I've created a free, 21-day crash course that
is hand-tailored to give you the best possible
introduction to computer vision. Sound good?
Enter your email below to start your journey to
becoming a computer vision master.
LET'S DO IT!
32 of 32 8/2/17, 12:32 PM