1. Trang chủ
  2. » Giáo án - Bài giảng

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

4 677 2
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 439,19 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Computer Vision – Face Detection in Java with OpenCV using JavaCVhttp://tkgospodinov.com/computer-vision-face-detection-in-java-with-opencv-using-javacv/ I stumbled upon a few libraries

Trang 1

Computer Vision – Face Detection in Java with OpenCV using JavaCV

(http://tkgospodinov.com/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 (http://sourceforge.net/projects/opencvlibrary/) 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 (http://code.google.com/p/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 (http://sourceforge.net/projects/opencvlibrary/) 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) (http://jna.dev.java.net/) , which allows JavaCV (http://code.google.com/p/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

importstatic name.audet.samuel.javacv.jna.cv.CV_BGR2GRAY;

importstatic name.audet.samuel.javacv.jna.cv.cvCvtColor;

importstatic name.audet.samuel.javacv.jna.cv.cvHaarDetectObjects;

importstatic name.audet.samuel.javacv.jna.cxcore.CV_AA;

importstatic name.audet.samuel.javacv.jna.cxcore.IPL_DEPTH_8U;

importstatic name.audet.samuel.javacv.jna.cxcore.cvGetSeqElem;

importstatic name.audet.samuel.javacv.jna.cxcore.cvLoad;

importstatic name.audet.samuel.javacv.jna.cxcore.cvPoint;

importstatic name.audet.samuel.javacv.jna.cxcore.cvRectangle;

importstatic name.audet.samuel.javacv.jna.highgui.cvLoadImage;

importstatic 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;

publicclass FaceDetection {

?

(#)

Trang 2

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-bit 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

privatestaticfinal String CASCADE_FILE = "haarcascade_frontalface_alt.xml";

publicstaticvoid 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);

}

}

Trang 3

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:

(http://tkgospodinov.com/wp-content/uploads/2010/09/example.jpg)

The original image

(http://tkgospodinov.com/wp-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

(http://opencv.willowgarage.com/documentation/cpp/index.html) 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 (http://code.google.com/p/javacv/) for more examples and open issues

As always, all questions or comments are welcome!

Trang 4

Rating: 4.8/5 (12 votes cast)

Posted in Artificial Intelligence (http://tkgospodinov.com/category/artificial-intelligence/) by TK at September 23rd, 2010

Tags: AI (http://tkgospodinov.com/tag/ai/) , Artificial Intelligence

(http://tkgospodinov.com/tag/artificial-intelligence/) , Face (http://tkgospodinov.com/tag/face/) , Face Detection (http://tkgospodinov.com/tag/face-detection/) , Image (http://tkgospodinov.com/tag/image/) , Image Processing (http://tkgospodinov.com/tag/image-processing/) , JavaCV

(http://tkgospodinov.com/tag/javacv/) , JNI (http://tkgospodinov.com/tag/jni/) , OpenCV

(http://tkgospodinov.com/tag/opencv/) , Picture (http://tkgospodinov.com/tag/picture/) , Shape

(http://tkgospodinov.com/tag/shape/)

Ngày đăng: 28/04/2014, 15:47

TỪ KHÓA LIÊN QUAN

w