2016-06-18 3 views
1
import com.googlecode.javacv.cpp.opencv_core.IplImage; 
import static com.googlecode.javacv.cpp.opencv_core.*; 
import static com.googlecode.javacv.cpp.opencv_highgui.*; 
import static com.googlecode.javacv.cpp.opencv_objdetect.*; 

public class FaceDetection{ 

    public static final String XML_FILE = 
      "resources/haarcascade_frontalface_default.xml"; 

    public static void main(String[] args){ 

     IplImage img = cvLoadImage("resources/lena.jpg");  
     detect(img);   
    } 

    public static void detect(IplImage src){ 

     CvHaarClassifierCascade cascade = new 
       CvHaarClassifierCascade(cvLoad(XML_FILE)); 
     CvMemStorage storage = CvMemStorage.create(); 
     CvSeq sign = cvHaarDetectObjects(
       src, 
       cascade, 
       storage, 
       1.5, 
       3, 
       CV_HAAR_DO_CANNY_PRUNING); 

     cvClearMemStorage(storage); 

     int total_Faces = sign.total();  

     for(int i = 0; i < total_Faces; i++){ 
      CvRect r = new CvRect(cvGetSeqElem(sign, i)); 
      cvRectangle (
        src, 
        cvPoint(r.x(), r.y()), 
        cvPoint(r.width() + r.x(), r.height() + r.y()), 
        CvScalar.RED, 
        2, 
        CV_AA, 
        0); 

     } 

     cvShowImage("Result", src); 
     cvWaitKey(0); 

    }   

例外JavaのOpenCVのhaarcascade_frontalface_default.xml

OpenCV Error: Unspecified error (The node does not represent a user 
object (unknown type?)) in cvRead, file src\persistence.cpp, line 4976 
Exception in thread "main" java.lang.RuntimeException: 
src\persistence.cpp:4976: error: (-2) The node does not represent a 
user object (unknown type?) in function cvRead 

誰もがこの問題を解決する方法を知っていますか?

答えて

0

OpenCV haarascascades XMLリソースファイルをcvLoad関数に渡そうとしていますが、これはJavaリソースファイルで動作するようには設計されていません。これはC++関数なので、この概念についてはわかりません。

私は同じ問題を抱えていましたが、このxmlファイルをJavaリソースから一時ディレクトリにコピーし、cvLoad関数にフィードして削除していた唯一の回避策が見つかりました。出来た。さらに、そのための特別なOpenCV機能があります。

私はhttps://github.com/bytedeco/javacv/blob/master/samples/FaceApplet.java

String classiferName = "haarcascade_frontalface_alt.xml"; 

// copying xml file into temp directory 
File classifierFile = Loader.extractResource(classiferName, null, "classifier", ".xml"); 
if (classifierFile == null || classifierFile.length() <= 0) { 
    throw new IOException("Could not extract \"" + classiferName + "\" from Java resources."); 
} 

// Preload the opencv_objdetect module to work around a known bug. 
Loader.load(opencv_objdetect.class); 

classifier = new CvHaarClassifierCascade(cvLoad(classifierFile.getAbsolutePath())); 

// deleting temp file 
classifierFile.delete(); 
if (classifier.isNull()) { 
    throw new IOException("Could not load the classifier file."); 
} 

からの例を使用し、それが役に立てば幸い!