Dec 25, 2013

Raspberry Pi - OpenCV with Picam (final)



5. implement OpenCV


Finally this step is final!!

5-1 Prepare camcv

Download  camcv_vid1.cpp 


Modify cmakelists.txt


sudo nano cmakelists.txt


Change camcv.c to camcv.vid1.cpp in add_executable





Modify camcv_vid1.cpp

sudo nano camcv_vid1.cpp


Line 59 (face recognition API)

----------------------------------------------------------------------------------------------
#include "/home/pi/pierre/libfacerec-0.04/include/facerec.hpp"
=>
#include "/home/pi/camcv/libfacerec/include/facerec.hpp"
----------------------------------------------------------------------------------------------


Line 681 (openCV lib)

---------------------------------------------------------------------------------------------------------------------------
fn_haar = "/usr/share/opencv/haarcascades/lbpcascade_frontalface.xml";
=>
fn_haar = "/usr/share/opencv/haarcascades/haarcascade_frontalface_default"; 
--------------------------------------------------------------------------------------------------------------------------


Line 764 (If you meet error last step)

--------------------------------------------------------------------------------------------------------------------------
else if (!raspipreview_create(&state.preview_parameters))
=>
else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) 
--------------------------------------------------------------------------------------------------------------------------

this error
---------------------------------------------------------------------------------------------
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates

---------------------------------------------------------------------------------------------



If you not have haarcascades folder in /usr/share/opencv/  need a download opencv2.3.1


Click to link OpenCV_2.3.1 

Here Change new link

Unzip and  copy /opencv_2.3.1/data/haarcascades/ to /usr/share/opencv/haarcascades/ 



5-2 make DB

Make DB or download sample


sample file  << Click to link for download sample file


Unzip and copy to /home/pi/camcv/others 





Include 400 pictures and 1 text file.

One person has 10 pictures.

ID LIST (csv.txt) file for link person and picture.




Last number mean ID




5-3 Modify camcv_vid1.cpp

sudo nano camcv_vid1.cpp


Line 65

Define name and id

id is csv.txt' id number.



Line 661

This is name for display!! 

PS. many picture make high accuracy but slow run time.



5-4 Run

sudo cmake .


sudo make


You can see change camcv_vid1.cpp instead of camcv.c

Run!!

./camcv others/csv.txt l 5500



init person list


If recognize your listed person, display name.


Dec 24, 2013

Raspberry Pi - OpenCV with Picam (4)


4. OpenCV using raspivid


Now we'll use raspivid.

http://thinkrpi.wordpress.com/2013/05/22/opencvpi-cam-step-6-video/ 

I refer to this blog step6


4-1 Modify raspivid.c 

Modify raspivid.c or click to link for download.

camcv_vid0.c

------------------------------raspivid.c--------------------------------------------------------------------------

We  delete- all lines related to the preview component,
- all lines related to the encoder component.
- all lines related to inline command parsing and picture info…
We change :
- add the callback directly to the video_port  (line 286)
- create and attach the pool (to get/send message)  to the video port… (line 320)
- change format encoding to ENCODING_I420 in line (268) (instead of OPAQUE)
---------------------------------------------------------------------------------------------------------------------
change name to camcv.c
mv camcv_vid0.c camcv.c
make

./camcv
If you meet this error
---------------------------------------------------------------------------------------------
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates

---------------------------------------------------------------------------------------------

change Line 489

nano camcv.c
--------------------------------------------------------------------------------------------------------------------------
else if (!raspipreview_create(&state.preview_parameters))
=> 

else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS)
--------------------------------------------------------------------------------------------------------------------------

Re cmake
very slowly but recoding vidio

If you change resolution

Modify Line 183

and 

color<--> gray change

Line 124

Reference my case.

  • 320×240 color : FPS = 27,2
  • 320×240 gray : FPS = 28,6
  • 640×480 color : FPS = 8
  • 640×480 gray : FPS = 17

Raspberry Pi - OpenCV with Picam (3) -add(install face recognition)



3. Apply OpenCV


3-1 Install GTK + UVC viewer

sudo apt-get install guvcview





Install completed then setting.

sudo usermod -a -G video pi


sudo modprobe uvcvideo

3-2 Install OpenCV library


sudo apt-get install libopencv-dev



very very long time~


3-3 Install Python-Opencv
sudo apt-get install python-opencv



