YOLOv3将产生的检测结果依序保存到指定文本文件

这是对修改YOLOv3源码的又一次尝试,由于附带了相关文件的全文,故显得文章比较长,其实总共仅修改了4处。

针对src文件夹下的image.cdemo.c两个C文件进行如下修改:

1、对image.c的修改

共两处修改,都在draw_detections函数中,详见该函数中的注释:


void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
   
    int i,j;
    changed by  dingjing,以下
    char filetosave[100] = "/home/dj/PycharmProjects/NewTest/videotxt/1.txt";
    FILE *fp;
    fp = fopen(filetosave, "a+");//追加内容
    if(fp == NULL)
    {
   
	printf("1.txt cannot open");
	return -1;
    }
 changed by  dingjing,以上

    for(i = 0; i < num; ++i){
   
        char labelstr[4096] = {
   0};
        int class = -1;
        for(j = 0; j < classes; ++j){
   
            if (dets[i].prob[j] > thresh){
   
                if (class < 0) {
   
                    strcat(labelstr, names[j]);
                    class = j;
                } else {
   
                    strcat(labelstr, ", ");
                    strcat(labelstr, names[j]);
                }
                printf("%s: %.0f%%\n", names[j], dets[i].prob[j]*100);
                 changed by  dingjing,以下
                fprintf(fp,"%s %.0f%% ", names[j], dets[i].prob[j]*100);
                 changed by  dingjing,以上
	    }
        }//for(j = 0; j < classes; ++j){
   
        if(class >= 0){
   
            int width = im.h * .006;

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

            //printf("%d %s: %.0f%%\n", i, 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 = dets[i].bbox;
            //printf("Box: %f %f %f %f\n", b.x, b.y, b.w, b.h);  
            

            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;

 changed by  dingjing,以下
            int centerx = (b.x*im.w);
            int centery = (b.y*im.h);
            printf("Box: %d %d %d %d %d %d\n", centerx,centery,left,top,right,bot);  
	  	    fprintf(fp,"%d %d %d %d %d %d\n",centerx,centery,left,top,right,bot);//标注框框的位置,中心点坐标,左上点,右下点
 changed by  dingjing,以上

 
            draw_box_width(im, left, top, right, bot, width, red, green, blue);

            if (alphabet) {
   
                image label = get_label(alphabet, labelstr, (im.h*.03));
                draw_label(im, top + width, left, label, rgb);
                free_image(label);
            }
            if (dets[i].mask){
   
                image mask = float_to_image(14, 14, 1, dets[i].mask);
                image resized_mask = resize_image(mask, b.w*im.w, b.h*im.h);
                image tmask = threshold_image(resized_mask, .5);
                embed_image(tmask, im, left, top);
                free_image(mask);
                free_image(resized_mask);
                free_image(tmask);
            }
        }//if(class >= 0){       
    }//for(i = 0; i < num; ++i){
   
fclose(fp);
}//void draw_detections(...)

image.c全文如下:

#include "image.h"
#include "utils.h"
#include "blas.h"
#include "cuda.h"
#include <stdio.h>
#include <math.h>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"


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} };

float get_color(int c, int x, int max)
{
   
    float ratio = ((float)x/max)*5;
    int i = floor(ratio);
    int j = ceil(ratio);
    ratio -= i;
    float r = (1-ratio) * colors[i][c] + ratio*colors[j][c];
    //printf("%f\n", r);
    return r;
}

image mask_to_rgb(image mask)
{
   
    int n = mask.c;
    image im = make_image(mask.w, mask.h, 3);
    int i, j;
    for(j = 0; j < n; ++j){
   
        int offset = j*123457 % n;
        float red = get_color(2,offset,n);
        float green = get_color(1,offset,n);
        float blue = get_color(0,offset,n);
        for(i = 0; i < im.w*im.h; ++i){
   
            im.data[i + 0*im.w*im.h] += mask.data[j*im.h*im.w + i]*red;
            im.data[i + 1*im.w*im.h] += mask.data[j*im.h*im.w + i]*green;
            im.data[i + 2*im.w*im.h] += mask.data[j*im.h*im.w + i]*blue;
        }
    }
    return im;
}

