In which we discover the limits of webcams connected to the BeagleBone Black.
As the previous couple of posts may have hinted, I am currently working on a computer vision application. On the hardware side, I am using a webcam connected to a BeagleBone Black to capture and process images. Finding the right camera and software configuration seems to be a challenge many people are trying to overcome. The following is what I have learned through experimentation.
During my first foray into the world of webcams on the BBB, I chose the PS3Eye. The PS3Eye has been used for many computer vision applications thanks to its ability to produce uncompressed 640x480 images at up to 60 FPS or uncompressed 320x240 at up to 120 FPS. The ability to capture uncompressed images at high frame rates plus being available for $16.98 would normally make the PS3Eye a fantastic choice; however, we are dealing with the BBB.
If you plug a PS3Eye into the BBB and fire up an OpenCV application to capture at 640x480, you will receive "Select Timeout" errors instead of a video stream. If you do the same but with the resolution set to 320x240, it will work. It turns out the PS3Eye transfers data in bulk mode over USB. In bulk mode, you are guaranteed to receive all of the transmission; however, you are not guaranteed timing. What is essentially happening is the PS3Eye is saturating the bulk allotment on the USB. The reason you encounter this problem at 640x480 and not 320x240 is because OpenCV with Python sets the frame rate to 30 FPS and provides no way to change it. We can calculate the amount of data put on the bus as follows:
Height * Width * (Channels * 8) * FPS
So for our uncompressed images at 640x480 we have:
640 * 480 * (3 * 8) * 30 = 221184000 bits/s or ~26.36 MB/s
and 320x240 is ~6.59 MB/s
As OpenCV with Python does not allow you to set frame rate, I modified v4l2grab to accept frame rate as a command line argument. With this, I discovered you can capture images from the PS3Eye at 640x480 as long as you set the frame rate to 15 FPS or less. You can also capture images at 320x240 at up to 60 FPS. The astute reader will notice that 640 * 480 * (8 * 3) * 15 = 320 * 240 * (8 * 3) * 60 which is ~13.2 MB/s. In other words, the USB on the BBB taps aout at ~13.2 MB/s for bulk transfers.
At this point you might be thinking you do not have to worry about frames per second because you will only take still shots. It turns out uvc under Linux does not support still image capture. In order to capture an image, you open the webcam the same way you would to capture a stream; however, you just grab one frame (or more if needed).
If you would like to capture 640x480 or larger images at 30 FPS or faster, all is not lost, but you will need a webcam that supports some sort of native compression. In my case, I am using a Logitech C920. It can compress using both H264 and MJPEG. If you want to capture a video stream, H264 is probably your best choice as it should have fewer compression artifacts. It you are after still shots, MJPEG will be your friend.
MJPEG typically compresses each frame as a separate jpeg*. Since MJPEG uses intra-frame compression, you only need to capture one image for a still shot. H264 uses inter-frame compression - meaning it relies on information from several frames to determine how to compress the current frame. In order to reconstruct the frame, you need all the ones involved in the compression. I know the last two sentences are a great simplification, but they suffice for our discussion.
In order to test the different combinations of frame rates and encodings, I extended the v4l2 capture sample available from the project's website. To the base sample I added the ability to specify image dimensions, frame rate, and pixel format (ie compression). I also added handling for CTRL-C so the webcam is not left in an inconsistent state if you kill the program with CTRL-C, and the ability to set the timeout value and maximum number of timeouts.
The program is available here framegrabber.c.
Please note this software is not finished. I am publishing it now so others may use it to determine the capabilities of their webcams, but I will be improving and extending it in the future. You may consider the capture timing functionality described in 1 below to be complete while the saving of frames described in 2 will change.
To compile framegrabber you must have the development files for v4l2 installed.
gcc framegrabber.c -o framegrabber
At this time, framegrabber is intended to be used in one of two ways.
1. Timing frame capture rates.
To time frame capture rates, simply pass framegrabber to time, omit the -o switch, and set -c to the number of frames you would like to capture. Omitting -o instructs the program to simply discard all captured frames. In this mode, framegrabber will capture c number of frames from the webcam as fast as possible.
Here is the simplest case:
time ./framegrabber -c 1000
And here we set the pixel format, image dimensions, and frame rate:
time ./framegrabber -f mjpeg -H 480 -W 640 -c 1000 -I 30
Have a look at all the other command line switches to get a sense of the possibilities.
2. Capturing frames from a webcam
As mentioned above, I extended the application v4l2grab to support setting the frame rate. v4l2grab allows you to capture jpeg images from webcams that support the YUYV format. It grabs frames in YUYV format and then converts the frames to jpeg.
When capturing frames with framegrabber, the raw frame is written out. No conversion to jpeg is done. This is mostly a proof on concept to show that frames captured in MJPEG format are individual jpegs and can be written out without further processing. This has been tested with a Logitech C920, and the output is indeed an jpeg image. Capturing in H264 and YUYV format will also work, but you will not be able to simply open the resulting file in your favorite image editor.
Currently there is no way to specify the filename for the frame or frames captured, and if -c is greater than one, the first c - 1 frames will be overwritten by framec. To capture a frame, include the -o switch and set -c to one. The resulting frame will be written to capture.jpg.
./framegrabber -f mjpeg -H 480 -W 640 -c 1
And now for the results of testing both the PS3Eye and Logitech C920
Here we see capturing 1000 frames from the Logitech C920 in MJPEG format takes ~33.6 seconds which is ~29.76 frames per second.
Here we see capturing 1000 frames from the Logitech C920 in YUYV format takes ~67 seconds which is ~14.92 frames per second.
Moving to the PS3Eye we see that if we try to capture at 30 FPS, we receive a select time out error, but if we set the frame rate to 15, we are successful. If you compare the results of the PS3Eye capture with the results of the Logitech C920 YUYV test, you will see the real times are essentially the same, almost 15 frames per second.
At this point you maybe wondering why the Logitech C920 does not receive select timeouts at 30 FPS YUYV but the PS3Eye does. If you notice, even though we set the frame rate to 30 FPS, we receive frames from the C920 at about 15 FPS. The C920 use isochronous transfers as opposed to bulk like the PS3Eye, and isochronous guarantees transfer speed but not delivery. It is likely that frames are getting dropped but enough make it through fast enough that we do not receive select timeouts. I have not tested this further as of now. For more information on USB transfers see .
In our final screenshot we can see that framegrabber uses very little cpu (.3%) while just grabbing frames.
I hope you find framegrabber useful. The interested reader can extend the process_image function to do as they will with frames captured from the webcam.
It seems some MJPEG streams omit the Huffman table causing the resulting jpeg captures to fail to open in some programs. The error message, if any, is something along the lines of "Huffman table 0x00 was not defined". If you cannot open the MJPEG captures, please try the Python script MJPEGpatcher.py below. MJPEGpatcher should patch the captured jpeg with the required information. It takes a single command line argument, -f FILENAME, and outputs FILENAME[patched].jpg. The Logitech C920 does not exhibit this behavior. MJPEGpatcher has been tested and works on images captured by a Toshiba webcam built into a laptop as well as an image submitted by a reader. I would appreciate any feedback.
William C Bonner pointed out in the comments that I neglected to provide any timing information for the C920 for resolutions greater than 640x480. When researching for this post, I was interested in explaining why the C920 could provide 640x480 at 30 FPS and the PS3Eye could not. In doing so, I focused on the greatest resolution and frame rate the two cameras had in common. To redress my omission, here are timings for the C920 for 1920x1080 at 30 FPS in MJPEG, H264, and YUYV formats. It can be seen below that the C920 is able to provide 1920x1080 at 30 FPS in both MJPEG and H264 formats, but YUYV tops out around 2.49 FPS
framegrabber.c (18.79 kb)
MJPEGpatcher.py (6.46 kb)