Tải bản đầy đủ (.pdf) (4 trang)

computer vision – face detection in java with opencv using javacv - tk gospodinov

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (439.19 KB, 4 trang )

Computer Vision – Face Detection in Java with OpenCV using JavaCV
( />I stumbled upon a few libraries that enable us to put some artificial intelligence goodness right into our Java applications. One such library is OpenCV
( . While I’ve read about it in a few occasions I had not really explored it myself – until about a week ago.
OpenCV stands for Open Source Computer Vision and has a catalog of functions that incorporate over 500 algorithms for real time computer vision and
image processing. It was originally started by Intel back in the late 90s and is currently released under the open source BSD license. While it is mainly
written in C, it has been ported on Python, Java, and other languages which has allowed it to gain more ground. In Java, it is available through JavaCV
( , which is a wrapper that calls the native functions.
Let’s switch gears and look at a simple example… But first we have to set up a few prerequisites. I am running it under Windows 7, but it is also available
under Mac and Linux. Here is what we’ll need to get it up and running:
1. Install the OpenCV ( distribution (I installed the 2.1 binary distribution for Windows).
2. Make sure that the OpenCV installation folder is in your PATH.
3. Download the Java Native Access library (JNA) ( , which allows JavaCV ( to call the native
functions.
4. Put JNA and JavaCV in your classpath.
5. Try the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


17
18
19
20
import static name.audet.samuel.javacv.jna.cv.CV_BGR2GRAY;
import static name.audet.samuel.javacv.jna.cv.cvCvtColor;
import static name.audet.samuel.javacv.jna.cv.cvHaarDetectObjects;
import static name.audet.samuel.javacv.jna.cxcore.CV_AA;
import static name.audet.samuel.javacv.jna.cxcore.IPL_DEPTH_8U;
import static name.audet.samuel.javacv.jna.cxcore.cvGetSeqElem;
import static name.audet.samuel.javacv.jna.cxcore.cvLoad;
import static name.audet.samuel.javacv.jna.cxcore.cvPoint;
import static name.audet.samuel.javacv.jna.cxcore.cvRectangle;
import static name.audet.samuel.javacv.jna.highgui.cvLoadImage;
import static name.audet.samuel.javacv.jna.highgui.cvSaveImage;
import name.audet.samuel.javacv.JavaCvErrorCallback;
import name.audet.samuel.javacv.jna.cv.CvHaarClassifierCascade;
import name.audet.samuel.javacv.jna.cxcore.CvMemStorage;
import name.audet.samuel.javacv.jna.cxcore.CvRect;
import name.audet.samuel.javacv.jna.cxcore.CvScalar;
import name.audet.samuel.javacv.jna.cxcore.CvSeq;
import name.audet.samuel.javacv.jna.cxcore.IplImage;

public class FaceDetection {
?
(#)
A few things to note… The cascade files are located in /data/haarcascades in your OpenCV installation folder. If you get OpenCV errors that cause it to
crash, you may have to compile OpenCV without SSE, per the following note by Samuel, the creator of JavaCV:
“OpenCV might crash if it has been compiled with SSE instructions. This is known to occur on 32-b it x86 CPUs when the SSE calling conventions of the
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

52
53
54
55
56
57
58
59
60

// The cascade definition to be used for detection.
private static final String CASCADE_FILE = "haarcascade_frontalface_alt.xml";

public static void main(String[] args) throws Exception {
// This will redirect the OpenCV errors to the Java console to give you
// feedback about any problems that may occur.
new JavaCvErrorCallback().redirectError();

// Load the original image.
IplImage originalImage = cvLoadImage(args[0], 1);

// We need a grayscale image in order to do the recognition, so we
// create a new image of the same size as the original one.
IplImage grayImage = IplImage.create(originalImage.width,
originalImage.height, IPL_DEPTH_8U, 1);

// We convert the original image to grayscale.
cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);

CvMemStorage storage = CvMemStorage.create();


// We instantiate a classifier cascade to be used for detection, using the cascade definition.
CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
cvLoad(CASCADE_FILE));

// We detect the faces.
CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1, 0);

//We iterate over the discovered faces and draw yellow rectangles around them.
for (int i = 0; i < faces.total; i++) {
CvRect r = new CvRect(cvGetSeqElem(faces, i));
cvRectangle(originalImage, cvPoint(r.x, r.y),
cvPoint(r.x + r.width, r.y + r.height), CvScalar.YELLOW, 1, CV_AA, 0);
}

// Save the image to a new file.
cvSaveImage(args[1], originalImage);
}
}
compiler used to build the Java implementation differ from the one used to compile OpenCV. The AMD64 architecture appears unaffected. A workaround
may be included in a future version of JNA, but for the moment, please be advised.”
I was able to use 2.1, but 2.0 had to be recompiled.
And here are the results:
( />content/uploads/2010/09/example.jpg)
The original image
( />content/uploads/2010/09/detected.jpg)
The image with the detected faces
As you can see, it recognized almost all faces. You can probably get it to recognize all if you tweak the code a little, notably the cvHaarDetectObjects
function call. I’ve tried it with other pictures too, and it performs really well! There is an OpenCV reference here
( . It is for C++, but it should be pretty clear to understand and apply to JavaCV. Also make

sure to check the project’s site ( for more examples and open issues.
As always, all questions or comments are welcome!
Rating: 4.8/5 (12 votes cast)
Posted in Artificial Intelligence ( by TK at
September 23rd, 2010.
Tags: AI ( , Artificial Intelligence
( , Face ( , Face
Detection ( , Image ( ,
Image Processing ( , JavaCV
( , JNI ( , OpenCV
( , Picture ( , Shape
( />

×