3-4 Install face recognition


face recognition API  <<< click to link then download API

Unzip to home/pi/  

You check bytefish-libfacerec folder

cd bytefish-libfacerec

sudo cmake .





sudo make




Check creation file libopencv_facerec.a 

copy to /home/pi/camcv/libfacerec/ 


mkdir /home/pi/camcv/libfacerec

cd /home/pi/camcv/libfacerec

cp -r /home/pi/bytefish-libfacerec/* .

cd..

3-5 Add cmakelists.txt 

nano cmakelists.txt

------------------------------------------------cmakelists.txt--------------------------------------------------------
cmake_minimum_required(VERSION 2.8)
 project(camcv)
 SET(COMPILE_DEFINITIONS -Werror)

#OPENCV
find_package( OpenCV REQUIRED )

#except if you’re pierre, change the folder where you installed libfacerec
#optional, only if you want to go till step 6 : face recognition
link_directories( /home/pi/camcv/libfacerec)

 include_directories(/opt/vc/include)
 include_directories(/opt/vc/include/interface/vcos)
 include_directories(/opt/vc/include/interface/vcos/pthreads)
 include_directories(/opt/vc/include/interface/vmcs_host/linux)
 include_directories(.)

 include_directories(/opt/vc/userland/host_applications/linux/libs/bcm_host/include)
 include_directories(/opt/vc/userland/interface/vcos)
 include_directories(/opt/vc/userland)
 include_directories(/opt/vc/userland/interface/vcos/pthreads)
 include_directories(/opt/vc/userland/interface/vmcs_host/linux)
 include_directories(/opt/vc/userland/interface/khronos/include)
 include_directories(/opt/vc/userland/interface/khronos/common)
 include_directories(/home/pi/camcv/)
 include_directories(/home/pi/camcv/gl_scenes/)
 include_directories(/home/pi/camcv/libfacerec/include/) 
add_executable(camcv RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv.c RaspiTex.c RaspiTexUtil.c gl_scenes/teapot.c gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/sobel.c gl_scenes/yuv.c tga.c )



target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so /home/pi/camcv/libfacerec/libopencv_facerec.a ${OpenCV_LIBS})
--------------------------------------------------------------------------------------------------------------------------

Red color is add code.


3-6 modify camcv.c

sudo nano camcv.c

Modify below code or click to link for download camcv.c 

download camcv.c
-----------------------------------------------camcv.c----------------------------------------------------------------
Lines 61+ : add OpenCV Includes
// *** PR : ADDED for OPENCV
#include <cv.h>#include <highgui.h>
Line 156 : modify init values for test (size of file)// *** PR : modif for demo purpose : smaller imagestate->timeout = 1000; // 5s delay before take imagestate->width = 320;//2592;state->height = 200; //1944;
Line 230+ : in static void encoder_buffer_callback function. This is the core of the modification. This function is a callback, call to get the image in the queue. buffer contains the picture from the camera.
// *** PR : OPEN CV Stuff here !
// create a CvMat empty structure, with size of the buffer.CvMat* buf = cvCreateMat(1,buffer->length,CV_8UC1);
// copy buffer from cam to CvMat
buf->data.ptr = buffer->data;

// decode image (interpret jpg)IplImage *img = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR);
// we can save it !
cvSaveImage(“foobar.bmp”, img,0);// or display itcvNamedWindow(“camcvWin”, CV_WINDOW_AUTOSIZE);cvShowImage(“camcvWin”, img );cvWaitKey(0);

Line 711/726/823 : we remove the native preview window (replaced by opencv window)// *** PR : we don’t want preview
camera_preview_port = NULL;

// PR : we don’t want preview
// status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection);

// mmal_connection_destroy(state.preview_connection);

-------------------------------------------------------------------------------------------------------------------------


cmake .

make

./camcv


If you meet this error
---------------------------------------------------------------------------------------------
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates

---------------------------------------------------------------------------------------------


Change Line 696
--------------------------------------------------------------------------------------------------------
else if (!raspipreview_create(&state.preview_parameters))

=> 

else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS)
--------------------------------------------------------------------------------------------------------

Re cmake~

If you completed all, open openCV window and display 

Raspberry Pi - OpenCV with Picam (2)


2. Make a camcv


2-1 Copy to another directory

cd

mkdir camcv

cd camcv

cp -r /opt/vc/userland/host_applications/linux/apps/raspicam/* .


2-2 Make a camcv file

raspistill change to camcv

mv raspistill.c camcv.c

If you typing command ls, you can see camcv.c file instead of raspistill.c file.


2-3 change  cmakelists.txt

sudo nano cmakelists.txt

Clear all and copy and paste this text.
-------------------------------------------------cmakelists.txt-------------------------------------------------------


  1. cmake_minimum_required(VERSION 2.8)
  2.  project(camcv)
  3.  SET(COMPILE_DEFINITIONS -Werror)
  4. include_directories(/opt/vc/userland/host_applications/linux/libs/bcm_host/include)
  5.  include_directories(/opt/vc/userland/interface/vcos)
  6.  include_directories(/opt/vc/userland)
  7.  include_directories(/opt/vc/userland/interface/vcos/pthreads)
  8.  include_directories(/opt/vc/userland/interface/vmcs_host/linux)
  9.  include_directories(/opt/vc/userland/interface/khronos/include)
  10.  include_directories(/opt/vc/userland/interface/khronos/common)
  11.  include_directories(/home/pi/camcv/)
  12.  include_directories(/home/pi/camcv/gl_scenes/)
  13.  include_directories(/home/pi/camcv/libfacerec/include/)
  14. add_executable(camcv RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv.c RaspiTex.c RaspiTexUtil.c gl_scenes/teapot.c gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/sobel.c gl_scenes/yuv.c tga.c )
  15. target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so )


----------------------------------------------------------------------------------------------------------------------

ADD------------------------------------------------------------------------------------------

If you can see below error, modify target_link_libaries in cmakelists.txt!!

/usr/bin/ld: CMakeFiles/camcv.dir/camcv.c.o: undefined reference to symbol 'sem_post@@GLIBC_2.4'
//lib/arm-linux-gnueabihf/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: ld returned 1 exit status
CMakeFiles/camcv.dir/build.make:392: recipe for target 'camcv' failed
make[2]: *** [camcv] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/camcv.dir/all' failed
make[1]: *** [CMakeFiles/camcv.dir/all] Error 2
Makefile:72: recipe for target 'all' failed
make: *** [all] Error 2


  1. target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so/opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so libpthread.so libm.so)

----------------------------------------------------------------------------------------------




If you installed samba, can use easy wordpad



save and exit.

2-4 Cmake

cmake .




2-5 Make

make




try to take a picture using camcv command.

./camcv -t 1000



PS. If make a mistake in cmakelists.txt, pi make a error. When error you retry!!


2-6(optional) Delete unused code.

Think RPI  recomend delete to below delete list.

sudo nano camcv.c   (or using word pad)

---------------------------------------------------Delete list----------------------------------------------------------


static void dump_status(RASPISTILL_STATE *state)


static int parse_cmdline(int argc, const char **argv, RASPISTILL_STATE *state)


static void display_valid_parameters(char *app_name)


static MMAL_STATUS_T add_exif_tag(RASPISTILL_STATE *state, const char *exif_tag)


static void add_exif_tags(RASPISTILL_STATE *state)


static void store_exif_tag(RASPISTILL_STATE *state, const char *exif_tag)
--------------------------------------------------------------------------------------------------------------------------

2-7 add function(save to file)

Search to default_status(&state); in camcv.c and add to below line


  1. state.filename=”foobar.jpg;

save and exit.

cmake .

make

./camcv -t 1000

If succeed to take a picture,save to foobar.jpg file.

Raspberry Pi - OpenCV with Picam (1)


I refer to this blog.


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

    


Raspberry : ver B 512MB

OS : Raspbian wheezy (2013.09.25).




1. PREPARE
1-1. prepare

Need a cmake. If you don't installed cmake, install cmake package.


 sudo apt-get install cmake




Getting basic source code.


git clone git://github.com/raspberrypi/userland




Move to  " /opt/vc/userland"

sudo mv /userland /opt/vc/userland

cd /opt/vc/userland


mkdir build

cd build


1-2. CMake

sudo cmake -D CMAKE_BUILD_TYPE=Release ..





If completed CMake, you can see makefile.


1-3. make

sudo make




 

This process is long long time.

Go to Coffee.



1-4. Install



sudo make install




If completed install then run!!

1-5. Run

cd /opt/vc/bin

./raspistill -t 3000

If picam turn on light and take a picture, it's normal

Go to next stage!!