Created
March 29, 2013 14:54
-
-
Save Yi-Tseng/5271334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.opencv.core.Core; | |
import org.opencv.core.Mat; | |
import org.opencv.core.MatOfRect; | |
import org.opencv.core.Point; | |
import org.opencv.core.Rect; | |
import org.opencv.core.Scalar; | |
import org.opencv.highgui.Highgui; | |
import org.opencv.highgui.VideoCapture; | |
import org.opencv.objdetect.CascadeClassifier; | |
class DetectFaceDemo { | |
public void run() { | |
System.out.println("\nRunning DetectFaceDemo"); | |
CascadeClassifier faceDetector = new CascadeClassifier("lbpcascade_frontalface.xml"); | |
Mat image = new Mat(); | |
VideoCapture capture = new VideoCapture(0); | |
while (capture.grab()) { | |
capture.retrieve(image); | |
if (image != null) { | |
MatOfRect faceDetections = new MatOfRect(); | |
faceDetector.detectMultiScale(image, faceDetections); | |
System.out.println(String.format("Detected %s faces", | |
faceDetections.toArray().length)); | |
if (faceDetections.toArray().length != 0) { | |
for (Rect rect : faceDetections.toArray()) { | |
Core.rectangle(image, new Point(rect.x, rect.y), | |
new Point(rect.x + rect.width, rect.y | |
+ rect.height), new Scalar(0, 255, 0)); | |
image = image.adjustROI(rect.y, rect.y + rect.height, rect.x, rect.x+rect.width); | |
break; | |
} | |
System.out.println(image.width() + " " + image.height()); | |
String filename = "faceDetection.jpg"; | |
System.out.println(String.format("Writing %s", filename)); | |
Highgui.imwrite(filename, image); | |
break; | |
} | |
} | |
} | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
try { | |
System.loadLibrary("opencv_java249"); | |
new DetectFaceDemo().run(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment