2016-06-18 82 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 haarcascades XML資源文件轉換爲cvLoad函數,該函數不是用來處理Java資源文件的,因爲它是一個C++函數,它不知道這個概念。

我有同樣的問題,我發現是抄襲從Java資源到臨時目錄中該XML文件的唯一的解決方法,將其提供給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."); 
} 

希望它可以幫助一個例子!