简体   繁体   English

OpenCV Haar Cascade xml格式

[英]OpenCV Haar Cascade xml format

I'm interested in face detection and I'm trying to use OpenCV for my Delphi application. 我对面部检测很感兴趣,我正在尝试将OpenCV用于我的Delphi应用程序。 I'm going to use Google Picasa for facial recognition later. 我稍后会使用Google Picasa进行面部识别。 And I've found 4 different basic haar cascades for detecting frontal faces: 我发现了4种不同的基本haar级联用于检测正面:

haarcascade_frontalface_default.xml
haarcascade_frontalface_alt.xml
haarcascade_frontalface_alt2.xml
haarcascade_frontalface_alt_tree.xml

The 'default' haarcascade gives too many false positives and is not good at all, while the 'alt', 'alt2' and 'tree' cascades seem to produce quite accurate, but sometimes different, results. “默认”haarcascade给出了太多误报并且根本不好,而'alt','alt2'和'tree'级联似乎产生了相当准确但有时不同的结果。 The 'tree' cascade tends to produce more false negatives than the 'alt' and 'alt2' ones, but the least false positives as well. 'tree'级联倾向于产生比'alt'和'alt2'更多的假阴性,但也是最少的误报。

The problem is that there're two different formats for these haarcascade xml files. 问题是这些haarcascade xml文件有两种不同的格式。

First like this: https://github.com/Danukeru/FOUCAM/blob/master/haarcascade_frontalface.xml 首先是这样的: https//github.com/Danukeru/FOUCAM/blob/master/haarcascade_frontalface.xml

And second like this: https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt.xml 第二个是这样的: https//github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt.xml

If I'm not mistaken, the second one is considered the 'new' format, but my app currently understands only the first 'old' format. 如果我没有弄错,第二个被认为是“新”格式,但我的应用程序目前只能理解第一个“旧”格式。 The 'new' cascades can probably be more accurate and I'd like to give them a try. “新”级联可能更准确,我想尝试一下。

Is there any way to convert the new format cascades to an old format? 有没有办法将新格式级联转换为旧格式?

Or how else can I use them in my app? 或者我如何在我的应用程序中使用它们?

Here's some code (from sites.google.com/site/josejp1/index/OCV.ZIP): 以下是一些代码(来自sites.google.com/site/josejp1/index/OCV.ZIP):

f1 := 'haarcascade_frontalface_alt.xml';
file1 := PChar(f1);
cascade_f := cvLoad(file1,0,0,0);
storage := cvCreateMemStorage(0);

s.width := 40;
s.height := 40;
faces := cvHaarDetectObjects(PCvArr(img), cascade_f, storage, 1.1, 3, 0, s);

And if I try to use the 'new' format xml cascade file I get an error: 如果我尝试使用'新'格式的xml级联文件,我会收到一个错误:

OpenCV GUI Error Handler
---------------------------
Unspecified error (The node does not represent a user object (unknown type?))
in function cvRead, ..\..\cxcore\src\cxpersistence.cpp(5061)

I haven't found any ready-to-use examples in Delphi with new C++ API so I've converted this example of detectMultiScale (a C++ version of cvHaarDetectObjects) to Delphi. 我没有在Delphi中找到任何带有新C ++ API的现成示例,所以我将这个detectMultiScale示例(cvHaarDetectObjects的C ++版本)转换为Delphi。 It compiles but crashes when tries to load a cascade. 它会在尝试加载级联时编译但崩溃。 Im new to C++ so any help would be appreciated. 我是C ++的新手,所以任何帮助都会受到赞赏。

Delphi 2010 project in zip archive zip存档中的Delphi 2010项目

unit Unit1;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  ExtCtrls,
  DateUtils,
  ExtDlgs,
  highgui_c,
  core_c,
  Core.types_c,
  imgproc_c,
  imgproc.types_c,
  objdetect;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button2: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
    mystorage: pCvMemStorage = nil;
    mycascade : pCvHaarClassifierCascade = nil;
    mycascade_name: AnsiString = 'cascades\haarcascade_frontalface_alt.xml';

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);

var img, grayImage : PIplImage;
    myobjects : TArray<TCvRect>;
    myscale : double;
    i: integer;
    r: pCvRect;
    mycolors : array[0..7] of TCvScalar;
    myCascadeClassifier : TCascadeClassifier;

begin

   mycolors[0] := CvScalar(0,0,255);
   mycolors[1] := CvScalar(0,128,255);
   mycolors[2] := CvScalar(0,255,255);
   mycolors[3] := CvScalar(0,255,0);
   mycolors[4] := CvScalar(255,128,0);
   mycolors[5] := CvScalar(255,255,0);
   mycolors[6] := CvScalar(255,0,0);
   mycolors[7] := CvScalar(255,0,255);

   img := cvLoadImage('lena.jpg');
   grayImage := cvCreateImage(cvGetSize(img),8,1);
   cvCvtColor(img,grayImage,CV_BGR2GRAY);
   cvShowImage('gray', grayImage);
   mystorage := cvCreateMemStorage(0);

   Memo1.Lines.Add('Memory allocated');

   mycascade := cvLoad('cascade.xml');
   myscale := 1.3;

   cvClearMemStorage(mystorage);
   myobjects := nil;
   myCascadeClassifier.detectMultiScale(grayImage,myobjects,1.1,3,CV_HAAR_SCALE_IMAGE or CV_HAAR_DO_CANNY_PRUNING,cvSize(0,0),cvSize(40,40));

   Memo1.Lines.Add('Object size? : ' + IntToStr(Length(myobjects)));

   for i := 0 to (Length(myobjects)-1) do
    begin
      cvRectangle(grayImage,cvPoint(r.x,r.y),cvPoint(r.x+r.width,r.y+r.height),CvScalar(0,0,255));
    end;

   cvNamedWindow('Output');
   cvShowImage('Output', grayImage);

   cvReleaseImage(grayImage);
   cvReleaseImage(img);

end;

end.

@TLama: I'm using the newest version of Delphi-OpenCV for this project with detectMultiScale, and some outdated (but working) version for the first project with cvHaarDetectObjects mentioned in the first post (sites.google.com/site/josejp1/index/OCV.ZIP). @TLama:我正在使用最新版本的Delphi-OpenCV用于带有detectMultiScale的项目,以及第一个项目中使用cvHaarDetectObjects的第一个项目的一些过时(但工作)版本(sites.google.com/site/josejp1/ / OCV.ZIP)指数。

if those delphi bindings are using the outdated c-api, (cvHaarDetectObjects) - you can only use the old format. 如果这些delphi绑定使用过时的c-api,(cvHaarDetectObjects) - 你只能使用旧的格式。

the c++ api ( cv::CascadeClassifier ) supports both, as well as hog and lbp cascades. c ++ api(cv :: CascadeClassifier)支持这两种,以及hog和lbp级联。

again, it's a limitation of the old c-api. 再次,它是旧c-api的限制。 if you can, avoid it ! 如果可以的话,避免它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM