Subscribe:

Ads 468x60px

Sunday, March 3, 2013

How to easily install OpenCV on Raspberry Pi

Hi all

      Building opencv source code in Raspberry Pi will take about 6-7 hours.
Here is the link to install opencv from source

Easy method to install opencv

1)Connect to Raspberry Pi using ssh. Fig shows an example of connecting Rpi using ssh




2)Install synaptic package manager from command line of raspberry pi


3)Install tight-vnc server from command line


4)Start a vnc server in Rpi for viewing the screen and detection window from opencv

Enter the command to start a vnc-server on Rpi

vncserver :1 -geometry 1366x600 -depth 16 -pixelformat rgb565

#New ‘X’ desktop is raspberrypi:1


5)Start vnc client from PC

eg: vncviewer 192.168.1.3:5901
eg:vncviewer rpi_ip:5901


6)Start synaptic from root terminal like this 


7)Install opencv from synaptic like this



8)Download the source code from below


9)Copy files from PC to Rpi using scp command

scp -r facedetect.py face.xml pi@192.168.1.3:"/home/pi"


10)Run python file using following command

python facedetect.py --cascade=face.xml 0


Output
--------


Video:


77 comments:

Anonymous said...

Why don't you just apt-get it? Surely that would be way faster then installing VNC for no reason...

sudo apt-get install *opencv*

Done.

Lentin Joseph said...

Yes ,ofcourse. It can be done in both ways. I did it in this way to showcase the desktop and make a feel that it is running in Rpi. :)

It will perform better if you run from ssh terminal

Lentin Joseph said...

I think this post cover most of commands for interacting with Rpi, So this will a good tutorial for beginers.

Arun Gopal said...

which type of camera are u used?

Arun Gopal said...

are you done same process in java and cross compilation of raspberry pi.and please give the source code for raspberry pi face detection .

Lentin Joseph said...

Hi Arun

I am using I ball face2facecam. I am not using java and all. I included the source code in the post itself.

http://www.mediafire.com/?ahd677sxyfpf1m7

Christopher Hills said...

When I used opencv on the Pi I just ran sudo apt-get install opencv-python and did a whole host of things. Check out my blog post http://orictosh.blogspot.co.uk/?m=1

Christopher Hills said...

Correction on that package name should be python-opencv

Arun Gopal said...

Hi,

i saw the opencv with eye and mouth and nose detection.iam download u r given source code and supported file.how to compile that face detection in the raspberry pi.please give me some steps how send this material on raspberry pi and how to compile that?iam send source code through scp command.is this ryt procedure?please clarify me.


Thanks&Regards,
K.Arungopal

Lentin Joseph said...

Hi Arun,

There is no change in the compilation procedure of opencv, its same as the pc, because we are working on OS layer, so command will be same.
What matters is processing power. When i tried the facedetection code, i scaled the image into 320x240. So what you have to do is scale image before processing. If you run face,nose and eye detection, it will take good amount of processing power, just rescale image into 320x240 and try, i have a doubt whether it work or not in RasPi.

Arun Gopal said...

ok thanks joseph.i want to compile opencv in raspberry pi.do u know about this?are u haveing any source code?presently iam working on this board for opencv face and eye and mouth detection.iam compiled u r code in raspi that is face detection only with rectangular.it is working cool.thanks.....when iam run the ./video it show s the error.please help me how to comipile that programe.



Thanks&Regards,
K.Arungopal

Arun Gopal said...

i have one dought.in python face detection is done in ractangular face using same code it is possible to get the mouth and eyes and nose detection.it is possible?if possible what changes iam done?i mean adding of extra xml file sin programe.how to add xml files in programe.



Thanks&Regards,

K.Arungopal

Anonymous said...

Hi, I think that is usefull show the list of packages that you installed on raspbian throught synaptic, while tha packages are installable throught apt, if the list is complete, someone could install opencv with terminal instead that on X or compile the library (lot of time).

Arun Gopal said...

Hi joseph thanks for the reply...what is my question is on the raspberry i want do the face detection?so please help me for giving any idea about that source code and xml files.....

Thanks&Regards,
K.Arungopal

Anonymous said...

