Sep 14, 2015

[ Raspberry Pi2 ] OpenCV using Picam 3



We saved jpg file in Previous posting.

This posting is about OpenCV.

1. Install OpenCV and related packages.

Install GTK + UVC viewer.

1
sudo apt-get install guvcview
cs


Setting uvcvideo.

1
2
sudo usermod -a -G video pi
sudo modprobe uvcvideo
cs


Install OPENCV library.

1
sudo apt-get install libopencv-dev
cs


Install Python-Opencv.
1
sudo apt-get install python-opencv
cs

Install face recognition


If above link is didn't download, use below link.


Extract to home/pi/ (using samba is easy)

You can see 'bytefish-libfacerec-xxx' folder in extract folder.

You move to /home/pi/ and rename 'bytefish-libfacerec '.




2. BUILD


cmake 
1
2
3
cd ..
cd bytefish-libfacerec
sudo cmake .
cs

make
1
sudo make
cs


If build is success, make this file libopencv_facerec.a 


move folder to /home/pi/camcv/libfacerec/ 
1
2
3
4
mkdir /home/pi/camcv/libfacerec
cd /home/pi/camcv/libfacerec
cp -r /home/pi/bytefish-libfacerec/* .  
cd ..
cs

Editing makelist
1
sudo nano CMakeLists.txt
cs

Recommend all text copy.
  1. cmake_minimum_required(VERSION 2.8)
  2.  project(camcv)
  3.  SET(COMPILE_DEFINITIONS -Werror)
  4. #OPENCV
  5. find_package( OpenCV REQUIRED )
  6. #except if you’re pierre, change the folder where you installed libfacerec
  7. #optional, only if you want to go till step 6 : face recognition
  8. link_directories( /home/pi/camcv/libfacerec)
  9.  include_directories(/opt/vc/include)
  10.  include_directories(/opt/vc/include/interface/vcos)
  11.  include_directories(/opt/vc/include/interface/vcos/pthreads)
  12.  include_directories(/opt/vc/include/interface/vmcs_host/linux)
  13.  include_directories(.)
  14.   
  15. include_directories(/opt/vc/userland/host_applications/linux/libs/bcm_host/include)
  16.  include_directories(/opt/vc/userland/interface/vcos)
  17.  include_directories(/opt/vc/userland)
  18.  include_directories(/opt/vc/userland/interface/vcos/pthreads)
  19.  include_directories(/opt/vc/userland/interface/vmcs_host/linux)
  20.  include_directories(/opt/vc/userland/interface/khronos/include)
  21.  include_directories(/opt/vc/userland/interface/khronos/common)
  22.  include_directories(/home/pi/camcv/)
  23.  include_directories(/home/pi/camcv/gl_scenes/)
  24.  include_directories(/home/pi/camcv/libfacerec/include/)
  25. add_executable(camcv RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv.c RaspiTex.c RaspiTexUtil.c gl_scenes/teapot.c
  26. gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/sobel.c gl_scenes/yuv.c tga.c )
  27. 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 /home/pi/camcv/libfacerec/libopencv_facerec.a ${OpenCV_LIBS})

Editing camcv.c
1
sudo nano camcv.c
cs

  1. Lines 61+ : add OpenCV Includes
  2. // *** PR : ADDED for OPENCV
  3. #include <cv.h>
  4. #include <highgui.h>

  5. Line 156 : modify init values for test (size of file)
  6. // *** PR : modify for demo purpose : smaller image
  7. state->timeout = 1000; // 5s delay before 
  8. take image
  9. state->width = 320;//2592;
  10. state->height = 200; //1944;

  11. Line 230+ : in static void encoder_buffer_callback function. 
  12. This is the core of the modification. 
  13. This function is a callback, call to get the image in the queue. 
  14. buffer contains the picture from the camera.

  15. // *** PR : OPEN CV Stuff here !
  16. // create a CvMat empty structure, with size of the buffer.
  17. CvMat* buf = cvCreateMat(1,buffer->length,CV_8UC1);
  18. // copy buffer from cam to 
  19. CvMatbuf->data.ptr = buffer->data;
  20. // decode image (interpret jpg)
  21. IplImage *img = cvDecodeImage(buf, CV_LOAD_IMAGE_COLOR);
  22. // we can save it 
  23. !cvSaveImage(“foobar.bmp”, img,0);
  24. // or display it
  25. cvNamedWindow(“camcvWin”, CV_WINDOW_AUTOSIZE);
  26. cvShowImage(“camcvWin”, img );
  27. cvWaitKey(0);
  28. Line 711/726/823 : we remove the native preview window (replaced by opencv window)
  29. // *** PR : we don’t want preview
  30. camera_preview_port = NULL;
  31. // PR : we don’t want preview
  32. // status = connect_ports(camera_preview_port, preview_input_port, &state.preview_connection);
  33. // mmal_connection_destroy(state.preview_connection);

Recommend download below file.

Editing Line 696
  1. else if (!raspipreview_create(&state.preview_parameters))
  2. => else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS)




Build 
1
2
sudo cmake .
sudo make
cs



Run
1
./camcv
cs


If you see above message, run in x-window.


And change image file, 'bmp'


No comments:

Post a Comment