Please note: this was an early post – the content of this post was turned into a video that is available on this page: Beaglebone: Video Capture and Image Processing on Embedded Linux using OpenCV
Getting started
1 2 |
root@beaglebone:~# uname -a Linux beaglebone 3.8.11 #1 SMP Wed May 8 07:34:27 CEST 2013 armv7l GNU/Linux |
Start by plugging in the camera with the power down and then booting, praying that the device is picked up. lsusb is a utility for displaying information about USB buses in the system and the devices connected to them. lsusb should show:
1 2 3 4 5 |
root@beaglebone:/usr/share# lsusb Bus 001 Device 002: ID 046d:082d Logitech, Inc. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub root@beaglebone:/usr/share# |
When you cd /dev and do an ls you should see video0 at the bottom:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
root@beaglebone:/dev# ls audio ircomm1 ircomm30 mixer1 ram2 tty13 tty34 tty55 usbmon0 audio1 ircomm10 ircomm31 mmcblk0 ram3 tty14 tty35 tty56 usbmon1 autofs ircomm11 ircomm4 mmcblk0boot0 ram4 tty15 tty36 tty57 usbmon2 block ircomm12 ircomm5 mmcblk0boot1 ram5 tty16 tty37 tty58 v4l btrfs-control ircomm13 ircomm6 mmcblk0p1 ram6 tty17 tty38 tty59 vcs bus ircomm14 ircomm7 mmcblk0p2 ram7 tty18 tty39 tty6 vcs1 char ircomm15 ircomm8 mqueue ram8 tty19 tty4 tty60 vcs2 console ircomm16 ircomm9 net ram9 tty2 tty40 tty61 vcs3 core ircomm17 kmem network_latency random tty20 tty41 tty62 vcs4 cpu_dma_latency ircomm18 kmsg network_throughput rfkill tty21 tty42 tty63 vcs5 disk ircomm19 log null rtc tty22 tty43 tty7 vcs6 dri ircomm2 loop-control ppp rtc0 tty23 tty44 tty8 vcsa dsp ircomm20 loop0 psaux shm tty24 tty45 tty9 vcsa1 dsp1 ircomm21 loop1 ptmx snd tty25 tty46 ttyGS0 vcsa2 fb0 ircomm22 loop2 pts stderr tty26 tty47 ttyO0 vcsa3 fd ircomm23 loop3 ram0 stdin tty27 tty48 ttyO1 vcsa4 full ircomm24 loop4 ram1 stdout tty28 tty49 ttyS0 vcsa5 fuse ircomm25 loop5 ram10 tty tty29 tty5 ttyS1 vcsa6 i2c-0 ircomm26 loop6 ram11 tty0 tty3 tty50 ttyS2 video0 i2c-1 ircomm27 loop7 ram12 tty1 tty30 tty51 ttyS3 watchdog initctl ircomm28 media0 ram13 tty10 tty31 tty52 ubi_ctrl watchdog0 input ircomm29 mem ram14 tty11 tty32 tty53 uinput zero ircomm0 ircomm3 mixer ram15 tty12 tty33 tty54 urandom root@beaglebone:/dev# |
If so, you are ready to go:
Video4Linux2
Video4Linux or V4L is a video capture application programming interface for Linux, supporting many USB webcams, TV tuners, and other devices. Video4Linux is closely integrated with the Linux kernel. To list the devices:
1 2 3 |
root@beaglebone:~/camera# v4l2-ctl --list-devices HD Pro Webcam C920 (usb-musb-hdrc.1.auto-1): /dev/video0 |
To list the formats available:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
root@beaglebone:/dev# v4l2-ctl --list-formats ioctl: VIDIOC_ENUM_FMT Index : 0 Type : Video Capture Pixel Format: 'YUYV' Name : YUV 4:2:2 (YUYV) Index : 1 Type : Video Capture Pixel Format: 'H264' (compressed) Name : H.264 Index : 2 Type : Video Capture Pixel Format: 'MJPG' (compressed) Name : MJPEG root@beaglebone:/dev# |
Get the driver detail:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
root@beaglebone:~# v4l2-ctl --get-priority Priority: 2 root@beaglebone:~# v4l2-ctl -D Driver Info (not using libv4l2): Driver name : uvcvideo Card type : HD Pro Webcam C920 Bus info : usb-musb-hdrc.1.auto-1 Driver version: 3.8.11 Capabilities : 0x84000001 Video Capture Streaming Device Capabilities Device Caps : 0x04000001 Video Capture Streaming |
We can list the controls available:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
root@beaglebone:~# v4l2-ctl -L brightness (int) : min=0 max=255 step=1 default=128 value=128 contrast (int) : min=0 max=255 step=1 default=128 value=128 saturation (int) : min=0 max=255 step=1 default=128 value=128 white_balance_temperature_auto (bool) : default=1 value=1 gain (int) : min=0 max=255 step=1 default=0 value=0 power_line_frequency (menu) : min=0 max=2 default=2 value=2 0: Disabled 1: 50 Hz 2: 60 Hz white_balance_temperature (int) : min=2000 max=6500 step=1 default=4000 value=4000 flags=inactive sharpness (int) : min=0 max=255 step=1 default=128 value=128 backlight_compensation (int) : min=0 max=1 step=1 default=0 value=0 exposure_auto (menu) : min=0 max=3 default=3 value=3 1: Manual Mode 3: Aperture Priority Mode exposure_absolute (int) : min=3 max=2047 step=1 default=250 value=250 flags=inactive exposure_auto_priority (bool) : default=0 value=1 pan_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0 tilt_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0 focus_absolute (int) : min=0 max=250 step=5 default=0 value=0 flags=inactive focus_auto (bool) : default=1 value=1 zoom_absolute (int) : min=100 max=500 step=1 default=100 value=100 |
Get all of the information available for the camera:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
root@beaglebone:~# v4l2-ctl --all Driver Info (not using libv4l2): Driver name : uvcvideo Card type : HD Pro Webcam C920 Bus info : usb-musb-hdrc.1.auto-1 Driver version: 3.8.11 Capabilities : 0x84000001 Video Capture Streaming Device Capabilities Device Caps : 0x04000001 Video Capture Streaming Priority: 2 Video input : 0 (Camera 1: ok) Format Video Capture: Width/Height : 640/480 Pixel Format : 'YUYV' Field : None Bytes per Line: 1280 Size Image : 614400 Colorspace : SRGB Crop Capability Video Capture: Bounds : Left 0, Top 0, Width 640, Height 480 Default : Left 0, Top 0, Width 640, Height 480 Pixel Aspect: 1/1 Streaming Parameters Video Capture: Capabilities : timeperframe Frames per second: 30.000 (30/1) Read buffers : 0 brightness (int) : min=0 max=255 step=1 default=128 value=128 contrast (int) : min=0 max=255 step=1 default=128 value=128 saturation (int) : min=0 max=255 step=1 default=128 value=128 white_balance_temperature_auto (bool) : default=1 value=1 gain (int) : min=0 max=255 step=1 default=0 value=0 power_line_frequency (menu) : min=0 max=2 default=2 value=2 white_balance_temperature (int) : min=2000 max=6500 step=1 default=4000 value=4000 flags=inactive sharpness (int) : min=0 max=255 step=1 default=128 value=128 backlight_compensation (int) : min=0 max=1 step=1 default=0 value=0 exposure_auto (menu) : min=0 max=3 default=3 value=3 exposure_absolute (int) : min=3 max=2047 step=1 default=250 value=250 flags=inactive exposure_auto_priority (bool) : default=0 value=1 pan_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0 tilt_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0 focus_absolute (int) : min=0 max=250 step=5 default=0 value=0 flags=inactive focus_auto (bool) : default=1 value=1 zoom_absolute (int) : min=100 max=500 step=1 default=100 value=100 root@beaglebone:~# |
We can then set an individual value. For example to set the brightness:
1 2 3 4 5 6 7 8 |
root@beaglebone:~# v4l2-ctl --set-ctrl=brightness=200 root@beaglebone:~# v4l2-ctl --all Driver Info (not using libv4l2): Driver name : uvcvideo ... brightness (int) : min=0 max=255 step=1 default=128 value=200 contrast (int) : min=0 max=255 step=1 default=128 value=128 ... |
If we wish to modify the resolution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
root@beaglebone:~# v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1 root@beaglebone:~# v4l2-ctl --all Driver Info (not using libv4l2): Driver name : uvcvideo Card type : HD Pro Webcam C920 Bus info : usb-musb-hdrc.1.auto-1 Driver version: 3.8.11 Capabilities : 0x84000001 Video Capture Streaming Device Capabilities Device Caps : 0x04000001 Video Capture Streaming Priority: 2 Video input : 0 (Camera 1: ok) Format Video Capture: Width/Height : 1920/1080 Pixel Format : 'H264' Field : None Bytes per Line: 3840 Size Image : 4147200 Colorspace : SRGB Crop Capability Video Capture: Bounds : Left 0, Top 0, Width 1920, Height 1080 Default : Left 0, Top 0, Width 1920, Height 1080 Pixel Aspect: 1/1 Streaming Parameters Video Capture: Capabilities : timeperframe Frames per second: 30.000 (30/1) Read buffers : 0 brightness (int) : min=0 max=255 step=1 default=128 value=200 contrast (int) : min=0 max=255 step=1 default=128 value=128 ... |
You will notice that the resolution has changed to 1920 x 1080 and the Pixel Format has changed to ‘H264’ which is a feature of the Logitech C920 HD USB Camera.
Using the capture.c and grabber.c programs
Again, this is better described in the recent video on this page: Beaglebone: Video Capture and Image Processing on Embedded Linux using OpenCV
The pixel format or type of compression, set by the application
Identifier Code Byte 0 Byte 1 Byte 2
V4L2_PIX_FMT_RGB24 ‘RGB3’ r7 r6 r5 r4 r3 r2 r1 r0 g7 g6 g5 g4 g3 g2 g1 g0 b7 b6 b5 b4 b3 b2 b1 b0
http://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html#rgb-formats
Video images are typically interlaced. Applications can request to capture or output only the top or bottom field, or both fields interlaced or sequentially stored in one buffer or alternating in separate buffers.
The source video capture code came from:
http://linuxtv.org/downloads/v4l-dvb-apis/capture-example.html
Changed code by setting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fprintf(stderr, "Force Format %d\n", force_format); if (force_format) { if (force_format==2){ fmt.fmt.pix.width = 1920; fmt.fmt.pix.height = 1080; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_H264; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; } else if(force_format==1){ fmt.fmt.pix.width = 640; fmt.fmt.pix.height = 480; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; } |
The new code is available at: github.com/derekmolloy/boneCV
OpenCV
OpenCV, which stands for Open (Source) Computer Vision is a substantial library of image processing and computer vision functions. The functions have been highly optimized and so are suitable for robotic type applications and indeed for implementation on boards such as the Beaglebone Black.
opkg update
opkg list |grep opencv
1 2 3 4 5 |
root@beaglebone:/dev# opkg install opencv-apps Installing opencv-apps (2.4.2-r3.4) to root... Downloading http://feeds.angstrom-distribution.org/feeds/v2012.12/ipk/eglibc/armv7a-vfp-neon/base/opencv-apps_2.4.2-r3.4_armv7a-vfp-neon.ipk. Configuring opencv-apps. root@beaglebone:/dev# |
To see what we just installed!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
root@beaglebone:/var/lib/opkg/info# more opencv-apps.list /usr/bin/opencv_performance /usr/share/OpenCV/lbpcascades/ /usr/share/OpenCV/haarcascades/haarcascade_lowerbody.xml /usr/share/OpenCV/haarcascades/haarcascade_mcs_upperbody.xml /usr/share/OpenCV/OpenCVConfig.cmake /usr/share/OpenCV/haarcascades/haarcascade_profileface.xml /usr/bin/opencv_haartraining /usr/share/OpenCV/haarcascades/haarcascade_mcs_leftear.xml /usr/share/OpenCV/haarcascades/haarcascade_righteye_2splits.xml /usr/share/OpenCV/haarcascades/haarcascade_lefteye_2splits.xml /usr/share/OpenCV/haarcascades/haarcascade_mcs_eyepair_big.xml /usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml /usr/share/OpenCV/haarcascades/haarcascade_eye.xml /usr/share/OpenCV/haarcascades/haarcascade_mcs_lefteye.xml /usr/share/OpenCV/haarcascades/haarcascade_mcs_nose.xml /usr/share/OpenCV/haarcascades/ /usr/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml /usr/share/OpenCV/haarcascades/haarcascade_fullbody.xml /usr/share/OpenCV/haarcascades/haarcascade_mcs_mouth.xml /usr/share/OpenCV/haarcascades/haarcascade_mcs_rightear.xml /usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt_tree.xml /usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml /usr/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml /usr/bin/opencv_createsamples /usr/share/OpenCV/haarcascades/haarcascade_mcs_eyepair_small.xml /usr/bin/opencv_traincascade /usr/share/OpenCV/haarcascades/haarcascade_upperbody.xml /usr/share/OpenCV/haarcascades/haarcascade_mcs_righteye.xml /usr/share/OpenCV/OpenCVConfig-version.cmake /usr/share/OpenCV/ /usr/share/OpenCV/haarcascades/haarcascade_eye_tree_eyeglasses.xml root@beaglebone:/var/lib/opkg/info# |
Check that you opencv environment is setup correctly:
1 2 3 4 5 6 |
root@beaglebone:~# pkg-config --cflags opencv -I/usr/include/opencv root@beaglebone:~# pkg-config --libs opencv -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_ts -lopencv_video -lopencv_videostab root@beaglebone:~# root@beaglebone:~# g++ `pkg-config --cflags opencv` `pkg-config --libs opencv` capture.cpp -o capture |
My Simple Cpp Code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<iostream> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { VideoCapture capture(0); if(!capture.isOpened()){ cout << "Failed to connect to the camera." << endl; } Mat frame; capture >> frame; if(frame.empty()){ cout << "Failed to capture an image" << endl; return -1; } imwrite("test.jpg", frame); return 0; } |
Compile using:
1 2 |
root@beaglebone:~# v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1 root@beaglebone:~# g++ `pkg-config --cflags --libs opencv` boneCV.cpp -o boneCV |
Final Edge Detection Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<iostream> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { VideoCapture capture(0); capture.set(CV_CAP_PROP_FRAME_WIDTH,1920); capture.set(CV_CAP_PROP_FRAME_HEIGHT,1080); if(!capture.isOpened()){ cout << "Failed to connect to the camera." << endl; } Mat frame, edges; capture >> frame; if(frame.empty()){ cout << "Failed to capture an image" << endl; return -1; } cvtColor(frame, edges, CV_BGR2GRAY); Canny(edges, edges, 0, 30, 3); imwrite("edges.png", edges); imwrite("capture.png", frame); return 0; } |
Unfortunately GPU support is not available, nor do I think it would be easy to achieve. If you try to perform an operation such as:
1 2 3 4 5 6 7 8 |
try{ cv::gpu::GpuMat input, output; input.upload(edges); cv::gpu::threshold(input, output, 128.0, 255.0, CV_THRESH_BINARY); } catch(const cv::Exception& e){ cout << "GPU operation failed" << endl; } |
You will get the following output error:
1 2 |
OpenCV Error: No GPU support (The library is compiled without CUDA support) in mallocPitch, file /build/v2012.12/build/tmp-angstrom_v2012_12-eglibc/work/armv7a-vfp-neon-angstrom-linux-gnueabi/opencv-2.4.2-r3/opencv/modules/core/src/gpumat.cpp, line 749 GPU operation failed |
Performance
See my post on the performance of OpenCV here: Setting the Beaglebone CPU Speed
Hi Derek,
Thanks for this and the great video. Excellent as always…
I tried to replicate your tutorial, but using the Creative Labs VF0610 Live HD Cam.
…
root@bbb-es-02:~/boneCV# lsusb
Bus 001 Device 002: ID 041e:4080 Creative Technology, Ltd
…
root@bbb-es-02:~/boneCV# v4l2-ctl –all
Driver Info (not using libv4l2):
Driver name : uvcvideo
Card type : VF0610 Live! Cam Socialize HD
…
Video input : 0 (Camera 1: ok)
Format Video Capture:
Width/Height : 960/544
Pixel Format : ‘YUYV’
…
I modified the cpature.c file to use the 960/544 values for width/height.
When I run it, I just see 1 “.” and a “Select timeout” error:
…
root@bbb-es-02:~/boneCV# ./capture -F -o -c 300 > output.raw
Force Format 2
.select timeout
…
There is a raw file, but I thing it is bogus:
…
-rw-r–r– 1 root root 1044480 May 26 00:25 output.raw
…
When I run raw2mpg4, It gives an error too:
…
(see Changelog for the list of incompatible changes).
[h264 @ 0x37a20] missing picture in access unit
[h264 @ 0x37a20] no frame!
[h264 @ 0x2da80] Could not find codec parameters (Video: h264)
[h264 @ 0x2da80] Estimating duration from bitrate, this may be inaccurate
output.raw: could not find codec parameters
…
Any clue what may be wrong here…
thanks,
/venkat
Hi Venkat, Thanks for the feedback. It looks like your pixel format is YUYV rather than “H264” in the case of my camera. I’d say the output.raw file is valid but you may need to change the pixel format to V4L2_PIX_FMT_YUYV in the code (replacing my changes). The raw2mpg4 will not work in this case as it assumes that the stream is H264 encoded; however, you may be able to change the call to ffmpeg so that it does a hard transcode. You should also check to see if ffmpeg can capture directly from the /dev/video0 directly. Try:
ffmpeg -f video4linux2 -i /dev/video0 out.avi
or if you wish to force the resolution and frame rate (where r is the rate):
ffmpeg -f video4linu2 -s 960×544 -r 15 -i /dev/video0 out.avi
Derek, great article on the Beaglebone Black. I have been using OpenCV for a few years on Windows, and want to use it on the Beaglebone black along with an Xtion pro live depth camera. I’m a complete Linux newbie and wondered if you could write an article on installing OpenCV to the BB. I need to compile OpenCV with OpenNI, and this is tricky for someone not familiar with linux.
Thanks for any help you can provide,
Dave.
Do you happen to know the amount of bandwidth the C920 uses over the USB connection?
I have an application where I would love to have 3-4 webcams running and streaming data directly to an attached USB storage device, and was wondering how feasible that would be with just a USB hub connected to the Beaglebone’s USB Host port.
Hi Dmitri, I think it is about 3Mbit/second per stream. That should allow for a number of cameras at the same time, depending on your network speed etc. I haven’t tried to connect a hub yet, but if I was I would start with the Beaglebone Ninja, which adds 4 mini-usb ports to the Beaglebone. Derek.
I’ve really been liking your videos and explanations on using the BeagleBone Black. Thanks for producing easy to follow videos and blog posts.
Based on your video I went out and got the Logitech C920 Webcam, partly because I thought that it’s hardware encoding would be a nice feature, and partly because it was available in a store around the corner from me. So far I like what I’ve seen of the camera, I’m just having problems working with it in linux.
Do you have much experience with FFMPEG?
I built the latest version on my BBB, and was trying to capture video from the C920 in h.264, but I’m not having much luck, it never seems to get frames, though I can tell by the “v4l2-ctl –all” command that it’s switching the camera mode.
My command line that’s not producing output is “ffmpeg -f v4l2 -s 1920×1080 -r 10 -vcodec h264 -i /dev/video0 -vcodec copy -y TestOutput.mkv” while the command line “ffmpeg -f v4l2 -s 1920×1080 -r 10 -vcodec mjpeg -i /dev/video0 -vcodec copy -y TestOutput.mkv” produces an output file that ends when I press the “q” key.
Thanks again. Wim.
Thanks William. I had the exact same problem with FFMPEG. I even tried rebuilding FFMPEG from source but still the same problem with H264 (with the same arguments). Like you I could capture MJPEG video for a number of minutes (or until the CPU couldn’t keep up). I feel that it is something that will be fixed in the future and I didn’t investigate modifying the source code of FFMPEG. I even tried using a headerless VLC – neither worked for me! Derek.
I thought I should mention that I managed to cobble together a bit of success with FFMPEG and VLC. I’ll get around to writing up what I did on my own blog after I get all the details worked out, but mentioning it here in rough form may be useful, since I was using your tools.
I ended up with this command line, piping output from your program into ffmpeg, and streaming out of ffmpeg via rtp on port 8090. 192.168.0.10 is the IP Address of my windows machine running VLC.
./boneCV/capture -F -o -c300000 | ffmpeg -i – -vcodec copy -f rtp rtp://192.168.0.10:8090/
I created a file arbitrarily named VLCConfig.sdp on the windows machine which contained:
v=0
o=- 1188340656180883 1 IN IP4 127.0.0.1
s=Bonecam streamed by GStreamer
i=bonecam
t=0 0
a=tool:GStreamer
a=type:broadcast
m=video 8090 RTP/AVP 96
c=IN IP4 127.0.0.1
a=rtpmap:96 H264/90000
I opened that file in VLC, and VLC seems to listen for a starting sequence for about 10 seconds, and then displays the video stream as long as it continues to arrive. I got the SDP file contents from http://www.oz9aec.net/index.php/gstreamer/473-using-the-logitech-c920-webcam-with-gstreamer .
When I run my FFMPEG command it displays what ti thinks should be a good SDP contents, but if I put that in the SDP file, it doesn’t work. I’ve not spent the time yet picking out what’s critically different.
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 192.168.0.10
t=0 0
a=tool:libavformat 55.10.100
m=video 8090 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0JAKLtAPAES8uAokAAAAwAQAAADA8YEAALcbAAtx73vheEQjUA=,aM44gA==; profile-level-id=424028
Running in this mode, with the beaglebone frequency governor set to ondemand, and running at 300MHz, “top” shows FFMPEG using only10% of the CPU. Nice. If I set the governor to performance, FFMPEG uses only 3.3 to 3.8% of the CPU.
Hi William, You re-invigorated my interest on this topic. I have just posted on streaming video using RTP and a separate post on streaming video using UDP unicasting and multicasting. I have to say I am astounded by the video streaming quality. I linked back to your blog from the RTP post as it was your post that made me work on this again! Hopefully you can get something from the posts. Thanks again, Derek.
Excellent – I will have to try this later,
Derek.
Derek,
How about using openCV real time algorithms for pedestrian detection on the beaglebone as example?
Hey Derek,
I have a beaglebone black and have noticed that with the Logitech C920, LifeCam HD-5000 or the LifeCam Studio cameras (MJPEG mode 1920×1080) if I point the camera at something with a lot of color I get frame corruption (lines on the screen or I will see v4l2 dequeue a buffer that is the maximum size the buffer can have (instead of being between 200 and 400kb it is like 1.8 mb, I’ve even seen a 10mb frame.. When I save this frame I noticed it is full of partial jpegs, totally corrupt.(lots of FFD8 headers) This is intermittent but seems to occur when I point the cameras at something very colorful (frame size increases). Do you think this is a bug in UVC Video driver? I can get the same thing to occur on the raspberry pi. On my desktop running Ubuntu I cannot, I’m guessing this is because my desktop is so powerful any timing issue would be very hard to replicate. Most of the time the camera pushes 30 fps no problem (all three cameras) I noticed a lot of dropping payload errors when tracing uvcvideo. Any clue what could be causing this or how I can prevent it? Using h264 it still occurs but not as often, I’m guessing because the pack size is smaller.
That’s interesting Paul – Yes, I noticed that very sudden scene changes corrupt the stream. However, it seems to correct when there is a keyframe change. I assumed it was a buffer problem with the camera encoding (it is designed as a stationary, monitor mounted camera). I hadn’t tried it on the desktop so clearly it is not the camera… I wonder if you change the clock frequency on the BBB does the problem get worse… if so it might be a resource issue; however, I hadn’t noticed any memory/CPU spikes when using the camera…
Really nice code display Derek. Is that a WordPress plugin?
Hi Eamon – Yes, it’s “Crayon Syntax Highlighter” plugin for WordPress. The really nice thing is that I am pulling the code directly from github, so it means any change to the repository changes the code on the page. If only it was in Moodle or Google sites for work in DCU! Derek
Hi Derek!!!
I’m using Beaglebone Black for my fingerprint recognition application and using eclipse with opencv for programming and debuging. but It seemed that I can not debug over them, When I debuged, It required shared library with the error log like this “libopencv_core.so.2.9: cannot open shared object file: No such file or directory”
Can you help me? Can you suggest me how to debug in this situation?
Thanks in advance!
HI Derek!
I am very surprised for the information that you share about BEAGLEBONE, but I have a question. My Favorite lenguage programming is Java, and I find information in opencv.org that explain how to build opencv for Java. My interest is to build the opencv on the BeagleBone for to get the .jar and .so file. But I have problems, because, the cmake don’t recognize the jdk when I follow the instructions of the openvc web page on the BeagleBone. You can help me? I need this for a important university project, but I have a basic information about linux.
link opencv for java: http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html#build.
(excuse me if my English is very regular…)
Hi Derek,
We are trying to build executable file of BoneCV through eclipse,we are not able to link up v4l2 the current program with libv4l2.Please help us.
hi Derek and everyone
I use angstrom 3.8.13 and camera logitech C905
I tried to use your code ‘My Simple Cpp Code example’ to show camera respond but I got only camera LED turned on and “select timeout” . any suggestion what to do?? thanks
I have followed the instruction before and everything was success and I even tried using a 5v power supply and still got nothing
please help
hello .. i ve been trying to connect my webcam (frontech ecam jil 2245) to my Beaglebone Black since 2 weeks without any success.. i keep getting this error:
root@beaglebone:/dev/boneCV# ./capture -f -c 200 -o > output.raw
Force Format 1
select timeout
since my cam does not support h.264 format i tried changing the code of capture.c to suit the required format i.e. YUYV… still no change…
i m a rookie at this. i need to perform some image processing techniques on live streaming video. It would be great help if u could reply ASAP.. please help!!
Iam a newbie in linux and its now month a while learning linux in beaglebone black. I am trying to do a video streaming as you mentioned in your blog. As far i understandl, you have install opencv package in beaglebone black. But is there any possible by doing cross compiler method rather than installing a opencv in beaglebone black.
If there is possible then is there any link to do this or any suggestion.
Thanks
Ganesh
Hello Derek,
First, I would like to thank you for making the video for OPenCV on the Beaglebone Black. It was very helpful.
Second, I would like to ask you a question. Do you think the BBB has the power to recognize a color lighted ping-pong ball vs non-colored ones and control two servo motors to aim a laser point at them? The balls will be moving at .25m/sec.
Thank you for your time and again, nice video.
Charles
Hi Derek,
My BBB is using the std Angstrom Linux – all is out of the box configs, except the typical changing IP#s etc to work within my network. I added and mount a 32Gig Kingston Class 10 microSD card with my various video clips (*.mp4) copied to it. I am running off of a separate power adapter. My effort is to run the BBB as a remote video player to a HD TV/Monitor. The Angstrom linux video player I have tried is the ” mplayer.” which is selected when just clicking on the video clip. When in play mode, the video (whatever the clip size) seems to run in slow motion and the larger the video (move from a size 25Mbyte to 180Mbytes) clip size, the slower or longer the video “start” offset time from the audio start. ( I am not interested in the audio per se) , as I am in the smooth video playback. Once the video is on the HD monitor it transitions from frame to frame very slowly. Running from the mounted microSD or Desktop
is roughly the same. Monitoring the CPU traffic – nearly 100% but the processes are the X window
and the gnome monitor taking up much of the CPU % with the mplayer taking about 25%. Have worked it with & without the network cable plugged in. Kind of strange – what might be throttling the CPU and slowng mplayer? Or should I try another player (although I don’t think mplayer seems on the outset to
be the problem?
thanks,
Hi Steve, I’m afraid the BBB is just not powerful enough to software decode and play high-resolution MP4 files at high frame rates. The Raspberry PI has a hardware decoder built in and will likely play the MP4 files fine and if you pay for a separate license it will also play MP2 files. Derek.
hello sir, i m using angstrom version in Beaglebone black. i wrote code for webcam with beaglebone black. i wrote following code named capture.cpp using nano capture.cpp.
when i m running at code using ./capture.cpp then it’s showing below error
” ./capture.cpp: line 3: using: command not found
./capture.cpp: line 4: using: command not found
./capture.cpp: line 6: syntax error near unexpected token
('
int main()’ ”./capture.cpp: line 6:
(which is in attached )
thanks
#include
#include
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture(0);
if(!capture.isOpened()){
cout << "Failed to connect to the camera." <> frame;
if(frame.empty()){
cout << "Failed to capture an image" << endl;
return -1;
}
imwrite("test.jpg", frame);
return 0;
}
Are you using g++ to compile? If you are using gcc, this would be a likely error.
Sir, i can able to solve that error now but when i m running ./capture , it’s giving this error ‘VIDIOC_QUERYMENU: Invalid argument ‘ 4-5 times, but i can able to get the image. Sir how to solve that eror and image quality is also very poor
thanks
I’m getting this error on execution of capture:
“error while loading shared libraries: libv4l2.so.0: cannot open shared object file: No such file or directory”
Installing the following did not fix the error:
apt-get install v4l-utils v4l-conf libav-tools
apt-get install libv4l-dev
Output of ‘ ldd capture’:
libv4l2.so.0 => not found
libc.so.6 => /lib/arm-linux-gnueabi/libc.so.6 (0xb6eae000)
/lib/ld-linux.so.3 (0xb6fa3000)
Were you able to solve this problem?
Sir, when i m running ./capture , it’s giving this error ‘VIDIOC_QUERYMENU: Invalid argument ‘ 4-5 times, but i can able to get the image. Sir how to solve that eror and image quality is also very poor
thanks
Thank you so much for this tutorial code and video, its saved me a ton of time. I modified capture.c to add motion jpeg following your h264 changes:
if (force_format==3){ /* HP 3110 webcam */
fmt.fmt.pix.width = 1280;
fmt.fmt.pix.height = 720;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
}
if (force_format==2){
fmt.fmt.pix.width = 1920;
fmt.fmt.pix.height = 1080;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
}
static void usage(FILE *fp, int argc, char **argv)
{
fprintf(fp,
“Usage: %s [options]\n\n”
“Version 1.3\n”
“Options:\n”
“-d | –device name Video device name [%s]\n”
“-h | –help Print this message\n”
“-m | –mmap Use memory mapped buffers [default]\n”
“-r | –read Use read() calls\n”
“-u | –userp Use application allocated buffers\n”
“-o | –output Outputs stream to stdout\n”
“-f | –format Force format to 640×480 YUYV\n”
“-F | –formatH264 Force format to 1920×1080 H264\n”
“-M | –formatMJPEG Force format to 1280×720 MJPEG\n”
“-c | –count Number of frames to grab [%i] – use 0 for infinite\n”
“\n”
“Example usage: capture -F -o -c 300 > output.raw\n”
“Captures 300 frames of H264 at 1920×1080 – use raw2mpg4 script to convert to mpg4\n”,
argv[0], dev_name, frame_count);
}
static const char short_options[] = “d:hmruofFMc:”;
static const struct option
long_options[] = {
{ “device”, required_argument, NULL, ‘d’ },
{ “help”, no_argument, NULL, ‘h’ },
{ “mmap”, no_argument, NULL, ‘m’ },
{ “read”, no_argument, NULL, ‘r’ },
{ “userp”, no_argument, NULL, ‘u’ },
{ “output”, no_argument, NULL, ‘o’ },
{ “format”, no_argument, NULL, ‘f’ },
{ “formatH264”, no_argument, NULL, ‘F’ },
{ “formatMJPEG, no_argument, NULL, ‘M” },
{ “count”, required_argument, NULL, ‘c’ },
{ 0, 0, 0, 0 }
};
In the main() getopts switch:
case ‘F’:
force_format=2;
break;
case ‘M’:
force_format=3;
break;
I hope this can help someone get started using a cheap webcam that only has mjpeg instead of h264 like the HP 3110.
FYI I’m using the BeagleBone “white” Rev A6 with the latest Angstrom image I could find:
http://downloads.angstrom-distribution.org/demo/beaglebone/testing/Angstrom-Cloud9-IDE-GNOME-eglibc-ipk-v2013.06-beaglebone-2013.12.13.img.xz
Haven’t got grabber.c working yet
I also made a raw2mjpeg script:
#!/bin/bash
echo “Video for Beaglebone Video Applications – derekmolloy.ie”
#echo “Converting raw file to mpeg4 format”
#ffmpeg -f h264 -i output.raw -vcodec copy output.mp4
echo “Converting raw file to mjpeg format”
# this really too slow to be useful, seems avi is a poor container choice
#ffmpeg -f mjpeg -i output.raw -vcodec copy output.avi
ffmpeg -f mjpeg -i output.raw -vcodec copy output.mkv
echo “Finished”
Hi all…I want to perform Image processing tasks in BBB which runs Debian Linux… so I need to install OpenCV on BBB.. It seems i should install cmake in prior… I dont have direct internet connection to my BBB.. I tried to download OpenCV from github.com as a ZIP file and i copied it to BBB using pendrive….Can anyone please guide me how to install cmake on BBB?? i tried to download cmake and ended up in incomplete build…. please help me …. your ideas will be really appreciated
Experts please help the beginners 🙂
Thanks in advance 🙂
Hi DEREK,
Hope you are busy with your “Beginning with BBB “….Kindly spare few seconds for reading this: your blog is extremely good for the beginners.. It would be better if you add somemore links like
1.Understanding the Os versions in BBB
2. OpenCV installations in offline mode(BBB without Internet connectivity)
Thanking You
Sir,
I am getting an error “select timeout” while running capture script. I am using a different camera with pixel format of “YUYV” and I have changed the force format option to “-f” according to that but the error is not going away. There is some difference in the drivers of camera also. Following are the full details of camera.
root@beaglebone:~/boneCV# v4l2-ctl –all
Driver Info (not using libv4l2):
Driver name : ov534
Card type : USB Camera-B4.04.27.1
Bus info : usb-musb-hdrc.1.auto-1.1
Driver version: 3.8.13
Capabilities : 0x85000001
Video Capture
Read/Write
Streaming
Device Capabilities
Device Caps : 0x05000001
Video Capture
Read/Write
Streaming
Priority: 2
Video input : 0 (ov534: ok)
Format Video Capture:
Width/Height : 640/480
Pixel Format : ‘YUYV’
Field : None
Bytes per Line: 1280
Size Image : 614400
Colorspace : SRGB
Streaming Parameters Video Capture:
Capabilities : timeperframe
Frames per second: 30.000 (30/1)
Read buffers : 2
User Controls
brightness (int) : min=0 max=255 step=1 default=0 value=0 flags=slider
contrast (int) : min=0 max=255 step=1 default=32 value=32 flags=slider
saturation (int) : min=0 max=255 step=1 default=64 value=64 flags=slider
hue (int) : min=-90 max=90 step=1 default=0 value=0 flags=slider
white_balance_automatic (bool) : default=1 value=1
exposure (int) : min=0 max=255 step=1 default=120 value=120 flags=inactive, volatile
gain_automatic (bool) : default=1 value=1 flags=update
gain (int) : min=0 max=63 step=1 default=20 value=20 flags=inactive, volatile
horizontal_flip (bool) : default=0 value=0
vertical_flip (bool) : default=0 value=0
power_line_frequency (menu) : min=0 max=1 default=0 value=0
sharpness (int) : min=0 max=63 step=1 default=0 value=0 flags=slider
Camera Controls
auto_exposure (menu) : min=0 max=1 default=0 value=0 flags=update
Hi Derek,
Very interesting work ! Please, which Linux kernel version you work with ?
Sorry, forgot my comment please
I’m also getting the ‘select timeout’ message when attempting to use the ./capture command line utility.
I’ve used both the built version and also re-building it on my BBB.
I looked at the code and it appears that it is doing some math to get the value of r, and I cannot figure out how to get around it.
My device attached is a YUYV camera device.
Help is much appreciated to get around this error.
Thanks
Ben
Hi Derek.
firsly thank you very much. it was really useful for me.
but i have a doubt
if executing any of the programs, the answer is
cssp-camera 18000000.camera: dma_alloc_coherent of size 0 failed
VIDIOC_REQBUFS error 12, Cannot allocate memory
could you help me? THANK YOU
Hi Derek,
I am using BBB Angstrom 3.8.13. When I tried to execute above programs there was no error in compilation.But when I run the output file i get the following error:
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
select timeout
I searched about it on web but I am not able to figure out why am I getting error?
Could you please help me out.
THANK YOU
Hi Derek,
Thanks for your wonderful work and guidance.
I have a question here.
Could we put some texts or overlay some text on the video when v4l2 capture the real video? Like your code capture.c?
Thanks very much.
Yang
hi Derek,
I got invalid argument and select timeout error.By rmmod i was able to finish select timeout error though invalid argument error still is present. Your help is much needed.
Hi Derek,
I followed your tutorials on grabbing picture frames from a webcam and also the one of cross-compiling, and they were indeed inspiring. I used my beaglebone to do the same, and it worked fine. But, I tried cross-compiling it on Eclipse IDE using arm-linux-gnueabi toolchain, it displayed errors on all the methods the contain “v4l2_”. For example: v4l2_open(), v4l2_mman(), v4l2_munmap(), v4l2_mmap(), v4l2_close() and v4l2_ioctl(). I later on changed the toolchain and used arm-angstrom-linux-gnueabi – hoping that it would solve the problem, since beaglebone is running angstrom linux. The IDE displayed errors that all the afore-mentioned methods were not defined. I would like to ask: were these errors due to the fact that these toolchains don’t have the library for these methods or the actual path for the library files were not properly used by the cross-compiler? I tried running it as a C++ program and also as a C program, I had the same errors.
I wrote other programs that did work fine. For example, the beaglebone is connected with a PIR sensor and a LED. When the sensor detects movement the LED goes on and if there is no movement after few seconds it goes off. This did work fine with the cross-compiler, and the program was written in C++.
Derek,
I’m having some issues with the bitrate, I believe, while running avconv since ffmpeg is being phased out on Debian (so I’ve heard). The issue is when I convert the .raw to .mp4, the speed seems to be doubled. I also noticed that my bitrate is almost double what yours shows in the video. I’m using the same C920 camera and BBB with Debian. How can I fix this and what might be causing it?
Here is my output:
debian@beaglebone:~/boneCV$ ./capture -F -c 300 -o > output.raw
Force Format 2
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
debian@beaglebone:~/boneCV$ avconv -f h264 -i output.raw -vcodec copy output.mp4
avconv version 0.8.17-6:0.8.17-1, Copyright (c) 2000-2014 the Libav developers
built on Mar 15 2015 18:02:14 with gcc 4.6.3
[h264 @ 0x40b00] max_analyze_duration reached
[h264 @ 0x40b00] Estimating duration from bitrate, this may be inaccurate
Input #0, h264, from ‘output.raw’:
Duration: N/A, bitrate: N/A
Stream #0.0: Video: h264 (Constrained Baseline), yuvj420p, 1920×1080 [PAR 1:1 DAR 16:9], 25 fps, 25 tbr, 1200k tbn, 60 tbc
File ‘output.mp4’ already exists. Overwrite ? [y/N] y
Output #0, mp4, to ‘output.mp4’:
Metadata:
encoder : Lavf53.21.1
Stream #0.0: Video: libx264, yuvj420p, 1920×1080 [PAR 1:1 DAR 16:9], q=2-31, 1200k tbn, 1200k tbc
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press ctrl-c to stop encoding
frame= 300 fps= 0 q=-1.0 Lsize= 7159kB time=9.97 bitrate=5884.3kbits/s
video:7154kB audio:0kB global headers:0kB muxing overhead 0.070055%
debian@beaglebone:~/boneCV$
I love Derek’s videos and have learned so much in so short a time. I’ve been battling a problem with grabber. Build went fine. Command line execution was easy. Conversion was easy. I can scp the files to my mac and look at them, awesome! So I went ahead and wrote a script that checks a value in a gpio folder to see if the PIR has been tripped and then execute grabber. Then it changes the file into a .jpg and transfers it to my mac. Works for hours, sometimes lots of hours, and then goes into a loop. I come home after work and find grabber on “top” in a tight loop. If I kill it, the script continues. Sometimes my device,
Cased:~# lsusb
Bus 001 Device 002: ID 1a40:0101 Terminus Technology Inc. 4-Port HUB
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 007: ID 046d:082d Logitech, Inc.
Bus 001 Device 004: ID 1a40:0101 Terminus Technology Inc. 4-Port HUB
Bus 001 Device 005: ID 0846:9030 NetGear, Inc. WNA1100 Wireless-N 150 [Atheros AR9271]
no longer shows up. My next step may be to look for an upgraded version of grabber, or maybe work capture into the home invasion detection software. Actually to date I have been merely taking pictures of my parrots.
If I come to Ireland, will you have a Guiness with me? It’s on my bucket list.
Hi! I m facing a problem in executing grabber, i keep waiting for a long time then no answer so i stop it with crtl C , and then i get a destroyed image
Do you have any solution for that?
Thnx!