环境:vmware虚拟机,ubuntun系统
- 制作中文标签
在 data/labels 文件夹下新建一个python文件,并运行
(my.names 文件中存放你要显示的中文名)
import os
import string
import pipes
font = 'futura-normal'
fileName=open("my.names",'r')
l=[label.lstrip().rstrip() for label in fileName.readlines()]
for i in range(8):
index=0
for word in l:
os.system(
"convert -fill black -background white -bordercolor white -border 4 -font /usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf -pointsize %d label:\"%s\" \"%d_%s.png\"" % ((i+1)*12, word, i,index))
index+=1
我的 my.names 文件
人
自行车
汽车
摩托车
飞机
公共汽车
火车
卡车
船
交通灯
消防栓
停车标志
停车收费表
长凳
鸟
猫
狗
马
羊
奶牛
大象
熊
斑马
长颈鹿
背包
雨伞
手提包
领带
手提箱
飞盘
滑雪板
滑雪板
运动球
风筝
棒球棒
棒球手套
滑板
冲浪板
网球拍
瓶子
酒杯
杯子
叉
刀
勺子
碗
香蕉
苹果
三明治
橙色
花椰菜
胡萝卜
热狗
披萨
甜甜圈
蛋糕
椅子
沙发
盆栽植物
床
迪宁表
厕所
电视监视器
笔记本电脑
老鼠
遥远的
键盘
手机
微波炉
烤箱
烤面包机
下沉
冰箱
书
时钟
花瓶
剪刀
泰迪熊
吹风机
牙刷
- 在 src 目录下修改 image.c 中的代码
1)在get_label_v3函数下添加自己的这个函数
image get_label_v3_my(image **characters, char *labelindex, int size)
{
size = size / 10;
if (size > 7) size = 7;
image label = make_empty_image(0, 0, 0);
int class,i=0,nlabels=1;
int len=strlen(labelindex);
for(i=0;i<len;i++)
{
if(labelindex[i]==',') ++nlabels;
}
for(i=0;i<nlabels;i++){
class=atoi(labelindex);
image l = characters[size][class];
image n = tile_images(label, l, -size - 1 + (size + 1) / 2);
free_image(label);
label = n;
labelindex=strchr(labelindex,',')+1;
}
image b = border_image(label, label.h*.25);
free_image(label);
return b;
}
2)修改 draw_detections_v3 函数中的代码块,替换成下面的
if (alphabet) {
char labelindex[100]= { 0 };
char class[10]={0};
sprintf(class,"%d",selected_detections[i].best_class);
strcat(labelindex, class);
int j;
for (j = 0; j < classes; ++j) {
if (selected_detections[i].det.prob[j] > thresh && j != selected_detections[i].best_class) {
strcat(labelindex,", ");
sprintf(class,"%d",selected_detections[i].best_class);
strcat(labelindex, class);
}
}
image label = get_label_v3_my(alphabet, labelindex, (im.h*.03));
draw_label(im, top + width, left, label, rgb);
free_image(label);
}
3)修改 load_alphabet() 函数
image **load_alphabet()
{
int i, j;
const int nsize = 8;
image** alphabets = (image**)xcalloc(nsize, sizeof(image*));
for(j = 0; j < nsize; ++j){
alphabets[j] = (image*)xcalloc(128, sizeof(image));
//将此处改为自己的标签类别(我的 my.names 文件生成了8个尺寸和80个类别的中文标签)
for(i = 0; i < 80; ++i){
char buff[256];
//读取生成的中文标签
sprintf(buff, "data/labels/%d_%d.png", j, i);
alphabets[j][i] = load_image_color(buff, 0, 0);
}
}
return alphabets;
}
4)重新编译并运行
$ make clean
$ make
- 测试
./darknet detect cfg/yolov3.cfg yolov3.weights data/person.jpg