20th
09,07
Introduction
The OpenCV is one of the greates and widely using image library for all developers and students. However, I am not a OpenCC and Graphics guy, but I choose OpenCV for Video Recording purpose. Since I mostly live and develop within Ubuntu, I tried writing and testing a little python-opencv program, but not working. It is mainly because the problem of new ffmpeg in ubuntu 9.04 Jaunty – see, https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/311188. While searching the Internet, I found an article to use video streaming of opencv(here, http://gijs.pythonic.nl/blog/2009/may/3/getting-video-io-working-opencv-and-ubuntu-jaunty-/). I tired because I don’t want to reinvent the wheel of course, but not working like many comments on ‘cvCreateVideoWriter‘ function.
Prepare to compile
Prepare to build
1 2 | sudo apt-get build-dep python-opencv sudo apt-get install libswscale-dev swig |
Prepare to configure
Link avcodec.h, avformat.h, avio.h, avutil.h and swscale.h in the /usr/include/ffmpeg (you have to create) like below.
1 2 3 4 5 6 | ls /usr/include/ffmpeg avcodec.h -> ../libavcodec/avcodec.h avformat.h -> ../libavformat/avformat.h avio.h -> ../libavformat/avio.h avutil.h -> ../libavutil/avutil.h swscale.h -> ../libswscale/swscale.h |
Checkout OpenCV
1 | svn co https://opencvlibrary.svn.sourceforge.net/svnroot/opencvlibrary/tags/latest_tested_snapshot |
Configure & Make
1 2 | cd latest_tested_snapshot/opencv ./configure --enable-shared --enable-swscale --enable-gpl --with-swig |
- –enabled-shared : as shared library
- –enable-swscale : alternative work around for ffmpeg
- –enable-gpl : opensource library
- –with-swig : regenerate swig interfaces
... Video I/O --------------------- Use QuickTime / Mac OS X: no Use xine: no Use gstreamer: no Use ffmpeg: yes Use dc1394 & raw1394: no Use v4l: yes Use v4l2: yes Use unicap: no Wrappers for other languages ======================== SWIG /usr/bin/swig -c++ Python yes Octave no ...
The highlighted configuration items are important, ffmpeg / swig / python. If everything is find, do make. The interface files in the checkout directory are not properly generated, so you have to generate yourself.
1 2 | make sudo make install |
Test and Correct OpenCV Python files
1 2 3 4 5 6 7 8 9 10 11 | $ cd /usr/local/lib/python2.6/site-packages/opencv $ python Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for ... >>> import cv.py Traceback (most recent call last): File "<stdin>", line 1, in <module> File "cv.py", line 8473, in <module> CV_SEQ_CONTOUR=CV_SEQ_POLYGON NameError: name 'CV_SEQ_POLYGON' is not defined |
Open cv.py and Edit
1 | CV_SEQ_CONTOUR=CV_SEQ_POLYGON |
(line 8473) to
1 | CV_SEQ_CONTOUR=CV_SEQ_POLYGON_TREE |
Everything is done.
Example of OpenCV and PyGame
Displaying two Video Cameras as one concatenated video and record it to a file.


To make my program independent, I just copied the opencv python binding to the local directory.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import sys import pygame import Image import opencv from PIL import Image from pygame.locals import * from opencv import highgui from opencv import cv def get_image( camera1, camera2 ) : im1 = highgui.cvQueryFrame( camera1 ) im2 = highgui.cvQueryFrame( camera2 ) pil1 = opencv.adaptors.Ipl2PIL( im1 ) pil2 = opencv.adaptors.Ipl2PIL( im2 ) img = Image.new( "RGB", (pil1.size[0] + pil2.size[0], pil1.size[1]) ) img.paste( pil1, (0,0,pil1.size[0],pil1.size[1]) ) img.paste( pil2, (pil1.size[0],0,pil1.size[0]+pil2.size[0], pil2.size[1] ) ) return img def CV_FOURCC( c1, c2, c3, c4 ) : return (((ord(c1))&255) \ + (((ord(c2))&255)<<8) \ + (((ord(c3))&255)<<16) \ + (((ord(c4))&255)<<24)) if __name__ == "__main__" : camera1 = highgui.cvCreateCameraCapture( 0 ) camera2 = highgui.cvCreateCameraCapture( 1 ) ( width, height ) = ( 640, 480 ) fps = 30.0 pygame.init() window = pygame.display.set_mode( ( width * 2, height ) ) pygame.display.set_caption( "WebCam Demo" ) screen = pygame.display.get_surface() writer = highgui.cvCreateVideoWriter( "captured.avi", CV_FOURCC('M','J','P','G'), fps, cv.cvSize(width * 2, height) ) if not writer : print "Error on writer" exit(1) on_exit = False while not on_exit : events = pygame.event.get() for event in events: if event.type == QUIT or event.type == KEYDOWN: on_exit = True im = get_image( camera1, camera2 ) highgui.cvWriteFrame( writer, opencv.adaptors.PIL2Ipl(im) ) pg_img = pygame.image.frombuffer( im.tostring(), im.size, im.mode ) screen.blit( pg_img, (0,0) ) pygame.display.flip() pygame.time.delay( int( 1000 * 1.0 / fps ) ) |
1 2 3 | python webcam.py Output #0, avi, to 'captured.avi': Stream #0.0: Video: mjpeg, yuvj420p, 1280x480, q=2-31, 78643 kb/s, 90k tbn, 30 tbc |
Reference
- https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/311188\
- http://ubuntuforums.org/showthread.php?p=7315433
- http://code.google.com/p/ctypes-opencv/source/checkout
- http://gijs.pythonic.nl/blog/2009/may/3/getting-video-io-working-opencv-and-ubuntu-jaunty-/
- http://www.pythonware.com/products/pil/
Disclamer
I copied all of ideas from the Internet and just do it myself for my purpose. However, if you want, feel free to use above example under GPL v.2. Thanks.
10 11th, 2009 at 1:17 pm
Thank you for the guide. It is great!
It works initially but it stops working two days later. I think maybe because I installed gstreamer-ffmpeg. But it still does not work after I uninstalled the package. I got the following error.
ALSA lib confmisc.c:768:(parse_card) cannot find card ‘0′
ALSA lib conf.c:3513:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:3513:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:3513:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:3985:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2205:(snd_pcm_open_noupdate) Unknown PCM default
Output #0, avi, to ‘capture2.avi’:
Stream #0.0: Video: mjpeg, xvmcmc, 640×480, q=2-31, 39321 kb/s, 90k tbn, 30 tbc
[mjpeg @ 0x26e04d0]colorspace not supported in jpeg
Traceback (most recent call last):
File “./webcam1.py”, line 70, in
cv.cvSize(width, height) )
File “/usr/local/lib/python2.6/dist-packages/opencv/highgui.py”, line 303, in cvCreateVideoWriter
return _highgui.cvCreateVideoWriter(*args)
RuntimeError: openCV Error:
Status=Bad argument
function name=CvVideoWriter_FFMPEG::open
error message=Could not open codec ‘mjpeg’: Unspecified error
file_name=highgui/cvcap_ffmpeg.cpp
line=1302
Do you have any idea what may go wrong?
10 11th, 2009 at 5:31 pm
Could you check your “CV_FOURCC(’M',’J',’P',’G')” and try different codecs. The error msg said you don’t have mjpeg anymore. It should work, good luck!
10 11th, 2009 at 10:17 pm
Hi Taesoo Kim;
I’m a bit buffled at the Step 2
Prepare to configure
Link avcodec.h, avformat.h, avio.h, avutil.h and swscale.h in the /usr/include/ffmpeg (you have to create) like below.
Could you please tell me what commands I should run?
thanks
Hsu
10 11th, 2009 at 11:16 pm
Just ignore the question above.. I’ve got it working! thanks so much
10 12th, 2009 at 8:55 am
MPEG1 (PIC1) works. Now I need to figure out why mjpeg dies… Thanks much for the reply!
10 12th, 2009 at 12:13 pm
I am happy that you are fine : )
03 19th, 2010 at 11:03 pm
Hey !
Nice topic
If anyone search for an example with OpenCV and C++ there it is : http://www.geckogeek.fr/lire-le-flux-dune-webcam-camera-video-avec-opencv.html.
Good Week !