2014-12-07 154 views
0

Hy我試圖讓opencv的facedetection樣本工作。 第一個問題在這裏solfed:opencv-3-0-0-facedetect-sample-fails打開CV 3.0.0人臉檢測detectMultiScale失敗

我的代碼現在看起來像這樣:

package org.maxbit.opencv.samples; 

import java.awt.image.ImageProducer; 

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.imgcodecs.Imgcodecs; 
import org.opencv.imgproc.Imgproc; 
import org.opencv.objdetect.CascadeClassifier; 

// 
// Detects faces in an image, draws boxes around them, and writes the results 
// to "faceDetection.png". 
// 
class DetectFaceDemo { 
    public void run() { 
    System.out.println("\nRunning DetectFaceDemo"); 

    // Create a face detector from the cascade file in the resources 
    // directory. 
    CascadeClassifier faceDetector = new CascadeClassifier(); 
// System.out.println(getClass().getResource("libpcascade_frontalface.xml").getPath()); 
    if(!faceDetector.load("D:/lbpcascade_frontalface.xml")) 
     System.out.println("ldpcascade_frontalface.xml not found!"); 
    System.out.println("Loading analyse method Done!"); 
    Mat image = Imgcodecs.imread("D:/lena.png"); 

    // 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()) { 
     Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0)); 
    } 

    // Save the visualized detection. 
    String filename = "faceDetection.png"; 
    System.out.println(String.format("Writing %s", filename)); 
    Imgcodecs.imwrite(filename, image); 
    } 
} 

public class SampleB { 
    public static void main(String[] args) { 
    System.out.println("Hello, OpenCV "+Core.NATIVE_LIBRARY_NAME); 

    // Load the native library. 
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
    new DetectFaceDemo().run(); 
    } 
} 

當我開始這方面,我得到以下erreor:

OpenCV Error: Assertion failed (clEnqueueReadBuffer(q, (cl_mem)u->handle, CL_TRUE, 0, u->size, alignedPtr.getAlignedPtr(), 0, 0, 0) == CL_SUCCESS) in cv::ocl::OpenCLAllocator::map, file ..\..\..\..\opencv\modules\core\src\ocl.cpp, line 3961 
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\ocl.cpp:3961: error: (-215) clEnqueueReadBuffer(q, (cl_mem)u->handle, CL_TRUE, 0, u->size, alignedPtr.getAlignedPtr(), 0, 0, 0) == CL_SUCCESS in function cv::ocl::OpenCLAllocator::map 
] 
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(Native Method) 
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:176) 
    at org.maxbit.opencv.samples.DetectFaceDemo.run(SampleB.java:35) 
    at org.maxbit.opencv.samples.SampleB.main(SampleB.java:57) 

它指向這個line of code

+0

你的問題是什麼? – Celeo 2014-12-07 19:14:33

+1

爲了確保這不是一個opencl/build問題,在調用'run()'之前,你可以使用java wrapper for''cv :: ocl :: setUseOpenCL(false)'作爲此鏈接http://docs.opencv .ORG/REF /主/ DC/D83/group__core__opencl.html#gab747fa4efd88d3188f4ebcbc8a639c1e。查看* lena.jpg * – Kiran 2014-12-09 05:26:10

+0

後請檢查​​image.empty()如果​​您碰巧找到錯誤根本原因,請更新答案。 – Kiran 2014-12-10 05:40:20

回答

1

我知道這是一箇舊帖子,但我能夠通過不同的方式設置文件路徑來解決此問題。我用下面的修復文件路徑,我只是說這下一行和它的作品,當然代碼應清洗好一點的

faceDetector.load("C:\\workspace\\OpenCV_Starting\\bin\\main\\resources\\lbpcascade_frontalface.xml"); 

(當然,C:\工作區\東西我的電腦,所以適當改變!)