static float get_pixel(image m, int x, int y, int c)
{
   
    assert(x < m.w && y < m.h && c < m.c);
    return m.data[c*m.h*m.w + y*m.w + x];
}
static float get_pixel_extend(image m, int x, int y, int c)
{
   
    if(x < 0 || x >= m.w || y < 0 || y >= m.h) return 0;
    /*
    if(x < 0) x = 0;
    if(x >= m.w) x = m.w-1;
    if(y < 0) y = 0;
    if(y >= m.h) y = m.h-1;
    */
    if(c < 0 || c >= m.c) return 0;
    return get_pixel(m, x, y, c);
}
static void set_pixel(image m, int x, int y, int c, float val)
{
   
    if (x < 0 || y < 0 || c < 0 || x >= m.w || y >= m.h || c >= m.c) return;
    assert(x < m.w && y < m.h && c < m.c);
    m.data[c*m.h*m.w + y*m.w + x] = val;
}
static void add_pixel(image m, int x, int y, int c, float val)
{
   
    assert(x < m.w && y < m.h && c < m.c);
    m.data[c*m.h*m.w + y*m.w + x] += val;
}

static float bilinear_interpolate(image im, float x, float y, int c)
{
   
    int ix = (int) floorf(x);
    int iy = (int) floorf(y);

    float dx = x - ix;
    float dy = y - iy;

    float val = (1-dy) * (1-dx) * get_pixel_extend(im, ix, iy, c) + 
        dy     * (1-dx) * get_pixel_extend(im, ix, iy+1, c) + 
        (1-dy) *   dx   * get_pixel_extend(im, ix+1, iy, c) +
        dy     *   dx   * get_pixel_extend(im, ix+1, iy+1, c);
    return val;
}


void composite_image(image source, image dest, int dx, int dy)
{
   
    int x,y,k;
    for(k = 0; k < source.c; ++k){
   
        for(y = 0; y < source.h; ++y){
   
            for(x = 0; x < source.w; ++x){
   
                float val = get_pixel(source, x, y, k);
                float val2 = get_pixel_extend(dest, dx+x, dy+y, k);
                set_pixel(dest, dx+x, dy+y, k, val * val2);
            }
        }
    }
}

image border_image(image a, int border)
{
   
    image b = make_image(a.w + 2*border, a.h + 2*border, a.c);
    int x,y,k;
    for(k = 0; k < b.c; ++k){
   
        for(y = 0; y < b.h; ++y){
   
            for(x = 0; x < b.w; ++x){
   
                float val = get_pixel_extend(a, x - border, y - border, k);
                if(x - border < 0 || x - border >= a.w || y - border < 0 || y - border >= a.h) val = 1;
                set_pixel(b, x, y, k, val);
            }
        }
    }
    return b;
}

image tile_images(image a, image b, int dx)
{
   
    if(a.w == 0) return copy_image(b);
    image c = make_image(a.w + b.w + dx, (a.h > b.h) ? a.h : b.h, (a.c > b.c) ? a.c : b.c);
    fill_cpu(c.w*c.h*c.c, 1, c.data, 1);
    embed_image(a, c, 0, 0); 
    composite_image(b, c, a.w + dx, 0);
    return c;
}

image get_label(image **characters, char *string, int size)
{
   
    size = size/10;
    if(size > 7) size = 7;
    image label = make_empty_image(0,0,0);
    while(*string){
   
        image l = characters[size][(int)*string];
        image n = tile_images(label, l, -size - 1 + (size+1)/2);
        free_image(label);
        label = n;
        ++string;
    }
    image b = border_image(label, label.h*.25);
    free_image(label);
    return b;
}

void draw_label(image a, int r, int c, image label, const float *rgb)
{
   
    int w = label.w;
    int h = label.h;
    if (r - h >= 0) r = r - h;

    int i, j, k;
    for(j = 0; j < h && j + r < a.h; ++j){
   
        for(i = 0; i < w && i + c < a.w; ++i){
   
            for(k = 0; k < label.c; ++k){
   
                float val = get_pixel(label, i, j, k);
                set_pixel(a, i+c, j+r, k, rgb[k] * val);
            }
        }
    }
}

