[vc_row][vc_column width=”1/1″][vc_column_text]OpenCV is based on (open) issued a cross-platform computer vision library, you can run on Linux, Windows and Mac OS operating systems. It is lightweight and efficient – composed of a series of C functions and a few C, while providing an interface to Python, Ruby, MATLAB and other languages, to achieve a lot of generic algorithm for image processing and computer vision.
PcDuino is an Arduino-compatible interface, mini pc, A8 architecture 1Ghz CPU, the computing power is good and OpenCV to run just fine. Here you can follow with their faces to achieve a moving camera.[/vc_column_text][vc_tour][vc_tab title=”Parts List” tab_id=”1389580704-1-27″][vc_column_text]
- pcDuino
- the sensor expansion board
- PTZ camera
- camera
[/vc_column_text][/vc_tab][vc_tab title=”Software Environment” tab_id=”1389580704-2-84″][vc_column_text]
- Pcduino onboard Ubuntu;
- GCC 4.6;
- QT 4.8.5: http://qt-project.org/downloads;
- OpenCV 2.4.7: http://opencv.org/downloads.html;
- Arduino SDK (c_enviroment): https://github.com/pcduino/c_enviroment.
Before starting hands, we first stroke about ideas.
You can easily achieve the Face Detect by OpenCV, OpenCV sample directory, there’s this example. Process generally get from the camera frame image, characterized by previously trained to detect human faces coordinates (ie the pixel coordinates of the image), and finally in the frame image circle on the face of the position.
What we need is the coordinates of the human face, the face first calculate the coordinates x and y axes are offset from the center of the screen (unit: pixels), and then point to fix the camera according to the deviation from the value-driven PTZ camera, people face the center of the screen coordinates coincide (ie, pointing to the face).[/vc_column_text][/vc_tab][vc_tab title=”Parts List” tab_id=”1389581052914-2-7″][vc_column_text]The hardware installation:
1. The first connection on pcDuino sensor expansion board,
2. Then head two servos are connected to the sensor expansion board PWM 5,6 pin (PWM and GPIO pins shared 5,6, 5,6 chosen because only 5,6 hardware PWM, does not make the CPU occupancy rate is too high).
One thing to note here is that if you choose a high-powered head and steering gear, steering gear need an independent power supply.
It is installed pictures:
[/vc_column_text][/vc_tab][vc_tab title=”software” tab_id=”1389581134440-3-5″][vc_column_text]First, compile and install OpenCV:
1 , the first installation of various dependencies , depending on your environment , may be missing , all make up , in order to compile a OpenCV once( because the compilation process took almost three hours ).
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev
2 , downloading codecs OpenCV package to generate the information needed to compile with cmake tool , fourth sentence of release compiled version , the installation directory is / usr / local
cd ~ / opencv
mkdir release
cd release
cmake-D CMAKE_BUILD_TYPE = RELEASE-D CMAKE_INSTALL_PREFIX = / usr / local ..
3 , began compiling
make
make install
About OpenCV installation you can refer to the official documentation : http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html # linux-installation
Second, compile and install c_enviroment:
1, c_enviroment is Pcduino control hardware I / O library , the software environment from the beginning to the link to download c_enviroment at the zip package, extract the compiler
cd c_enviroment
make
After compilation , we can enter the output / test directory , test the sample led lights
Third, compile and install Qt:
In order to facilitate the development , I use the Qt creator as an IDE.
1, install Qt creator
sudo apt-get install qtcreator
You can now run in programing in Qt creator , but this time it can not be used , also need to install Qt library
2 , install Qt library
By the beginning of the link to download the software environment for embedded version of Qt library: Qt libraries 4.8.5 for embedded Linux
Specific installation process you can refer to this post , will not go into here :
http://www.pcduino.org/forum.php?mod=viewthread&tid=21&highlight =% E5% 9C% A8pcduino% E5% AE% 89% E8% A3% 85Qt
After a lengthy compile and install , Qt finally completed , bringing the environment be setting up a half a .
Create a C + + project called face_tracking_camera , edit face_tracking_camera.pro file , add and c_enviroment OpenCV source files , header files and libraries path , I am here to OpenCV installed in / usr / local /, c_enviroment installed in / home / ubuntu / c_enviroment /
expand source
Here, the whole setting up the environment .[/vc_column_text][/vc_tab][vc_tab title=”Code” tab_id=”1389581217322-4-10″][vc_column_text]# include <opencv2/opencv.hpp>
# include <Arduino.h>
# include <wiring_private.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <assert.h>
# include <math.h>
# include <float.h>
# include <limits.h>
# include <time.h>
# include <ctype.h>
# include <sys/time.h>
# include <signal.h>
/ / Sorter features for face recognition database file path
constchar * cascade_name = “/ usr / local / share / OpenCV / haarcascades / haarcascade_frontalface_alt.xml”;
/ / Define the X -axis ( horizontal swing ), Y -axis ( vertical swing ) the center of the steering gear
staticintcenterlevelX = 110;
staticintcenterlevelY = 60;
/ / Define the servo frequency
constintfrequncy = 260;
/ / Use an integer to store the current direction of the servo swing
staticintturningRight = 1;
/ / Signature list of functions
voiddetect_and_draw (IplImage * image);
voidstart_pulse (intpwm_id, intfreq, intvalue);
voidsigroutine (intdunno);
voidreset ()
longgetCurrentTime ();
longstartTime;
longendTime;
/ / led indicator pin pin
intpin_led = 3;
/ / Is used to calculate an OpenCV application memory
staticCvMemStorage * storage = 0;
/ / Declare a haar classifier
staticCvHaarClassifierCascade * cascade = 0;
voidsetup () {
/ / Define the pin as an output indicator
pinMode (pin_led, OUTPUT);
/ / Reset the servo head
reset ();
/ / Monitor interrupt signals
signal (SIGINT, sigroutine);
/ / Create a file named result window
cvNamedWindow (“result”, 1);
/ / Open the camera
CvCapture * capture = cvCaptureFromCAM (-1);
/ / Declare two opencv image type
IplImage * img;
IplImage * newImg;
/ / Load classifier
cascade = (CvHaarClassifierCascade *) cvLoad (cascade_name, 0, 0, 0);
/ / Check classifier abnormal loads
if (! cascade) {
fprintf (stderr, “ERROR: Could not load classifier cascade \ n”);
}
/ / Locate memory block
storage = cvCreateMemStorage (0);
while (1) {
/ / Because of the limited pcduino computing power , in order to ensure the frame rate , the acquisition from the camera to the screen narrow 1 / 2
newImg = cvQueryFrame (capture);
(! newImg) if break;
img = cvCreateImage (cvSize (newImg-> width / 2, newImg-> height / 2), newImg-> depth, newImg-> nChannels);
cvResize (newImg, img);
/ / Flip the image
cvFlip (img, img, 1);
/ / Call the function to identify and draw images
detect_and_draw (img);
/ / Release the memory used by image
cvReleaseImage (& img);
/ / Listen esc key
intc = cvWaitKey (33);
if (c == 27) break;
}
/ / Release the camera
cvReleaseCapture (& capture);
/ / Destroy window
cvDestroyWindow (“result”);
}
voiddetect_and_draw (IplImage * img) {
startTime = getCurrentTime ();
/ / Clear the memory space used
cvClearMemStorage (storage);
intscale = 1;
inti;
/ / Declare a central point storage location identified Face
CvPoint ptcenter;
/ / Face
if (cascade) {
/ / Frame Face Detection
CvSeq * faces = cvHaarDetectObjects (img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize (80, 80));
/ / If it detects more than one face, traversal removed
for (i = 0; i <(faces? faces-> total: 0); i + +) {
/ / Create a rectangular face
CvRect * r = (CvRect *) cvGetSeqElem (faces, i);
/ / Translation of the center of the face rectangle
ptcenter.x = (r-> x + (r-> width / 2)) * scale;
ptcenter.y = (r-> y + (r-> height / 2)) * scale;
/ / Draw a circle identifies the location of a human face
cvCircle (img, ptcenter, (r-> width + r-> height) / 4, CV_RGB (255,0,0), 3, 8, 0);
}
}
/ / Display the image
cvShowImage (“result”, img);
/ / Calculate the frame rate
endTime = getCurrentTime ();
longtime = endTime-startTime;
intframerate = 1000/time;
/ / Check whether the center is empty
if (ptcenter.x && ptcenter.y) {
/ / std :: cout << “center_point: (” << ptcenter.x << “,” << ptcenter.y << “) \ tframe_rate:” << framerate << “\ n” << std :: endl;
/ / std :: cout << “x:” << (ptcenter.x-img-> width / 2) << “\ ty:” << (ptcenter.y-img-> height / 2) << std :: endl;
/ / The led indicator pin output low, turn off lights
digitalWrite (pin_led, LOW);
/ / Driver camera moves to the center position of the face
centerlevelX + = (ptcenter.x-img-> width / 2) / 110 * 2;
if (centerlevelX <= 170 && centerlevelX> = 50) start_pulse (6, frequncy, centerlevelX);
centerlevelY – = (ptcenter.y-img-> height / 2) / 70;
if (centerlevelY <= 90 && centerlevelY> = 45) start_pulse (5, frequncy, centerlevelY);
/ / Display the corrected amplitude of the X and Y axes and frame
std :: cout << “X:” << centerlevelX << “\ tY:” << centerlevelY << “\ tFrameRate:” << framerate << std :: endl;
} else {
printf (“no face is detected in the image \ n”);
/ / Indicator lights
digitalWrite (pin_led, HIGH);
/ / If no face is detected then swing the camera around
if (centerlevelX <= 170 && turningRight == 1) {
start_pulse (6, frequncy, centerlevelX + = 2);
if (centerlevelX> 170) turningRight = 0;
/ / std :: cout << centerlevelX << std :: endl;
}
if (centerlevelX> = 50 && turningRight == 0) {
start_pulse (6, frequncy, centerlevelX-= 2);
if (centerlevelX <50) turningRight = 1;
/ / std :: cout << centerlevelX << std :: endl;
}
}
/ / Camera down to prevent excessive
if (centerlevelY <45) centerlevelY = 45;
}
/ / Reset function , adjust the servo X, Y axis to the center position
voidreset () {
delay (50);
start_pulse (5, frequncy, 60);
start_pulse (6, frequncy, 110);
delay (50);
}
longgetCurrentTime () {
structtimeval tv;
gettimeofday (& tv, NULL);
longtime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
returntime;
}
/ / Servo drive functions
voidstart_pulse (intpwm_id, intfreq, intvalue) {
intstep = 0;
step = pwmfreq_set (pwm_id, freq);
/ / printf (“PWM% d set freq% d and valid duty cycle range [0,% d] \ n”, pwm_id, freq, step);
if (step> 0) {
/ / printf (“PWM% d test with duty cycle% d \ n”, pwm_id, value);
analogWrite (pwm_id, value);
delay (50);
}
}
/ / signal callback function , monitor the interrupt signal , do some work status reset
voidsigroutine (intdunno) {
switch (dunno) {
case2:
printf (“Get a signal – SIGINT \ n”);
reset ();
analogWrite (6,0);
analogWrite (5,0);
digitalWrite (pin_led, LOW);
exit (0);
break;
}
}
voidloop () {}[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.