2014-06-16 23:38:35 +02:00
|
|
|
unit ocv.comp.Proc;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
Uses
|
|
|
|
ocv.comp.Types,
|
|
|
|
ocv.objdetect_c,
|
|
|
|
ocv.core.types_c;
|
|
|
|
|
|
|
|
function ocvHaarCascadeTransform(
|
2014-08-06 00:36:22 +02:00
|
|
|
{ } const Source: IocvImage;
|
|
|
|
{ } const Cascade: pCvHaarClassifierCascade;
|
|
|
|
{ } var HaarRects: TocvRects;
|
|
|
|
{ } const MinSize, MaxSize: TcvSize;
|
|
|
|
{ } const Equalize: Boolean = True;
|
|
|
|
{ } const Scale: Double = 1.3;
|
|
|
|
{ } const MinNeighbors: Integer = 3;
|
|
|
|
{ } const Flag: TocvHaarCascadeFlagSet = []): Boolean;
|
2014-06-16 23:38:35 +02:00
|
|
|
|
|
|
|
function ocvLoadHaarCascade(const HaarCascadeType: TocvHaarCascadeType): pCvHaarClassifierCascade;
|
|
|
|
|
2014-08-26 14:00:30 +02:00
|
|
|
function IsRectEmpty(const Rect: TocvRect): Boolean;
|
|
|
|
|
2014-06-16 23:38:35 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
Uses
|
2014-09-29 09:12:47 +02:00
|
|
|
{$IFDEF HAS_UNITSCOPE}
|
2015-03-30 23:02:32 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2014-09-29 09:12:47 +02:00
|
|
|
Winapi.Windows,
|
2015-03-30 23:02:32 +02:00
|
|
|
{$ENDIF MSWINDOWS}
|
2014-06-16 23:38:35 +02:00
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
System.ZLib,
|
2014-08-26 14:00:30 +02:00
|
|
|
{$ELSE}
|
2015-03-30 23:02:32 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2014-08-26 14:00:30 +02:00
|
|
|
Windows,
|
2015-03-30 23:02:32 +02:00
|
|
|
{$ENDIF MSWINDOWS}
|
2014-08-26 14:00:30 +02:00
|
|
|
SysUtils,
|
|
|
|
Classes,
|
|
|
|
ZLib,
|
2014-09-29 09:12:47 +02:00
|
|
|
{$ENDIF}
|
2014-06-16 23:38:35 +02:00
|
|
|
ocv.core_c,
|
|
|
|
ocv.imgproc_c,
|
|
|
|
ocv.cvutils;
|
|
|
|
|
|
|
|
{$I Opencv.inc}
|
|
|
|
|
|
|
|
Type
|
|
|
|
TocvHaarCascadeRecord = record
|
|
|
|
Name: String;
|
|
|
|
FileName: String;
|
|
|
|
end;
|
2014-08-06 00:36:22 +02:00
|
|
|
///
|
2014-06-16 23:38:35 +02:00
|
|
|
// Run utils\CompressHaar\uCompressHaar.dpr
|
|
|
|
// Add to serarch path \Delphi-OpenCV\resource\facedetectxml\
|
2014-08-06 00:36:22 +02:00
|
|
|
///
|
|
|
|
{$R haarcascade.res}
|
2014-06-16 23:38:35 +02:00
|
|
|
{$I haarcascade.inc}
|
|
|
|
|
|
|
|
function ocvLoadHaarCascade(const HaarCascadeType: TocvHaarCascadeType): pCvHaarClassifierCascade;
|
|
|
|
|
|
|
|
function TempPath: string;
|
|
|
|
var
|
|
|
|
BufSize: Cardinal;
|
|
|
|
begin
|
|
|
|
BufSize := GetTempPath(0, nil);
|
|
|
|
SetLength(Result, BufSize);
|
|
|
|
GetTempPath(BufSize, PChar(Result));
|
|
|
|
Result := Trim(Result);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Var
|
|
|
|
FullFileName: String;
|
|
|
|
RS: TResourceStream;
|
|
|
|
DC: TZDecompressionStream;
|
|
|
|
FS: TFileStream;
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
FullFileName := TempPath + FrontalFaceXML[HaarCascadeType].FileName;
|
|
|
|
if not FileExists(FullFileName) then
|
|
|
|
begin
|
|
|
|
RS := TResourceStream.Create(hInstance, FrontalFaceXML[HaarCascadeType].Name, RT_RCDATA);
|
|
|
|
DC := TZDecompressionStream.Create(RS);
|
|
|
|
FS := TFileStream.Create(FullFileName, fmCreate);
|
|
|
|
try
|
|
|
|
FS.CopyFrom(DC, DC.Size);
|
|
|
|
finally
|
|
|
|
DC.Free;
|
|
|
|
FS.Free;
|
|
|
|
RS.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
if FileExists(FullFileName) then
|
|
|
|
Result := cvLoad(c_str(FullFileName), nil, nil, nil);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function ocvHaarCascadeTransform;
|
|
|
|
Var
|
|
|
|
storage: pCvMemStorage;
|
|
|
|
gray: IocvImage;
|
|
|
|
detected_objects: pCvSeq;
|
|
|
|
i: Integer;
|
|
|
|
cvr: pCvRect;
|
2015-03-30 23:02:32 +02:00
|
|
|
// r, g, b: byte;
|
2014-06-16 23:38:35 +02:00
|
|
|
begin
|
|
|
|
SetLength(HaarRects, 0);
|
|
|
|
Result := False;
|
|
|
|
if Assigned(Cascade) then
|
|
|
|
begin
|
|
|
|
storage := cvCreateMemStorage(0);
|
|
|
|
try
|
|
|
|
gray := Source.GrayImage;
|
|
|
|
if Equalize then
|
|
|
|
cvEqualizeHist(gray.IpImage, gray.IpImage);
|
2014-08-06 00:36:22 +02:00
|
|
|
detected_objects := cvHaarDetectObjects(gray.IpImage, Cascade, storage, Scale, MinNeighbors, HaarSetToFlag(Flag), MinSize, MaxSize);
|
2014-06-16 23:38:35 +02:00
|
|
|
if Assigned(detected_objects) then
|
|
|
|
begin
|
|
|
|
SetLength(HaarRects, detected_objects^.total);
|
|
|
|
i := 0;
|
|
|
|
While i < detected_objects^.total do
|
|
|
|
begin
|
|
|
|
cvr := pCvRect(cvGetSeqElem(detected_objects, i));
|
|
|
|
HaarRects[i] := ocvRect(cvr^.X, cvr^.Y, (cvr^.X) + (cvr^.Width), (cvr^.Y) + (cvr^.Height));
|
|
|
|
Inc(i);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Result := True;
|
|
|
|
finally
|
|
|
|
cvReleaseMemStorage(storage);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-08-26 14:00:30 +02:00
|
|
|
function IsRectEmpty(const Rect: TocvRect): Boolean;
|
|
|
|
begin
|
|
|
|
Result := (Rect.Right <= Rect.Left) or (Rect.Bottom <= Rect.Top);
|
|
|
|
end;
|
|
|
|
|
2014-06-16 23:38:35 +02:00
|
|
|
end.
|