void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b)
{
   
    //normalize_image(a);
    int i;
    if(x1 < 0) x1 = 0;
    if(x1 >= a.w) x1 = a.w-1;
    if(x2 < 0) x2 = 0;
    if(x2 >= a.w) x2 = a.w-1;

    if(y1 < 0) y1 = 0;
    if(y1 >= a.h) y1 = a.h-1;
    if(y2 < 0) y2 = 0;
    if(y2 >= a.h) y2 = a.h-1;

    for(i = x1; i <= x2; ++i){
   
        a.data[i + y1*a.w + 0*a.w*a.h] = r;
        a.data[i + y2*a.w + 0*a.w*a.h] = r;

        a.data[i + y1*a.w + 1*a.w*a.h] = g;
        a.data[i + y2*a.w + 1*a.w*a.h] = g;

        a.data[i + y1*a.w + 2*a.w*a.h] = b;
        a.data[i + y2*a.w + 2*a.w*a.h] = b;
    }
    for(i = y1; i <= y2; ++i){
   
        a.data[x1 + i*a.w + 0*a.w*a.h] = r;
        a.data[x2 + i*a.w + 0*a.w*a.h] = r;

        a.data[x1 + i*a.w + 1*a.w*a.h] = g;
        a.data[x2 + i*a.w + 1*a.w*a.h] = g;

        a.data[x1 + i*a.w + 2*a.w*a.h] = b;
        a.data[x2 + i*a.w + 2*a.w*a.h] = b;
    }
}

void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b)
{
   
    int i;
    for(i = 0; i < w; ++i){
   
        draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
    }
}

void draw_bbox(image a, box bbox, int w, float r, float g, float b)
{
   
    int left  = (bbox.x-bbox.w/2)*a.w;
    int right = (bbox.x+bbox.w/2)*a.w;
    int top   = (bbox.y-bbox.h/2)*a.h;
    int bot   = (bbox.y+bbox.h/2)*a.h;

    int i;
    for(i = 0; i < w; ++i){
   
        draw_box(a, left+i, top+i, right-i, bot-i, r, g, b);
    }
}

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));
        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);
        }
    }
    return alphabets;
}

void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
   
    int i,j;
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将YOLOv7检测结果保存为JSON文件,你需要按照以下步骤进行操作: 1. 首先,完成YOLOv7检测并获取结果。这可能涉及使用YOLOv7模型加载权重,对图像进行预处理,并使用模型进行推理。确保你已经获得了检测边界框的坐标和类别标签。 2. 创建一个Python字典或列表,用于存储检测结果。你可以使用字典来存储每个检测边界框的相关信息(例如,边界框坐标和类别标签),或者使用列表来存储每个检测结果的顺序信息。 3. 将检测结果填充到字典或列表中。对于每个检测边界框,将其相关信息添加为字典的键值对,或将其作为列表的一个元素。 4. 使用Python的JSON模块将字典或列表转换为JSON格式的字符串。导入json库,然后使用json.dumps()函数将字典或列表转换为JSON字符串。 5. 将JSON字符串写入文件。打开一个新文件,使用Python的文件操作功能将JSON字符串写入文件中。确保指定文件名以".json"结尾。 下面是一个示例代码,演示了如何将YOLOv7检测结果保存为JSON文件: ```python import json # 模拟YOLOv7检测结果 detections = [ {"class": "person", "bbox": [10, 20, 100, 200]}, {"class": "car", "bbox": [50, 60, 150, 250]} ] # 创建一个字典,用于存储检测结果 results = {"detections": detections} # 将检测结果转换为JSON格式的字符串 json_str = json.dumps(results) # 将JSON字符串写入文件 with open("detections.json", "w") as json_file: json_file.write(json_str) ``` 运行上述代码后,将生成一个名为"detections.json"的文件,其中包含YOLOv7检测结果的JSON表示。请根据你的实际情况修改检测结果和文件名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值