hello joseph,i ran your face detection code on raspberrypi,but i am getting detection time in between 600 and 900 ms,your is 200 ms.i am using intex webcam.
Thanks&Regards,
Abhilash

Lentin Joseph said...

Hi

Did you try it without vnc_server?? And what about your webcam resolution, Is it 640x480?

Anonymous said...

hi,if i only use ssh,then its giving gtk error.should i modify the program?my webcam resolution is 640x480.
-abhilash

Arun Gopal said...

Hi joseph and anonymous....

first in raspberry pi install vncserver...for that open a terminal and type sudo apt-get install toghtvncserver....and open a host terminal and type sudo apt-get install xvnc4viewer....after installation in host type open a terminal and type in host vncviewer...type u r ip address and enter and it asks password and enter it shows the raspberry pi gui screen.....



Thanks&Regards,
K.Arungopal

Anonymous said...

I get an error message when I put this in the terminal ...

4)Start a vnc server in Rpi for viewing the screen and detection window from opencv

Enter the command to start a vnc-server on Rpi

vncserver :1 -geometry 1366x600 -depth 16 -pixelformat rgb565

#New ‘X’ desktop is raspberrypi:1

Lentin Joseph said...

Did you install tightvnc package in Raspberry Pi ? You need to install it first.

Anonymous said...

OK I installed it now. And got the vncserver running.

Now I cant get further at step 5).

I have a mac. How can I start the vncviewer there?

Anonymous said...

I got it already I installed VNC Viewer for Mac in the end and I hat to put the IP Adress like this in there 192.168.178:1.

Also OpenVC is working now. I had to kill motion first.

Thanks for the great tutorial!

Varun hebbar said...

how to transfer facedetect.py file from windows xp to raspberry pi vis ssh?

scp is not working..

Lentin Joseph said...

Try this

http://winscp.net/eng/index.php

Varun hebbar said...

thanx....

now i got another problem...
installed opencv from synaptic...and installed packages....
and i saved facedetect.py and face.xml in my gmail draft...then i saved both from raspberry pi platform(using midori browser)...
when i type this

python facedetect.py --cascade=face.xml 0

it gives the error

no module named cv2.cv in line 10....

please help...

Arun Gopal said...

Hi varun..iam using this blog..its absolutely work on raspberry pi..First u have to install simplecv in raspberry pi..http://www.internetdelascosas.cl/2012/12/06/instalando-bibliotecas-en-python/...Just follow this link..install python on raspberry pi bacause code in python..and for installing opencv in raspberry pi

sudo apt-get install libcv-dev
sudo apt-get install python-opencv
sudo apt-get install libopencv-dev
sudo apt-get install libcv2.3
sudo apt-get install libopencv-doc

Thats it u r opencv is installing on raspberry pi..its is very simple to use..try this sure u get output..xml and code files is send through only scp command only..otherwise it can not works.


Thanks&Regards,
K.Arungopal

Varun hebbar said...

after installing the above packages....there is no space left in pi....

what to do now?

showin no space left error....
please help

im using type B rpi....comes with 512MB RAM..

Lentin Joseph said...

@Varun ,I am using 32GB SD card, may be you need to expand the SD Card size,

"no space left error" is only due to SD card not due to RAM !!

Varun hebbar said...

ok im using 4gb card...

but i have a doubt...after plugin out the card from rpi and connecting to pc using card reader it shows
only 58MB used...

hazyy said...

Hi, thank you for this tutorial. I'm following the steps from terminal in my macbook. i reached this point and got:

pi@raspberrypi ~ $ vncviewer 102.168.2.2:5901
-bash: vncviewer: command not found

