YOLO添加中文标签

YOLO默认输出检测物体的英文名称,我们需要显示本土化,本来以为只需要在代码添加label的地方汉化就可以,后发现不会有效果。
参考YOLO应用化之添加中文支持
可能是点背,我遇到的问题要比上边提到的多,故我又写了篇博客记录我的心酸历程,希望能帮到更多人。
修改步骤如下

1、修改data/labels/make_labels.py

# -*- coding: utf-8 -*-    
import os   

#l=[]
#with open("coco.names") as list_in:
#    for line in list_in:
#        l.append(line)

l=["人","自行车","车","摩托车","飞机","大巴","火车","卡车","船","交通灯","消防栓","停止标识","停车计时器","长凳","鸟","猫","狗","马","羊","牛","大象","熊","斑马","长颈鹿","背包","伞","手提包","领带","手提箱","飞盘","雪橇","滑雪板","体育用球","风筝","棒球棒","棒球手套","滑板","冲浪板","网球拍","瓶子","红酒杯","杯子","叉子","小刀","勺子","碗","香蕉","苹果","三明治","橘子","西兰花","萝卜","热狗","披萨","甜甜圈","蛋糕","椅子","沙发","盆栽","床","餐桌","厕所","显示器","笔记本","鼠标","遥控","键盘","手机","微波炉","烤箱","吐司机","水槽","冰箱","书","闹钟","花瓶","剪刀","泰迪熊","吹风机","牙刷"]
#l= ["人","自行车"]

def make_labels(s): 
    i = 0 
    for word in l:   
        os.system("convert -fill black -background white -bordercolor white -border 4  -font /usr/share/fonts/wqy-microhei/wqy-microhei.ttc -pointsize %d label:\"%s\" \"cn_%d_%d.png\""%(s,word,i,s/12-1)) 
        i = i + 1 

for i in [12,24,36,48,60,72,84,96]:
    make_labels(i)

2、执行 python make_labels.py
出错:convert: not found提示
解决:安装imagemagick
yum install ImageMagick ImageMagick-devel
再执行python make_labels.py
出错:convert: not authorized `}’ @ error/constitute.c/ReadImage/454.
解决:找到policy.xml, 注释LABEL行,如下
这里写图片描述
再执行python make_labels.py,成功生成80个中文类别8种规格的label。
3、修改image.c

diff --git a/src/image.c b/src/image.c
index ac1b629..db62c59 100644
--- a/src/image.c
+++ b/src/image.c
@@ -9,7 +9,9 @@
 #include "stb_image.h"
 #define STB_IMAGE_WRITE_IMPLEMENTATION
 #include "stb_image_write.h"
-
+#ifdef OPENCV
+#define CHINESE
+#endif
 int windows = 0;

 float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
@@ -144,7 +146,21 @@ image get_label(image **characters, char *string, int size)
     free_image(label);
     return b;
 }
+#ifdef CHINESE
+image get_label_chinese(image **characters, int class, int size)
+{
+    if(size > 7) size = 7;
+    image label = make_empty_image(0,0,0);
+    image l = characters[size][class];
+    image n = tile_images(label, l, -size - 1 + (size+1)/2);
+    free_image(label);
+    label = n;

+    image b = border_image(label, label.h*.25);
+    free_image(label);
+    return b;
+}
+#endif
 void draw_label(image a, int r, int c, image label, const float *rgb)
 {
     int w = label.w;
@@ -226,11 +242,20 @@ image **load_alphabet()
     image **alphabets = calloc(nsize, sizeof(image));
     for(j = 0; j < nsize; ++j){
         alphabets[j] = calloc(128, sizeof(image));
+#ifdef CHINESE
+       for(i = 0; i < 80; i++)
+       {
+           char buff[256];
+           sprintf(buff, "data/labels/cn_%d_%d.png", i, j);
+           alphabets[j][i] = load_image_color(buff, 0, 0);
+       }
+#else
         for(i = 32; i < 127; ++i){
             char buff[256];
             sprintf(buff, "data/labels/%d_%d.png", i, j);
             alphabets[j][i] = load_image_color(buff, 0, 0);
         }
+#endif
     }
     return alphabets;
 }
@@ -290,7 +315,12 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,

             draw_box_width(im, left, top, right, bot, width, red, green, blue);
             if (alphabet) {
-                image label = get_label(alphabet, labelstr, (im.h*.03)/10);
+#ifdef CHINESE
+                image label = get_label_chinese(alphabet, class, (im.h*.03)/10);
+#else
+
+               image label = get_label(alphabet, names[class], (im.h*.03)/10);
+#endif
                 draw_label(im, top + width, left, label, rgb);
                 free_image(label);
             }

4、重新编译,执行
./darknet detect cfg/yolo.cfg yolo.weights data/horses.jpg

你可能感兴趣的:(计算机视觉,本土化,YOLO)