Sie sind auf Seite 1von 7

Android OpenCV :Simple Face Tracking

Pi19404
April 2, 2013

Contents

Contents
Android OpenCV :Simple Face Tracking
0.1 0.2 0.3 0.4 Introduction . . . . . . . . . . . . . . . . . . . Processing Raw Camera Data and other Face Tracking . . . . . . . . . . . . . . . . . . Face Tracking : Implementation Details 0.4.1 Native Code . . . . . . . . . . . . . . . 0.4.2 Java Code . . . . . . . . . . . . . . . . 0.5 Launch Application . . . . . . . . . . . . . . . 0.6 Code . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3
3 3 3 4 4 6 6 6 6

2 | 7

Android OpenCV :Simple Face Tracking

Android OpenCV :Simple Face Tracking


0.1 Introduction
In this article will look at simple application of Face Location on android platform using OpenCV libraries using Haar Cascade Classifiers.

0.2 Processing Raw Camera Data and other details


The android application provides the raw camera image in YUV format as byte image in the registered preview Callback function.Refer to earlier artile about details of decoding raw camera data into format suitable for processing by OpenCV libraries. For details regarding compilation,setup and other details kindly refer to previous android articles/

0.3 Face Tracking


The Face tracking algorithm consists of face localization and face tracking. The harr cascade classifier will be used to achieve face localization which provides a ROI where the face is located. Once a ROI is obtained CamShift Algorithm will be used to track the face. However camshift tracker will tracking any similar colored object in the frame even though face is not present in the scene.Hence a reset tracker button is provided to reinitialize the tracker which will set the tracking status to false and face localization loop will be launched.

3 | 7

Android OpenCV :Simple Face Tracking If the tracking is lost at any stage ,localization algorithm is triggered again and cycle of localization and tracking continues.

0.4 Face Tracking : Implementation Details


0.4.1 Native Code
The first step of compilation using the opencv,javacv method is to write the header files. The classes defined in jni/AndroidOpenCVProcessImagehpp code provides a high level interface to opencv haar casacade detection class for face localization and camshift methods for tracking. The face localization provides largest face region detected by the cascade classifier. The class haarcascade is responsible for face localization.Below are the methods of the class
1 2

haarcascade ( char * name ) Rect detect ( Mat & frame )

 The haarcascade constructor accepts the haar cascade configuration file name as input and loads the configuration file.  The function detect accepts the input frame performs ,resizes the image ,pre processes the image and then performs multiscale detection using haar cascade classifier.

From a list of possible candidates it selects the largest ROI detected and returns the details to main tracking algorithm. To achieve real time performance on the mobile phone the image is reduced to 160x120. The haar detection is performed on this downscaled image and the location of face region are scaled by suitable ammount to plot the ROI properly. The class meanshift provides high level interface to camshift tracking algorithm.

4 | 7

Android OpenCV :Simple Face Tracking

1 2 3 4

meanshift () void buildHistogram () Mat calcProbability () int track ( Mat & image , Rect & rect )

 The method meanshift is constructor for the class which initializes the parameters of histogram etc  The method buildHistogram is method which constructs the HS histogram from the input frame.  The method calcProbability is a method which computes the probability map of frame once histogram has been constructed.  The method track is the main method to be called for mean shift tracking .

The class OpenCVProcess is main class which provide interface to the java code.The methods of class are as follows
1 2 3 4

int run ( int width , int height , signed char * _yuv , int * _bgra ) int run ( Mat bgr ) void initDetector ( const char * name ) void initTracker ()

 The method is main method face.The primary task is to BGR image and then call the Mat data structure which as

that is called by the java interdecode the camera data to Mat run routine which is the main with input processing routine. the tracker status .If the cascade classifier for face shift tracker and provides classifier as input .

 The run(Mat bgr) method checks status is false it calles the haar localization else it calls the mean the ROI detected by haar cascade

 the initDetector method calls the initialization routine of haar cascade class to load the configuration file.  the initTracke method sets the tracker status to false so that face localization loop is executed.

If errors occur in the opencv native routine it will not be caught by the java interface.Hence a retun code is passed to indicate status of the opencv native routines.

5 | 7

Android OpenCV :Simple Face Tracking

0.4.2 Java Code


The next step is to write the Java Files to native interface with the header files In the android a menu option is provided in the application is provided to initialize/reset the tracker.
 file AndOCVFaceDetectionEx.java contains main android routine which launches the application.  file ProcessImage.java contains methods to interface with the native code.  file Preview.java contains methods to initialize the camera,set the camera parameter,get the raw camera  file DrawOnTop.java contains methods to draw the processed image on top of camera preview.

0.5 Launch Application


Transfer the apk file generated to device and test the application.

(a) Screenshot Image

Image

0.6 Code
The code can be found in code repository https://github.com/ pi19404/m19404/tree/master/Android/AndroidOpenCV1.2 or https://code. google.com/p/m19404/source/browse/Android/AndroidOpenCV1.2. The header files is located in jni directory.The library files are not placed in the repository download them from appropriate packages on send a mail separately for download link.

6 | 7

Bibliography

Bibliography
[1] Gary R. Bradski.  Computer Vision Face Tracking For Use in a Perceptual User Interface. In: Intel Technology Journal Q2 (1998).

url: http://citeseerx. ist.psu.edu/viewdoc/summary?doi=10.1.1.14.7673.

[2]

Paul A. Viola and Michael J. Jones.  Rapid Object Detection using a Boosted Cascade of Simple Features. In: CVPR (1). 2001, pp. 511518.

7 | 7