:( i don't know what to do here. can you please help me?

Lentin Joseph said...

Hi ,You need to run vncviewer from client system ,ie ubuntu. For getting vncviewer command in client system, you have to install xvnc4viewer from synaptic.

hazyy said...
This comment has been removed by the author.
Anonymous said...

Hi, after running the command
root@PIWEB:/OpenCV# python facedetect.py --cascade=face.xml 0

i got the following error:

(result:10414): Gtk-WARNING **: cannot open display: localhost:10.0

what is wrong there???

Thx

AV Exporters said...

i was able to get the cam working on raspberry using the above method but how to power the servos as they ar enot moving if i power them from raspberry.How you used external power for servos ?

Thanks

Lentin Joseph said...

@AV Exporters

Servos will consume good amount of current, so power to raspberry pi must have suffficient amount of current sourcing capacity. I think 5V,2A power supply will work fine with one or two servos

AV Exporters said...

I am using my Note 2 adapter to power raspberry.None of the servos move and now my raspberry just hangs after detecting face for the first time

Anonymous said...

Got it all working.Servos work and detect face but then the tilt servo just stuck at 180 degree any ideas

Anonymous said...

Hi guys...i have Rpi and laptop with Ubuntu OS...so i want to run video via USB cam using by Rbi...please tel me step by step procedure...

Thanks

regards,
Dhanash.

Anonymous said...

hi good eve...i installed Raspbian in Rpi...now i want to install opencv c++ on Rpi...how to install.. help me guys....

thankyou

regards
prakash

Lentin Joseph said...

Hi Dhanash

You can view usb cam using this following tool called camorama

$sudo apt-get update
$sudo apt-get install camorama

Lentin Joseph said...

Hi Prakash

This post also gives idea about the installation of opencv c++.

Andres Burbano said...

Hi,

I did the full steps of the tutorial, but I have a problem running the program.

I plug 3 different cameras and I also install camorama, but all of them give me this error:

$ python facedetect.py --cascade=face.xml 0
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
libv4l2: error allocating conversion buffer
mmap: Cannot allocate memory
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
libv4l1: error allocating v4l1 buffer: Cannot allocate memory
HIGHGUI ERROR: V4L: Mapping Memmory from video source error: Invalid argument

The only one that do not show this error is the Asus Xtion but it wont shows anything!!

regards

Andres

Kevin Domadia said...

Hey Lentin!
I am using eye blink detection using opencv on raspberry pi. It is working fine but it lags a lot. It might be probably due to large image resolution. Is it possible to change the image resolution and how?
Please help!
Thanks !

Lentin Joseph said...

Hi Kevin

I think its version 2.4.3 or 2.4.5

Lentin Joseph said...

Andreas

I couldnt find the reason, its not the problem of code i think. Its a bug found in low virtual memory. What is the size of memory card you using?

Kevin Domadia said...

Hey Lentin,
Actually my question was different...:D ... I am using your eye blink detection code on my raspberry pi... It is working fine but it lags a lot. It might be probably due to large image resolution. Is it possible to change the image resolution and how?
Please help!

Lentin Joseph said...

Hi Kevin

Change the line number 77 and 78. This indicate the resolution. Reduce the resolution to 160 x 120.

Kevin Domadia said...
This comment has been removed by the author.
Kevin Domadia said...

Hi Lentin,
you are talking about these two commands right?
cv::Mat img_display;
roiImg.copyTo( img_display );
Thanks!

Andres Burbano said...

im using 512

Lentin Joseph said...

Hi Kevin

I mean the following lines


width = 320 #leave None for auto-detection
height = 240 #leave None for auto-detection

change into


width = 160 #leave None for auto-detection
height = 120 #leave None for auto-detection

Lentin Joseph said...

Hi Andreas

Are you using 512 mb sdcard? Try to upgrade it into 4gb or 8 gb. May be you can solve this issue

Kevin Domadia said...

Hi Lentin!, actually I was talking about eye blink detection not about face detection:)..So please tell me what changes do I have to make in eye blink detection code?

Lentin Joseph said...

Hi Kevin
Sorry for the carelessness :).Eye blink detection will take good system resource so i dont suggest you to run it in Rpi.

Currently i didnt put code to reduce resolution because it was tested in a pc. But you can make a try in Rpi with reduced resolution. You can do this by adding 2 more lines below the cvCaptureFromCAM(0) as show below. Make a try and please tell me if it is working in Rpi or not.

capture = cvCaptureFromCAM( 0);

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 320);

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 240 );

Andres Burbano said...

Hi Lentin,

im using now 16gb and still not working!!! any Idea?

Andres

Lentin Joseph said...

Hi Andres

I think its because of driver fault. My web cam support uvc driver and it displays video stream.

I searched in internet about this issue, but it remains unresolved. For debugging this issue please mail me the screenshot of 'dmesg' and 'lsusb' to lentin007@gmail.com

