YoloV4可以用opencv4.0以上加载和使用。但是Lazarus只能支持到opencv2.4.13。所以不能直接使用yolov4,后来只好用VC写了一个dll,调用Opencv4.81再调用YoloV4。

在Lazarus下调用这个dll就可以用yolov4了,方法如下

一、声明dll库

//初始化Yolo库
function YoloInit(cfgfile:pchar;wfile:pchar):Integer; stdcall; external 'LazarusYoloV4dll.dll';

//识别数据 imageFile:要识别的文件名;Yolosize:识别大小320 416 512 608等;FZ:阀值(0-1的小数);JsonStr:输出的字符串
function YoloImageFile(imageFile:PChar;Yolosize:Integer;FZ:Double;JsonStr:PChar):Integer; stdcall; external 'LazarusYoloV4dll.dll';

 

二、初始化

if YoloInit('E:/VC/opencv4.81/ConsoleApplication1/x64/Debug/yolov4-tiny.cfg','E:/VC/opencv4.81/ConsoleApplication1/x64/Debug/yolov4-tiny.weights')<0 then
begin
ShowMessage('初始化出错');
end;

三、定义类型

classIDName:array[0..79] of String=('人','自行车','车','摩托车','飞机','公共汽车','火车','车','船',
'红绿灯','消防栓','停车标志','停车计时器','长凳','鸟','猫','狗','马','羊',
'牛','大象','熊','斑马','长颈鹿','背包','雨伞','手提包','领带','手提箱','飞盘',
'滑雪板','滑雪板','运动球','风筝','棒球棒','棒球手套','滑板','冲浪板',
'网球拍','瓶子','酒杯','杯子','叉子','刀','勺子','碗','香蕉','苹果','三明治',
'橙子','西兰花','胡萝卜','热狗','披萨','甜甜圈','蛋糕','椅子','沙发','盆栽','床','餐桌',
'马桶','电视','笔记本电脑','鼠标','遥控器','键盘','手机','微波炉','烤箱','烤面包机','水槽','冰箱',
'书','钟','花瓶','剪刀','泰迪熊','吹风机','牙刷');

四、识别

procedure TForm1.Button3Click(Sender: TObject);
var
Pjson:PChar;
rs,i,top1,left1,height1,width1:Integer;
ObjSt:String;
jsonst,jsonpost:TJSONObject;
jsonposts:TJSONArray;
begin
Pjson:=StrAlloc(1024*1024);
rs:=YoloImageFile('./test2.jpg' ,320,0.6,Pjson); 
if rs=0 then
begin
Memo1.Text:=Pjson;
jsonst:=TJSONObject(GetJSON(pjson));
rs:=jsonst.Get('DataSize',0);
jsonposts:=jsonst.Arrays['data'];

Image1.Canvas.Brush.Style := bsClear;
Image1.Canvas.Pen.Color:=clBlue;
Image1.Canvas.Pen.Width:=1;
Image1.Canvas.Font.Size:=16;
Image1.Canvas.Font.Color:=clRed;

for i:=0 to rs-1 do
begin
jsonpost:=TJSONObject(jsonposts[i]);
top1:=jsonpost.Get('top',0) ;
left1:=jsonpost.Get('left',0);
width1:=jsonpost.Get('width',0);
height1:=jsonpost.Get('height',0);
Image1.Canvas.Frame(left1,top1,left1+width1,top1+height1); //画框
ObjSt:=Format('%s:%.2f',[ classIDname[jsonpost.Get('classId')],jsonpost.Get('confidence',0.0)]);
Image1.Canvas.TextOut(left1,top1-18,ObjSt);
end;

jsonst.Free;
end;

StrDispose(Pjson

 

);
end;

ok完成

当前DLL是64位的,还要下载opencv_world481.dll才能使用。

LazarusYoloV4dll.dll下载地址

链接:https://pan.baidu.com/s/1dMY8X0ojy3vmuzKCwalcUQ
提取码:yolo

 

posted on 2024-03-15 22:36  禁卫军  阅读(39)  评论(0编辑  收藏  举报