加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Unit1.pas 7.63 KB
一键复制 编辑 原始数据 按行查看 历史
dkq811230 提交于 2022-06-16 08:30 . 人像识别
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.Imaging.jpeg, System.Generics.Collections,pngimage,faceUtils,functionUtils;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
OpenDialog2: TOpenDialog;
Button3: TButton;
Panel1: TPanel;
Image1: TImage;
Panel2: TPanel;
Image2: TImage;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
featureData1:TBytes;
featureData2:TBytes;
implementation
{$R *.dfm}
uses
math;
procedure TForm1.Button1Click(Sender: TObject);
var
sExt,hex:string;
lBitmap :TBitmap;
faceInfo:ASF_MultiFaceInfo;
singlefaceInfo: ASF_SingleFaceInfo;
iRet: integer;
m:TMemoryStream;
imgData:TBytes;
feature:ASF_FaceFeature;
lImgDataInfo: TImgDataInfo;
begin
if OpenDialog1.Execute(self.Handle) then
begin
lBitmap := TBitmap.Create;
try
sExt := ExtractFileExt(OpenDialog1.FileName);
//检测并提取人脸特征
if (CompareText(sExt, '.jpg') = 0) or (CompareText(sExt, '.jpeg') = 0)
then
ReadJpegFile(OpenDialog1.FileName, lBitmap)
else if (CompareText(sExt, '.bmp') = 0) then
ReadBmpFile(OpenDialog1.FileName, lBitmap)
else if (CompareText(sExt, '.png') = 0) then
ReadPngFile(OpenDialog1.FileName, lBitmap)
else
Exit;
Image1.Picture.Assign(lBitmap);
ReadBmpToImgDataInfo(lBitmap,lImgDataInfo);
iRet:=ASFDetectFaces(FFaceHandle,lImgDataInfo.Width,lImgDataInfo.Height,ASVL_PAF_RGB24_B8G8R8,PMuint8(
lImgDataInfo.pImgData),@faceInfo);
if iRet=0 then
begin
Memo1.Lines.Add('人像数为:'+intTostr(faceInfo.faceNum));
if faceInfo.faceNum=1 then
begin
ZeroMemory(@singlefaceInfo,SizeOf(ASF_SingleFaceInfo));
singlefaceInfo.faceRect.left:=faceInfo.faceRect[0].left;
singlefaceInfo.faceRect.top:=faceInfo.faceRect[0].top;
singlefaceInfo.faceRect.right:=faceInfo.faceRect[0].right;
singlefaceInfo.faceRect.bottom:=faceInfo.faceRect[0].bottom;
singlefaceInfo.faceOrient:=faceInfo.faceOrient[0];
ZeroMemory(@feature,SizeOf(ASF_FaceFeature));
iRet:=ASFFaceFeatureExtract(FFaceHandle,lImgDataInfo.Width,lImgDataInfo.Height,ASVL_PAF_RGB24_B8G8R8,PMuint8(lImgDataInfo.pImgData),@singlefaceInfo,@feature);
if iRet=0 then
begin
if feature.featureSize=1032 then
begin
ZeroMemory(@featureData1,SizeOf(ASF_FaceFeature));
SetLength(featureData1,feature.featureSize);
Move(feature.feature^,featureData1[0],Length(featureData1));
hex:=BytesToHex(featureData1);
Memo1.Lines.Add('提取人像特征值成功:'+hex);
end;
end
else
begin
Memo1.Lines.Add('提取人像特征值失败:'+intTostr(iRet));
end;
end;
end
else
begin
Memo1.Lines.Add('人像数判断出错:'+intTostr(iRet));
end;
finally
freeandNil(lBitmap);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
sExt,hex:string;
lBitmap :TBitmap;
faceInfo:ASF_MultiFaceInfo;
singlefaceInfo: ASF_SingleFaceInfo;
iRet: integer;
m:TMemoryStream;
imgData:TBytes;
feature:ASF_FaceFeature;
lImgDataInfo: TImgDataInfo;
begin
if OpenDialog2.Execute(self.Handle) then
begin
lBitmap := TBitmap.Create;
try
sExt := ExtractFileExt(OpenDialog2.FileName);
//检测并提取人脸特征
if (CompareText(sExt, '.jpg') = 0) or (CompareText(sExt, '.jpeg') = 0)
then
ReadJpegFile(OpenDialog2.FileName, lBitmap)
else if (CompareText(sExt, '.bmp') = 0) then
ReadBmpFile(OpenDialog2.FileName, lBitmap)
else if (CompareText(sExt, '.png') = 0) then
ReadPngFile(OpenDialog1.FileName, lBitmap)
else
Exit;
Image2.Picture.Assign(lBitmap);
ReadBmpToImgDataInfo(lBitmap,lImgDataInfo);
iRet:=ASFDetectFaces(FFaceHandle,lImgDataInfo.Width,lImgDataInfo.Height,ASVL_PAF_RGB24_B8G8R8,PMuint8(
lImgDataInfo.pImgData),@faceInfo);
if iRet=0 then
begin
Memo1.Lines.Add('人像数为:'+intTostr(faceInfo.faceNum));
if faceInfo.faceNum=1 then
begin
ZeroMemory(@singlefaceInfo,SizeOf(ASF_SingleFaceInfo));
singlefaceInfo.faceRect.left:=faceInfo.faceRect[0].left;
singlefaceInfo.faceRect.top:=faceInfo.faceRect[0].top;
singlefaceInfo.faceRect.right:=faceInfo.faceRect[0].right;
singlefaceInfo.faceRect.bottom:=faceInfo.faceRect[0].bottom;
singlefaceInfo.faceOrient:=faceInfo.faceOrient[0];
ZeroMemory(@feature,SizeOf(ASF_FaceFeature));
iRet:=ASFFaceFeatureExtract(FFaceHandle,lImgDataInfo.Width,lImgDataInfo.Height,ASVL_PAF_RGB24_B8G8R8,PMuint8(lImgDataInfo.pImgData),@singlefaceInfo,@feature);
if iRet=0 then
begin
if feature.featureSize=1032 then
begin
ZeroMemory(@featureData2,SizeOf(ASF_FaceFeature));
SetLength(featureData2,feature.featureSize);
Move(feature.feature^,featureData2[0],Length(featureData2));
hex:=BytesToHex(featureData2);
Memo1.Lines.Add('提取人像特征值成功:'+hex);
end;
end
else
begin
Memo1.Lines.Add('提取人像特征值失败:'+intTostr(iRet));
end;
end;
end
else
begin
Memo1.Lines.Add('人像数判断出错:'+intTostr(iRet));
end;
finally
freeandNil(lBitmap);
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
faceFeature1,faceFeature2:ASF_FaceFeature;
byteData:TBytes;
mr:integer;
score:MFloat;
begin
try
try
//初始化数据
ZeroMemory(@faceFeature1,SizeOf(ASF_FaceFeature));
ZeroMemory(@faceFeature2,SizeOf(ASF_FaceFeature));
//处理第一个特征数据
// byteData:=HexToBytes(StringOf(featureData1));
if Length(featureData1)<>1032 then
begin
raise Exception.Create('特征数据1不正确');
end;
faceFeature1.featureSize:=Length(featureData1);
faceFeature1.feature:= GetMemory(faceFeature1.featureSize);
Move(featureData1[0],faceFeature1.feature^,faceFeature1.featureSize);
//处理第二个特征数据
//byteData:=HexToBytes(featureData));
if Length(featureData2)<>1032 then
begin
raise Exception.Create('特征数据2不正确');
end;
faceFeature2.featureSize:=Length(featureData2);
faceFeature2.feature:=GetMemory(faceFeature2.featureSize);
Move(featureData2[0],faceFeature2.feature^,faceFeature2.featureSize);
mr:=ASFFaceFeatureCompare(FFaceHandle,faceFeature1,faceFeature2,MFloat(score),ASF_LIFE_PHOTO);
if mr<>0 then
begin
raise Exception.Create('人脸数据比较出错:'+inttoStr(mr));
end;
Memo1.Lines.Add('人像特征值比较值为:'+FloatToStr(score));
except
on Ex:Exception do
begin
raise Exception.Create(Ex.Message);
end;
end;
finally
FreeMemory(faceFeature1.feature);
FreeMemory(faceFeature2.feature);
end;
end;
end.
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化