Jiwan Neupane said...

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog,
looking forward for more contents...Great job, keep it up.
stream host.

John Seo said...

Hi Lentin

The post is really good for me.
Actually, I run a python file and display is shown, ok.
But there is no red rectangular which is not shown.
And i want to change code for showing a position that is face location. How should i do ?

Lentin Joseph said...

Hi John

Rectangular only appears when there is a face detection.. The detection depends upon lighting conditions and all.. you can print the face location too ..

pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
You can print pt1 and pt2 ..Avg of pt1 and pt2 gives center .. Pt1 and pt2 contain x,y of the rectangle

Kamil Molendys said...

Hi, I'm using the raspberry camera. Now, when I start the script I have a blank window (result). Can you help me? This is a screenshot: https://dl.dropboxusercontent.com/u/22330630/DSC_0869.jpg P.S. I want to precise that the cam works when I use raspistill

Lentin Joseph said...

Hi Kamil
I think rpi cam is not support in opencv now. I was using a usb webcam for this project.

Kamil Molendys said...

Thanks for the response and compliments for your blog ;)

Kamil Molendys said...

I found a solution installing UV4L > http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=16 ;)

sagar said...

im developing driver profile authentication system in car ..for that face recognition is needed .. can this raspberry pi+camera work like a standalone system for recognition. Or system interfacing is mandatory??
thanks in advance..

Anonymous said...

To install OpenCV using the terminal on Ubuntu:
$ su -
# apt-get update
# apt-get install build-essential
# apt-get install libavformat-dev
# apt-get install x264 v4l-utils ffmpeg
# apt-get install libcv2.3 libcvaux2.3 libhighgui2.3 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev

OpenCV should be installed in /usr/local/ Installation Directory

You may also want to compile and view examples
$ cp -r /usr/share/doc/opencv-doc/examples .
$ cd examples
$ cd c
$ sh build_all.sh

Source: http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html

Richard Tenorio said...

hi Lentin,

I got the error below when i run your code. pls help me. The error showed like this:

HIGHGUI ERROR: V4L/V4L2: VIDIOC_CROPCAP
/dev/video0 does not support memory mapping
VIDIOC_QBUF: Inappropriate ioctl for device

By the way, I am using raspi camera and model B raspberry pi with 32GB memory card. Thanks

Lentin Joseph said...

Hi Richard

Actually this example is working with ordinary webcam. If you are using Rpi camera, you can follow this link for setting opencv

http://thinkrpi.wordpress.com/2013/05/22/opencv-and-camera-board-csi/

Richard Tenorio said...

Hi Lentin,

Is your tutorial compatible to any versions of OpenCV? Can i get your e-mail address so i can give you my code for you to test in your own raspberry pi and see the actual problem that i am experiencing??Thanks lentin.

Lentin Joseph said...

Hi Richard
My email id is lentin007@gmail.com. May be you can send me the error you getting, That will be enough for debugging.

Anonymous said...

Hi Lentin,
I got an error in line 63
cascade=cv.load(options.cascade)
TypeError: OpenCV returned NULL
could you please help me in this?

Lentin Joseph said...

Hi,
You have to include cascade file during the execution of this code like this

python facedetect.py --cascade=face.xml 0. Where facedetect.py is the python file, face.xml is the cascade file and 0 is the camera index

Richard Tenorio said...

Hi Lentin,

Are u familiar with auto running of python script during boot process in raspberry pi? Thanks

Gopi Raj Mamidi said...

Hey.. Getting this error for your sample face detect example.
running Rpi Raspbian using SSH ubuntu 12.04:

VIDIOC_QUERYMENU: INVALID ARGUMENT
libv4l2:error setting pixel format:device busy
extension Randr missing on display :0

Lentin Joseph said...

Hi @Richard
Following link shows how we can run a python script during booting

http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/

DailyDols said...

Did you find a way to cross-compile OpenCV for RasPi, e.g. from a Ubuntu system?
I'm stuck with cmake, because it checks available ressources on Ubuntu instead of the Pi, even if I set the pathes correctly.

Lentin Joseph said...

You can follow this link to setup raspicam

http://www.uco.es/investiga/grupos/ava/node/40

supra said...

How do u access xml file or download xml file?
The file I was going to downloaded face.xml.

Post a Comment