欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

win10 YOLO v2 v3添加中文标签

程序员文章站 2022-05-27 16:13:06
...

这个方向各个博客步骤有些乱,多是linux,自己win10 64位,根据自己实现整理。

step1:install ImageMagick

https://www.imagemagick.org/script/download.php
下第一个。
win10 YOLO v2 v3添加中文标签

step2:下载ukai.ttc

下载ukai.ttc放在labels文件夹下
https://download.csdn.net/download/qq_35608277/10351225
2积分,没有o积分选项。没有留言发。

step3:make label png

darknet提供了制作png图片的脚本,data/labels/make_labels.py

#下载ukai.ttc的路径
font = 'H:\\deeplearning\\yolov2\\darknet-masterv2\\darknet-master\\build\\darknet\\x64\\data\\labels\\ukai.ttc' # 'futura-normal'

def make_labels(s):
    l = ["车"]
    for word in l:
            os.system("convert -fill black -background white -bordercolor white -border 4  -font %s -pointsize %d label:\"%s\" \"cn_0_%d.png\""%(font,s,word,s/12-1)) #cn_0_0/1/2/3/4/5/6/7.png如果多类自己改为cn_1_%d,,依次输出吧,没有研究出来怎么遍历。。
for i in [12,24,36,48,60,72,84,96]:
    make_labels(i)

在该目录下,shift右键,打开powershell,

python make_labels.py

即可生成图片。

step4.更改模型源代码

修改image.c
https://github.com/PaulChongPeng/darknet/commit/798fe7cc4176d452a83d63eb261d6129e397a521
跟着这个改。

image **load_alphabet()
{
    int i, j;
    const int nsize = 8;
    image **alphabets = calloc(nsize, sizeof(image));
    for(j = 0; j < nsize; ++j){
        alphabets[j] = calloc(128, sizeof(image));
#ifdef CHINESE
         for (i = 0; i <1; i++) {//i=0;i<80;i++  coco 一共八十类//i是类数,这里自己只有一类
            char buff[256];
            sprintf(buff, "data/labels/cn%d_%d.png", i, j);//png 命名cn0_0/1/2/3/4/5/6/7
            alphabets[j][i] = load_image_color(buff, 0, 0);

        }

这里直接把大小固定了

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[3][class];//改尺寸 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;
    }

不知为什么输出文字描述的地方name[class]是乱码

void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
    int i;

    for(i = 0; i < num; ++i){
        int class = max_index(probs[i], classes);
        float prob = probs[i][class];
        if(prob > thresh){

            //// for comparison with OpenCV version of DNN Darknet Yolo v2
            //printf("\n %f, %f, %f, %f, ", boxes[i].x, boxes[i].y, boxes[i].w, boxes[i].h);
            // int k;
            //for (k = 0; k < classes; ++k) {
            //  printf("%f, ", probs[i][k]);
            //}
            //printf("\n");

            int width = im.h * .012;

            if(0){
                width = pow(prob, 1./2.)*10+1;
                alphabet = 0;
            }

            printf("%s: %.0f%%\n",names[class], prob*100);//显示类名:预测概率

改线宽

void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
    int i;

    for(i = 0; i < num; ++i){
        int class = max_index(probs[i], classes);
        float prob = probs[i][class];
        if(prob > thresh){

            //// for comparison with OpenCV version of DNN Darknet Yolo v2
            //printf("\n %f, %f, %f, %f, ", boxes[i].x, boxes[i].y, boxes[i].w, boxes[i].h);
            // int k;
            //for (k = 0; k < classes; ++k) {
            //  printf("%f, ", probs[i][k]);
            //}
            //printf("\n");

            int width = im.h * .012;

            if(0){
                width = pow(prob, 1./2.)*10+1;
                alphabet = 0;
            }

            printf("%s: %.0f%%\n",names[class], prob*100);//显示类名:预测概率
            int offset = class*123457 % classes;
            float red = get_color(2,offset,classes);
            float green = get_color(1,offset,classes);
            float blue = get_color(0,offset,classes);
            float rgb[3];

            //width = prob*20+2;

            rgb[0] = red;
            rgb[1] = green;
            rgb[2] = blue;
            box b = boxes[i];

            int left  = (b.x-b.w/2.)*im.w;
            int right = (b.x+b.w/2.)*im.w;
            int top   = (b.y-b.h/2.)*im.h;
            int bot   = (b.y+b.h/2.)*im.h;

            if(left < 0) left = 0;
            if(right > im.w-1) right = im.w-1;
            if(top < 0) top = 0;
            if(bot > im.h-1) bot = im.h-1;

            draw_box_width(im, left, top, right, bot, width/2, red, green, blue);//width=width/2
            if (alphabet) {
#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/2, left, label, rgb);//width=width/2

yolov3 class对应的参数改一下,其他类似。
ref
https://blog.csdn.net/hrsstudy/article/details/61671886