Click here to Skip to main content
14,163,348 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
import static com.googlecode.javacv.cpp.opencv_core.cvSetImageROI;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;

import java.util.Random;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;

public class FaceDetection {

	// The cascade definition to be used for detection.
	private static final String CASCADE_FILE = "C:\\OpenCV2.3\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";

	public static void main(String arg[]) throws Exception {

		float randomNumber = 0;

		Random random = new Random();

		randomNumber = random.nextFloat();

		// Load the original image.
		IplImage originalImage = cvLoadImage(
				"E:\\4 th Year 1Semester\\Project\\JavaCVApp\\Steve-Jobs-iphone_320.jpg", 1);

		// create image window named "My Image"
		final CanvasFrame canvas = new CanvasFrame("My Image");

		// request closing of the application when the image window is closed
		canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

		// 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, 3,
				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);
			// show image on window
			canvas.showImage(originalImage);
		}

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

			cvSetImageROI(originalImage, r);
			// Save the image to a new file.
			cvSaveImage("E:\\4 th Year 1Semester\\Project\\JavaCVApp\\src\\"
					+ randomNumber + i + ".jpg", originalImage);

		}

		System.out.println("nu=" + faces.total());

	}

}
Posted
Updated 7-Jun-12 18:15pm
v2
Comments
Sandeep Mewara 8-Jun-12 1:05am
   
This code dump is what? You tried to convert and faced any issue?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First Way

Install ADT plugin
Right click on java project
Select Android > Convert to Android project.
Supply /res folder, /AndroidManifest.xml and /default.properties if needed.

Second Way: UPD

Actually, the "First Way" doesn't work with latest ADT plugin versions, for some reason google has removed the convert command. Another way would be:

Check in your java project into a VCS (svn or git or whatever)
Delete it from workspace
Import the project from VCS
On import, select Create New Project -> Android

Third Way: UDP :)

Remove the java project from workspace
Start create new Android project wizard
Select create from existing source option and point to your java project folder.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

stupids dont found new technology for software
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 11 Feb 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100