Eclipse+Java+OpenCV246Face Service

來源:互聯網
上載者:User

1.環境搭建:見上一篇部落格

整個項目的結構圖:


2.編寫DetectFaceDemo.java,代碼如下:

package com.njupt.zhb.test;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.objdetect.CascadeClassifier;//// Detects faces in an image, draws boxes around them, and writes the results// to "faceDetection.png".//public class DetectFaceDemo {  public void run() {    System.out.println("\nRunning DetectFaceDemo");    System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());    // Create a face detector from the cascade file in the resources    // directory.    //CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());    //Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());    //注意:來源程式的路徑會多列印一個‘/’,因此總是出現如下錯誤/* * Detected 0 faces Writing faceDetection.png libpng warning: Image * width is zero in IHDR libpng warning: Image height is zero in IHDR * libpng error: Invalid IHDR data */    //因此,我們將第一個字元去掉    String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);    CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);    Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));    // Detect faces in the image.    // MatOfRect is a special container class for Rect.    MatOfRect faceDetections = new MatOfRect();    faceDetector.detectMultiScale(image, faceDetections);    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));    // Draw a bounding box around each face.    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));    }    // Save the visualized detection.    String filename = "faceDetection.png";    System.out.println(String.format("Writing %s", filename));    Highgui.imwrite(filename, image);  }}

3.編寫測試類別:

package com.njupt.zhb.test;public class TestMain {  public static void main(String[] args) {    System.out.println("Hello, OpenCV");    // Load the native library.    System.loadLibrary("opencv_java246");    new DetectFaceDemo().run();  }}//運行結果://Hello, OpenCV////Running DetectFaceDemo///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml//Detected 8 faces//Writing faceDetection.png

運行結果:

源碼下載:http://download.csdn.net/detail/nuptboyzhb/5961985

未經允許,不得用於商業目的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.