Доброго времени суток! Встретился с не очень хорошей ситуацией. Прочел статью на хабре про обучение классификатора хаара,сделал все,как нужно. В примере который там выложили у меня все работает. Но в другие проекты (к примеру ) он наотрез отказывается считывать мой xml файл! Не понимаю,почему так.
Примерная структура моего xml файла:
<?xml version="1.0"?>
<opencv_storage>
<cascade>
<stageType>BOOST</stageType>
<featureType>HAAR</featureType>
<height>24</height>
<width>14</width>
<stageParams>
<boostType>GAB</boostType>
<minHitRate>9.9500000476837158e-001</minHitRate>
<maxFalseAlarm>4.0000000596046448e-001</maxFalseAlarm>
<weightTrimRate>9.4999999999999996e-001</weightTrimRate>
<maxDepth>1</maxDepth>
<maxWeakCount>100</maxWeakCount></stageParams>
<featureParams>
<maxCatCount>0</maxCatCount>
<mode>ALL</mode></featureParams>
<stageNum>16</stageNum>
<stages>
<!-- stage 0 -->
<_>
<maxWeakCount>6</maxWeakCount>
<stageThreshold>-1.1054115295410156e+000</stageThreshold>
<weakClassifiers>
<_>
....
Примерная структура xml файлов,которые читаются практически везде:
<?xml version="1.0"?>
<!--
-->
<opencv_storage>
<A_gest type_id="opencv-haar-classifier">
<size>
24 24</size>
<stages>
<_>
<!-- stage 0 -->
<trees>
<_>
<!-- tree 0 -->
<_>
<!-- root node -->
<feature>
<rects>
<_>
3 3 9 16 -1.</_>
<_>
3 7 9 8 2.</_></rects>
<tilted>0</tilted></feature>
<threshold>-0.0223442204296589</threshold>
<left_val>0.7737345099449158</left_val>
<right_val>-0.9436557292938232</right_val></_></_>
<_>
Думал от версии OpenCV зависит- но нет, в старом OpenCV (2.2 ) у меня получился в точности такой же xml по структуре.
Затем решил уже обученные каскады Хаара загрузить в готовый проект по детектированию лица, и НЕТ,опять! Те же ошибки пошли,только теперь и на другие проекты.
Прошу помощи, голову сломал уже.
Пример последнего кода,который я запускал и в котором вылетала ошибка
/**
* @file facedetect.cpp
* @author Karan Thakkar
* @brief Uses the Haar Cascade Classifier to detect face and eyes in a video feed
*/
#include "opencv2/objdetect/objdetect.hpp"
#include <opencv2\highgui\highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <sys/stat.h>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay(IplImage* frame);
/** Global variables */
CvHaarClassifierCascade* face_cascade = 0;
CvHaarClassifierCascade* eye_cascade = 0;
CvMemStorage* pStorageface = 0;
CvMemStorage* pStorageeye = 0; // memory for detector to use
RNG rng(12345);
/**
* @function main
*/
int main(int argc, const char** argv)
{
CvCapture* capture;
IplImage* frame = 0;
//-- 1. Load the cascade
face_cascade = (CvHaarClassifierCascade *)cvLoad("C:\opencv\build\etc\haarcascades\haarcascade_frontalface_alt_tree.xml", 0, 0, 0);
eye_cascade = (CvHaarClassifierCascade *)cvLoad("C:\opencv\build\etc\haarcascadeshaarcascade_eye_tree_eyeglasses.xml", 0, 0, 0);
while (true)
{
//-- 2. Read the video stream
capture = cvCaptureFromCAM(1);
frame = cvQueryFrame(capture);
//-- 3. Apply the classifier to the frame
detectAndDisplay(frame);
int c = waitKey(10);
if ((char)c == 27) { exit(0); }
}
// clean up and release resources
cvReleaseImage(&frame);
if (face_cascade) cvReleaseHaarClassifierCascade(&face_cascade);
if (pStorageface) cvReleaseMemStorage(&pStorageface);
if (pStorageeye) cvReleaseMemStorage(&pStorageeye);
return 0;
}
/**
* @function detectAndDisplay
*/
void detectAndDisplay(IplImage* frame)
{
CvSeq * pFaceRectSeq; // memory-access interface
CvSeq * pEyeRectSeq;
pStorageface = cvCreateMemStorage(0);
// detect faces in image
pFaceRectSeq = cvHaarDetectObjects
(frame, face_cascade, pStorageface,
1.1, // increase search scale by 10% each pass
3, // merge groups of three detections
CV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a face
cvSize(40, 40)); // smallest size face to detect = 40x40
pStorageeye = cvCreateMemStorage(0);
// detect faces in image
pEyeRectSeq = cvHaarDetectObjects
(frame, eye_cascade, pStorageeye,
1.1, // increase search scale by 10% each pass
3, // merge groups of three detections
CV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a face
cvSize(40, 40)); // smallest size face to detect = 40x40
const char * DISPLAY_WINDOW = "Haar Window";
int i;
// create a window to display detected faces
cvNamedWindow(DISPLAY_WINDOW, CV_WINDOW_AUTOSIZE);
// draw a rectangular outline around each detection
for (i = 0; i<(pFaceRectSeq ? pFaceRectSeq->total : 0); i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pFaceRectSeq, i);
CvPoint pt1 = { r->x, r->y };
CvPoint pt2 = { r->x + r->width, r->y + r->height };
cvRectangle(frame, pt1, pt2, CV_RGB(255, 255, 255), 3, 4, 0);
}
// draw a rectangular outline around each detection
for (i = 0; i<(pEyeRectSeq ? pEyeRectSeq->total : 0); i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pEyeRectSeq, i);
CvPoint pt1 = { r->x, r->y };
CvPoint pt2 = { r->x + r->width, r->y + r->height };
cvRectangle(frame, pt1, pt2, CV_RGB(255, 255, 255), 3, 4, 0);
}
// display face detections
cvShowImage(DISPLAY_WINDOW, frame);
}
У меня стоит OpenCV 3.1 , VisualStudio 2015. Win7 64 bit.