yolo2 检测到的物体输出2

2024-08-26 03:32
文章标签 输出 检测 物体 yolo2

本文主要是介绍yolo2 检测到的物体输出2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明

检测到的物体被box所标记,输出box所在的位置,并存储到txt中。
上次是在src/image.c的代码中修改的,相当于是在画出检测出的物体的框的同时把坐标存入了txt,这次修改的是examples/detector.c文件。

先来看一下主函数 examples/darknet.c

官网上运行的示例测试代码为:

这里写图片描述

其中运行的主函数的源码就是examples/darknet.c,后面的detector test等参数通过命令行传给了主函数。
darknet.c的代码为(代码比较长,省略了其他只看main):
#include "darknet.h"#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{//test_resize("data/bad.jpg");//test_box();//test_convolutional_layer();if(argc < 2){fprintf(stderr, "usage: %s <function>\n", argv[0]);return 0;}gpu_index = find_int_arg(argc, argv, "-i", 0);if(find_arg(argc, argv, "-nogpu")) {gpu_index = -1;}#ifndef GPUgpu_index = -1;
#elseif(gpu_index >= 0){cuda_set_device(gpu_index);}
#endifif (0 == strcmp(argv[1], "average")){average(argc, argv);} else if (0 == strcmp(argv[1], "yolo")){run_yolo(argc, argv);} else if (0 == strcmp(argv[1], "voxel")){run_voxel(argc, argv);} else if (0 == strcmp(argv[1], "super")){run_super(argc, argv);} else if (0 == strcmp(argv[1], "lsd")){run_lsd(argc, argv);} else if (0 == strcmp(argv[1], "detector")){ //*********************************run_detector(argc, argv);} else if (0 == strcmp(argv[1], "detect")){float thresh = find_float_arg(argc, argv, "-thresh", .24);char *filename = (argc > 4) ? argv[4]: 0;char *outfile = find_char_arg(argc, argv, "-out", 0);int fullscreen = find_arg(argc, argv, "-fullscreen");test_detector("cfg/coco.data", argv[2], argv[3], filename, thresh, .5, outfile, fullscreen);} else if (0 == strcmp(argv[1], "cifar")){run_cifar(argc, argv);} else if (0 == strcmp(argv[1], "go")){run_go(argc, argv);} else if (0 == strcmp(argv[1], "rnn")){run_char_rnn(argc, argv);} else if (0 == strcmp(argv[1], "vid")){run_vid_rnn(argc, argv);} else if (0 == strcmp(argv[1], "coco")){run_coco(argc, argv);} else if (0 == strcmp(argv[1], "classify")){predict_classifier("cfg/imagenet1k.data", argv[2], argv[3], argv[4], 5);} else if (0 == strcmp(argv[1], "classifier")){run_classifier(argc, argv);} else if (0 == strcmp(argv[1], "regressor")){run_regressor(argc, argv);} else if (0 == strcmp(argv[1], "segmenter")){run_segmenter(argc, argv);} else if (0 == strcmp(argv[1], "art")){run_art(argc, argv);} else if (0 == strcmp(argv[1], "tag")){run_tag(argc, argv);} else if (0 == strcmp(argv[1], "compare")){run_compare(argc, argv);} else if (0 == strcmp(argv[1], "dice")){run_dice(argc, argv);} else if (0 == strcmp(argv[1], "writing")){run_writing(argc, argv);} else if (0 == strcmp(argv[1], "3d")){composite_3d(argv[2], argv[3], argv[4], (argc > 5) ? atof(argv[5]) : 0);} else if (0 == strcmp(argv[1], "test")){test_resize(argv[2]);} else if (0 == strcmp(argv[1], "captcha")){run_captcha(argc, argv);} else if (0 == strcmp(argv[1], "nightmare")){run_nightmare(argc, argv);} else if (0 == strcmp(argv[1], "rgbgr")){rgbgr_net(argv[2], argv[3], argv[4]);} else if (0 == strcmp(argv[1], "reset")){reset_normalize_net(argv[2], argv[3], argv[4]);} else if (0 == strcmp(argv[1], "denormalize")){denormalize_net(argv[2], argv[3], argv[4]);} else if (0 == strcmp(argv[1], "statistics")){statistics_net(argv[2], argv[3]);} else if (0 == strcmp(argv[1], "normalize")){normalize_net(argv[2], argv[3], argv[4]);} else if (0 == strcmp(argv[1], "rescale")){rescale_net(argv[2], argv[3], argv[4]);} else if (0 == strcmp(argv[1], "ops")){operations(argv[2]);} else if (0 == strcmp(argv[1], "speed")){speed(argv[2], (argc > 3 && argv[3]) ? atoi(argv[3]) : 0);} else if (0 == strcmp(argv[1], "oneoff")){oneoff(argv[2], argv[3], argv[4]);} else if (0 == strcmp(argv[1], "oneoff2")){oneoff2(argv[2], argv[3], argv[4], atoi(argv[5]));} else if (0 == strcmp(argv[1], "partial")){partial(argv[2], argv[3], argv[4], atoi(argv[5]));} else if (0 == strcmp(argv[1], "average")){average(argc, argv);} else if (0 == strcmp(argv[1], "visualize")){visualize(argv[2], (argc > 3) ? argv[3] : 0);} else if (0 == strcmp(argv[1], "mkimg")){mkimg(argv[2], argv[3], atoi(argv[4]), atoi(argv[5]), atoi(argv[6]), argv[7]);} else if (0 == strcmp(argv[1], "imtest")){test_resize(argv[2]);} else {fprintf(stderr, "Not an option: %s\n", argv[1]);}return 0;
}
可以发现调用了run_detector(argc, argv);这个函数,这个函数位于 examples/detector.c中。查看detector.c,发现调用了test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)。
于是考虑在这个函数中存储输出文件。

编写了一个sava_txt函数。

#include "darknet.h"static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
{list *options = read_data_cfg(datacfg);char *train_images = option_find_str(options, "train", "/home/maqy/darknet/darknet-master/scripts/train.txt");char *backup_directory = option_find_str(options, "backup", "/backup/");srand(time(0));char *base = basecfg(cfgfile);printf("%s\n", base);float avg_loss = -1;network *nets = calloc(ngpus, sizeof(network));srand(time(0));int seed = rand();int i;for(i = 0; i < ngpus; ++i){srand(seed);
#ifdef GPUcuda_set_device(gpus[i]);
#endifnets[i] = load_network(cfgfile, weightfile, clear);nets[i].learning_rate *= ngpus;}srand(time(0));network net = nets[0];int imgs = net.batch * net.subdivisions * ngpus;printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);data train, buffer;layer l = net.layers[net.n - 1];int classes = l.classes;float jitter = l.jitter;list *plist = get_paths(train_images);//int N = plist->size;char **paths = (char **)list_to_array(plist);load_args args = get_base_args(net);args.coords = l.coords;args.paths = paths;args.n = imgs;args.m = plist->size;args.classes = classes;args.jitter = jitter;args.num_boxes = l.max_boxes;args.d = &buffer;args.type = DETECTION_DATA;//args.type = INSTANCE_DATA;args.threads = 8;pthread_t load_thread = load_data(args);clock_t time;int count = 0;//while(i*imgs < N*120){while(get_current_batch(net) < net.max_batches){if(l.random && count++%10 == 0){printf("Resizing\n");int dim = (rand() % 10 + 10) * 32;if (get_current_batch(net)+200 > net.max_batches) dim = 608;//int dim = (rand() % 4 + 16) * 32;printf("%d\n", dim);args.w = dim;args.h = dim;pthread_join(load_thread, 0);train = buffer;free_data(train);load_thread = load_data(args);for(i = 0; i < ngpus; ++i){resize_network(nets + i, dim, dim);}net = nets[0];}time=clock();pthread_join(load_thread, 0);train = buffer;load_thread = load_data(args);/*int k;for(k = 0; k < l.max_boxes; ++k){box b = float_to_box(train.y.vals[10] + 1 + k*5);if(!b.x) break;printf("loaded: %f %f %f %f\n", b.x, b.y, b.w, b.h);}*//*int zz;for(zz = 0; zz < train.X.cols; ++zz){image im = float_to_image(net.w, net.h, 3, train.X.vals[zz]);int k;for(k = 0; k < l.max_boxes; ++k){box b = float_to_box(train.y.vals[zz] + k*5, 1);printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);draw_bbox(im, b, 1, 1,0,0);}show_image(im, "truth11");cvWaitKey(0);save_image(im, "truth11");}*/printf("Loaded: %lf seconds\n", sec(clock()-time));time=clock();float loss = 0;
#ifdef GPUif(ngpus == 1){loss = train_network(net, train);} else {loss = train_networks(nets, ngpus, train, 4);}
#elseloss = train_network(net, train);
#endifif (avg_loss < 0) avg_loss = loss;avg_loss = avg_loss*.9 + loss*.1;i = get_current_batch(net);printf("%ld: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs);if(i%100==0){
#ifdef GPUif(ngpus != 1) sync_nets(nets, ngpus, 0);
#endifchar buff[256];sprintf(buff, "%s/%s.backup", backup_directory, base);save_weights(net, buff);}if(i%10000==0 || (i < 1000 && i%100 == 0)){
#ifdef GPUif(ngpus != 1) sync_nets(nets, ngpus, 0);
#endifchar buff[256];sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);save_weights(net, buff);}free_data(train);}
#ifdef GPUif(ngpus != 1) sync_nets(nets, ngpus, 0);
#endifchar buff[256];sprintf(buff, "%s/%s_final.weights", backup_directory, base);save_weights(net, buff);
}static int get_coco_image_id(char *filename)
{char *p = strrchr(filename, '_');return atoi(p+1);
}static void print_cocos(FILE *fp, char *image_path, box *boxes, float **probs, int num_boxes, int classes, int w, int h)
{int i, j;int image_id = get_coco_image_id(image_path);for(i = 0; i < num_boxes; ++i){float xmin = boxes[i].x - boxes[i].w/2.;float xmax = boxes[i].x + boxes[i].w/2.;float ymin = boxes[i].y - boxes[i].h/2.;float ymax = boxes[i].y + boxes[i].h/2.;if (xmin < 0) xmin = 0;if (ymin < 0) ymin = 0;if (xmax > w) xmax = w;if (ymax > h) ymax = h;float bx = xmin;float by = ymin;float bw = xmax - xmin;float bh = ymax - ymin;for(j = 0; j < classes; ++j){if (probs[i][j]) fprintf(fp, "{\"image_id\":%d, \"category_id\":%d, \"bbox\":[%f, %f, %f, %f], \"score\":%f},\n", image_id, coco_ids[j], bx, by, bw, bh, probs[i][j]);}}
}void print_detector_detections(FILE **fps, char *id, box *boxes, float **probs, int total, int classes, int w, int h)
{int i, j;for(i = 0; i < total; ++i){float xmin = boxes[i].x - boxes[i].w/2. + 1;float xmax = boxes[i].x + boxes[i].w/2. + 1;float ymin = boxes[i].y - boxes[i].h/2. + 1;float ymax = boxes[i].y + boxes[i].h/2. + 1;if (xmin < 1) xmin = 1;if (ymin < 1) ymin = 1;if (xmax > w) xmax = w;if (ymax > h) ymax = h;for(j = 0; j < classes; ++j){if (probs[i][j]) fprintf(fps[j], "%s %f %f %f %f %f\n", id, probs[i][j],xmin, ymin, xmax, ymax);}}
}void print_imagenet_detections(FILE *fp, int id, box *boxes, float **probs, int total, int classes, int w, int h)
{int i, j;for(i = 0; i < total; ++i){float xmin = boxes[i].x - boxes[i].w/2.;float xmax = boxes[i].x + boxes[i].w/2.;float ymin = boxes[i].y - boxes[i].h/2.;float ymax = boxes[i].y + boxes[i].h/2.;if (xmin < 0) xmin = 0;if (ymin < 0) ymin = 0;if (xmax > w) xmax = w;if (ymax > h) ymax = h;for(j = 0; j < classes; ++j){int class = j;if (probs[i][class]) fprintf(fp, "%d %d %f %f %f %f %f\n", id, j+1, probs[i][class],xmin, ymin, xmax, ymax);}}
}void validate_detector_flip(char *datacfg, char *cfgfile, char *weightfile, char *outfile)
{int j;list *options = read_data_cfg(datacfg);char *valid_images = option_find_str(options, "valid", "/home/maqy/darknet/darknet-master/scripts/2007_test.txt");char *name_list = option_find_str(options, "names", "data/names.list");char *prefix = option_find_str(options, "results", "results");char **names = get_labels(name_list);char *mapf = option_find_str(options, "map", 0);int *map = 0;if (mapf) map = read_map(mapf);network net = parse_network_cfg(cfgfile);if(weightfile){load_weights(&net, weightfile);}set_batch_network(&net, 2);fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);srand(time(0));list *plist = get_paths(valid_images);char **paths = (char **)list_to_array(plist);layer l = net.layers[net.n-1];int classes = l.classes;char buff[1024];char *type = option_find_str(options, "eval", "voc");FILE *fp = 0;FILE **fps = 0;int coco = 0;int imagenet = 0;if(0==strcmp(type, "coco")){if(!outfile) outfile = "coco_results";snprintf(buff, 1024, "%s/%s.json", prefix, outfile);fp = fopen(buff, "w");fprintf(fp, "[\n");coco = 1;} else if(0==strcmp(type, "imagenet")){if(!outfile) outfile = "imagenet-detection";snprintf(buff, 1024, "%s/%s.txt", prefix, outfile);fp = fopen(buff, "w");imagenet = 1;classes = 200;} else {if(!outfile) outfile = "comp4_det_test_";fps = calloc(classes, sizeof(FILE *));for(j = 0; j < classes; ++j){snprintf(buff, 1024, "%s/%s%s.txt", prefix, outfile, names[j]);fps[j] = fopen(buff, "w");}}box *boxes = calloc(l.w*l.h*l.n, sizeof(box));float **probs = calloc(l.w*l.h*l.n, sizeof(float *));for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(classes+1, sizeof(float *));int m = plist->size;int i=0;int t;float thresh = .005;float nms = .45;int nthreads = 4;image *val = calloc(nthreads, sizeof(image));image *val_resized = calloc(nthreads, sizeof(image));image *buf = calloc(nthreads, sizeof(image));image *buf_resized = calloc(nthreads, sizeof(image));pthread_t *thr = calloc(nthreads, sizeof(pthread_t));image input = make_image(net.w, net.h, net.c*2);load_args args = {0};args.w = net.w;args.h = net.h;//args.type = IMAGE_DATA;args.type = LETTERBOX_DATA;for(t = 0; t < nthreads; ++t){args.path = paths[i+t];args.im = &buf[t];args.resized = &buf_resized[t];thr[t] = load_data_in_thread(args);}time_t start = time(0);for(i = nthreads; i < m+nthreads; i += nthreads){fprintf(stderr, "%d\n", i);for(t = 0; t < nthreads && i+t-nthreads < m; ++t){pthread_join(thr[t], 0);val[t] = buf[t];val_resized[t] = buf_resized[t];}for(t = 0; t < nthreads && i+t < m; ++t){args.path = paths[i+t];args.im = &buf[t];args.resized = &buf_resized[t];thr[t] = load_data_in_thread(args);}for(t = 0; t < nthreads && i+t-nthreads < m; ++t){char *path = paths[i+t-nthreads];char *id = basecfg(path);copy_cpu(net.w*net.h*net.c, val_resized[t].data, 1, input.data, 1);flip_image(val_resized[t]);copy_cpu(net.w*net.h*net.c, val_resized[t].data, 1, input.data + net.w*net.h*net.c, 1);network_predict(net, input.data);int w = val[t].w;int h = val[t].h;get_region_boxes(l, w, h, net.w, net.h, thresh, probs, boxes, 0, 0, map, .5, 0);if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, classes, nms);if (coco){print_cocos(fp, path, boxes, probs, l.w*l.h*l.n, classes, w, h);} else if (imagenet){print_imagenet_detections(fp, i+t-nthreads+1, boxes, probs, l.w*l.h*l.n, classes, w, h);} else {print_detector_detections(fps, id, boxes, probs, l.w*l.h*l.n, classes, w, h);}free(id);free_image(val[t]);free_image(val_resized[t]);}}for(j = 0; j < classes; ++j){if(fps) fclose(fps[j]);}if(coco){fseek(fp, -2, SEEK_CUR); fprintf(fp, "\n]\n");fclose(fp);}fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
}void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *outfile)
{int j;list *options = read_data_cfg(datacfg);char *valid_images = option_find_str(options, "valid", "/home/maqy/darknet/darknet-master/scripts/2007_test.txt");//这里应该是train还是test??char *name_list = option_find_str(options, "names", "data/names.list");char *prefix = option_find_str(options, "results", "results");char **names = get_labels(name_list);char *mapf = option_find_str(options, "map", 0);int *map = 0;if (mapf) map = read_map(mapf);network net = parse_network_cfg(cfgfile);if(weightfile){load_weights(&net, weightfile);}set_batch_network(&net, 1);fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);srand(time(0));list *plist = get_paths(valid_images);char **paths = (char **)list_to_array(plist);layer l = net.layers[net.n-1];int classes = l.classes;char buff[1024];char *type = option_find_str(options, "eval", "voc");FILE *fp = 0;FILE **fps = 0;int coco = 0;int imagenet = 0;if(0==strcmp(type, "coco")){if(!outfile) outfile = "coco_results";snprintf(buff, 1024, "%s/%s.json", prefix, outfile);fp = fopen(buff, "w");fprintf(fp, "[\n");coco = 1;} else if(0==strcmp(type, "imagenet")){if(!outfile) outfile = "imagenet-detection";snprintf(buff, 1024, "%s/%s.txt", prefix, outfile);fp = fopen(buff, "w");imagenet = 1;classes = 200;} else {if(!outfile) outfile = "comp4_det_test_";fps = calloc(classes, sizeof(FILE *));for(j = 0; j < classes; ++j){snprintf(buff, 1024, "%s/%s%s.txt", prefix, outfile, names[j]);fps[j] = fopen(buff, "w");}}box *boxes = calloc(l.w*l.h*l.n, sizeof(box));float **probs = calloc(l.w*l.h*l.n, sizeof(float *));for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(classes+1, sizeof(float *));int m = plist->size;int i=0;int t;float thresh = .005;float nms = .45;int nthreads = 4;image *val = calloc(nthreads, sizeof(image));image *val_resized = calloc(nthreads, sizeof(image));image *buf = calloc(nthreads, sizeof(image));image *buf_resized = calloc(nthreads, sizeof(image));pthread_t *thr = calloc(nthreads, sizeof(pthread_t));load_args args = {0};args.w = net.w;args.h = net.h;//args.type = IMAGE_DATA;args.type = LETTERBOX_DATA;for(t = 0; t < nthreads; ++t){args.path = paths[i+t];args.im = &buf[t];args.resized = &buf_resized[t];thr[t] = load_data_in_thread(args);}time_t start = time(0);for(i = nthreads; i < m+nthreads; i += nthreads){fprintf(stderr, "%d\n", i);for(t = 0; t < nthreads && i+t-nthreads < m; ++t){pthread_join(thr[t], 0);val[t] = buf[t];val_resized[t] = buf_resized[t];}for(t = 0; t < nthreads && i+t < m; ++t){args.path = paths[i+t];args.im = &buf[t];args.resized = &buf_resized[t];thr[t] = load_data_in_thread(args);}for(t = 0; t < nthreads && i+t-nthreads < m; ++t){char *path = paths[i+t-nthreads];char *id = basecfg(path);float *X = val_resized[t].data;network_predict(net, X);int w = val[t].w;int h = val[t].h;get_region_boxes(l, w, h, net.w, net.h, thresh, probs, boxes, 0, 0, map, .5, 0);if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, classes, nms);if (coco){print_cocos(fp, path, boxes, probs, l.w*l.h*l.n, classes, w, h);} else if (imagenet){print_imagenet_detections(fp, i+t-nthreads+1, boxes, probs, l.w*l.h*l.n, classes, w, h);} else {print_detector_detections(fps, id, boxes, probs, l.w*l.h*l.n, classes, w, h);}free(id);free_image(val[t]);free_image(val_resized[t]);}}for(j = 0; j < classes; ++j){if(fps) fclose(fps[j]);}if(coco){fseek(fp, -2, SEEK_CUR); fprintf(fp, "\n]\n");fclose(fp);}fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
}void validate_detector_recall(char *cfgfile, char *weightfile)
{network net = parse_network_cfg(cfgfile);if(weightfile){load_weights(&net, weightfile);}set_batch_network(&net, 1);fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);srand(time(0));list *plist = get_paths("/home/maqy/darknet/darknet-master/scripts/2007_test.txt"); //测试???数据char **paths = (char **)list_to_array(plist);layer l = net.layers[net.n-1];int classes = l.classes;int j, k;box *boxes = calloc(l.w*l.h*l.n, sizeof(box));float **probs = calloc(l.w*l.h*l.n, sizeof(float *));for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(classes+1, sizeof(float *));int m = plist->size;int i=0;float thresh = .001;float iou_thresh = .5;float nms = .4;int total = 0;int correct = 0;int proposals = 0;float avg_iou = 0;for(i = 0; i < m; ++i){char *path = paths[i];image orig = load_image_color(path, 0, 0);image sized = resize_image(orig, net.w, net.h);char *id = basecfg(path);network_predict(net, sized.data);get_region_boxes(l, sized.w, sized.h, net.w, net.h, thresh, probs, boxes, 0, 1, 0, .5, 1);if (nms) do_nms(boxes, probs, l.w*l.h*l.n, 1, nms);char labelpath[4096];find_replace(path, "images", "labels", labelpath);find_replace(labelpath, "JPEGImages", "labels", labelpath);find_replace(labelpath, ".jpg", ".txt", labelpath);find_replace(labelpath, ".JPEG", ".txt", labelpath);int num_labels = 0;box_label *truth = read_boxes(labelpath, &num_labels);for(k = 0; k < l.w*l.h*l.n; ++k){if(probs[k][0] > thresh){++proposals;}}for (j = 0; j < num_labels; ++j) {++total;box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h};float best_iou = 0;for(k = 0; k < l.w*l.h*l.n; ++k){float iou = box_iou(boxes[k], t);if(probs[k][0] > thresh && iou > best_iou){best_iou = iou;}}avg_iou += best_iou;if(best_iou > iou_thresh){++correct;}}fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total);free(id);free_image(orig);free_image(sized);}
}
//保存船的位置和数量
void save_txt(char *filename,box *boxes,int num,float **probs,float thresh,image im,int classes)
{char a[100];    //存储每行的字符串char name[100]; //存储txt的路径strcpy(name,filename);int k=0;while(name[k]!='\0'){k++;}k=k-3;name[k]='t';k++;name[k]='x';k++;name[k]='t';k++;printf("txt_name:%s\n",name);int shipnum=0;   //船的数量FILE *fp;printf("save_txt filename:%s \n",filename);if((fp=fopen(name,"w"))==NULL){printf("%s can't open\n",name);exit(1);}int left;//距离图片左边界的值  right-left为box宽度int right;int top;//距离图片上边界的值  bot-top为box高度int bot;int i;for(i=0;i<num;i++){int class = max_index(probs[i], classes);     float prob = probs[i][class];if(prob > thresh){left  = (boxes[i].x-boxes[i].w/2.)*im.w;//距离图片左边界的值  right-left为box宽度right = (boxes[i].x+boxes[i].w/2.)*im.w;top   = (boxes[i].y-boxes[i].h/2.)*im.h;//距离图片上边界的值  bot-top为box高度bot   = (boxes[i].y+boxes[i].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;++shipnum;printf("boxes[%d]: left:%d  right:%d  top:%d  bot:%d prob:%f \n",i,left,right,top,bot,prob);  //只有1类的话概率存在probs[i][1]中sprintf(a,"%d %d %d %d\n",left,right,top,bot);//将每个box的数据打印到 a中fputs(a,fp);     //写入文件}}sprintf(a,"%d\n",shipnum);printf("save shipnum:%s \n",a);fputs(a,fp);fclose(fp);      //关闭文件
}void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
{list *options = read_data_cfg(datacfg);char *name_list = option_find_str(options, "names", "data/names.list");char **names = get_labels(name_list);image **alphabet = load_alphabet();network net = parse_network_cfg(cfgfile);if(weightfile){load_weights(&net, weightfile);}set_batch_network(&net, 1);srand(2222222);double time;char buff[256];char *input = buff;int j;float nms=.3;while(1){if(filename){strncpy(input, filename, 256);} else {printf("Enter Image Path: ");fflush(stdout);input = fgets(input, 256, stdin);if(!input) return;strtok(input, "\n");}image im = load_image_color(input,0,0);image sized = letterbox_image(im, net.w, net.h);//image sized = resize_image(im, net.w, net.h);//image sized2 = resize_max(im, net.w);//image sized = crop_image(sized2, -((net.w - sized2.w)/2), -((net.h - sized2.h)/2), net.w, net.h);//resize_network(&net, sized.w, sized.h);layer l = net.layers[net.n-1];box *boxes = calloc(l.w*l.h*l.n, sizeof(box));float **probs = calloc(l.w*l.h*l.n, sizeof(float *));for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(l.classes + 1, sizeof(float *));float **masks = 0;if (l.coords > 4){masks = calloc(l.w*l.h*l.n, sizeof(float*));for(j = 0; j < l.w*l.h*l.n; ++j) masks[j] = calloc(l.coords-4, sizeof(float *));}float *X = sized.data;time=what_time_is_it_now();network_predict(net, X);printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);get_region_boxes(l, im.w, im.h, net.w, net.h, thresh, probs, boxes, masks, 0, 0, hier_thresh, 1);   //这里得到了box未转化前的区域以及probs//maqy添加,打印出boxes//打印出l.w,l.h,l.nprintf("l.w:%d   l.h:%d   l.n:%d   l.classes:%d  \n",l.w,l.h,l.n,l.classes);printf("maqy filename:%s\n",filename);//for(kkk=0;kkk<l.w*l.h*l.n;kkk++)//{//    printf("boxes[%d]: x:%f  y:%f  w:%f  h:%f prob:%f ---%f\n",kkk,boxes[kkk].x,boxes[kkk].y,boxes[kkk].w,boxes[kkk].h,probs[kkk][0],probs[kkk][1]);//}
//  int left;//距离图片左边界的值  right-left为box宽度
//       int right;
//        int top;//距离图片上边界的值  bot-top为box高度
//        int bot;
//        int kkk;
//  for(kkk=0;kkk<l.w*l.h*l.n;kkk++)
//  {
//      left  = (boxes[kkk].x-boxes[kkk].w/2.)*im.w;//距离图片左边界的值  right-left为box宽度
//          right = (boxes[kkk].x+boxes[kkk].w/2.)*im.w;
//          top   = (boxes[kkk].y-boxes[kkk].h/2.)*im.h;//距离图片上边界的值  bot-top为box高度
//          bot   = (boxes[kkk].y+boxes[kkk].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;
//      if(probs[kkk][1]>thresh)
//      {
//          printf("boxes[%d]: left:%d  right:%d  top:%d  bot:%d prob:%f \n",kkk,left,right,top,bot,probs[kkk][1]);  //只有1类的话概率存在probs[kkk][1]中
//      }
//  }if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);//else if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms);save_txt(filename, boxes, l.w*l.h*l.n, probs, thresh,im,l.classes);//必须放在上个if后面,不然会有重复的框出现draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, masks, names, alphabet, l.classes);//maqyif(outfile){save_image(im, outfile);}else{save_image(im, "predictions");
#ifdef OPENCVcvNamedWindow("predictions", CV_WINDOW_NORMAL); if(fullscreen){cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);}show_image(im, "predictions");cvWaitKey(0);cvDestroyAllWindows();
#endif}free_image(im);free_image(sized);free(boxes);free_ptrs((void **)probs, l.w*l.h*l.n);if (filename) break;}
}void run_detector(int argc, char **argv)
{char *prefix = find_char_arg(argc, argv, "-prefix", 0);float thresh = find_float_arg(argc, argv, "-thresh", .24);float hier_thresh = find_float_arg(argc, argv, "-hier", .5);int cam_index = find_int_arg(argc, argv, "-c", 0);int frame_skip = find_int_arg(argc, argv, "-s", 0);int avg = find_int_arg(argc, argv, "-avg", 3);if(argc < 4){fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);return;}char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);char *outfile = find_char_arg(argc, argv, "-out", 0);int *gpus = 0;int gpu = 0;int ngpus = 0;if(gpu_list){printf("%s\n", gpu_list);int len = strlen(gpu_list);ngpus = 1;int i;for(i = 0; i < len; ++i){if (gpu_list[i] == ',') ++ngpus;}gpus = calloc(ngpus, sizeof(int));for(i = 0; i < ngpus; ++i){gpus[i] = atoi(gpu_list);gpu_list = strchr(gpu_list, ',')+1;}} else {gpu = gpu_index;gpus = &gpu;ngpus = 1;}int clear = find_arg(argc, argv, "-clear");int fullscreen = find_arg(argc, argv, "-fullscreen");int width = find_int_arg(argc, argv, "-w", 0);int height = find_int_arg(argc, argv, "-h", 0);int fps = find_int_arg(argc, argv, "-fps", 0);char *datacfg = argv[3];char *cfg = argv[4];char *weights = (argc > 5) ? argv[5] : 0;char *filename = (argc > 6) ? argv[6]: 0;if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, outfile, fullscreen);else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear);else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile);else if(0==strcmp(argv[2], "valid2")) validate_detector_flip(datacfg, cfg, weights, outfile);else if(0==strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights);else if(0==strcmp(argv[2], "demo")) {list *options = read_data_cfg(datacfg);int classes = option_find_int(options, "classes", 1);   //classchar *name_list = option_find_str(options, "names", "data/names.list");char **names = get_labels(name_list);demo(cfg, weights, thresh, cam_index, filename, names, classes, frame_skip, prefix, avg, hier_thresh, width, height, fps, fullscreen);}
}

写的有点乱,以后再做修改。
功能就是在待检测图片路径下多了一个同名的txt文件,存储的是框的位置和数量

这篇关于yolo2 检测到的物体输出2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1107429

相关文章

使用C#如何创建人名或其他物体随机分组

《使用C#如何创建人名或其他物体随机分组》文章描述了一个随机分配人员到多个团队的代码示例,包括将人员列表随机化并根据组数分配到不同组,最后按组号排序显示结果... 目录C#创建人名或其他物体随机分组此示例使用以下代码将人员分配到组代码首先将lstPeople ListBox总结C#创建人名或其他物体随机分组

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

烟火目标检测数据集 7800张 烟火检测 带标注 voc yolo

一个包含7800张带标注图像的数据集,专门用于烟火目标检测,是一个非常有价值的资源,尤其对于那些致力于公共安全、事件管理和烟花表演监控等领域的人士而言。下面是对此数据集的一个详细介绍: 数据集名称:烟火目标检测数据集 数据集规模: 图片数量:7800张类别:主要包含烟火类目标,可能还包括其他相关类别,如烟火发射装置、背景等。格式:图像文件通常为JPEG或PNG格式;标注文件可能为X

基于 YOLOv5 的积水检测系统:打造高效智能的智慧城市应用

在城市发展中,积水问题日益严重,特别是在大雨过后,积水往往会影响交通甚至威胁人们的安全。通过现代计算机视觉技术,我们能够智能化地检测和识别积水区域,减少潜在危险。本文将介绍如何使用 YOLOv5 和 PyQt5 搭建一个积水检测系统,结合深度学习和直观的图形界面,为用户提供高效的解决方案。 源码地址: PyQt5+YoloV5 实现积水检测系统 预览: 项目背景

JavaFX应用更新检测功能(在线自动更新方案)

JavaFX开发的桌面应用属于C端,一般来说需要版本检测和自动更新功能,这里记录一下一种版本检测和自动更新的方法。 1. 整体方案 JavaFX.应用版本检测、自动更新主要涉及一下步骤: 读取本地应用版本拉取远程版本并比较两个版本如果需要升级,那么拉取更新历史弹出升级控制窗口用户选择升级时,拉取升级包解压,重启应用用户选择忽略时,本地版本标志为忽略版本用户选择取消时,隐藏升级控制窗口 2.

顺序表之创建,判满,插入,输出

文章目录 🍊自我介绍🍊创建一个空的顺序表,为结构体在堆区分配空间🍊插入数据🍊输出数据🍊判断顺序表是否满了,满了返回值1,否则返回0🍊main函数 你的点赞评论就是对博主最大的鼓励 当然喜欢的小伙伴可以:点赞+关注+评论+收藏(一键四连)哦~ 🍊自我介绍   Hello,大家好,我是小珑也要变强(也是小珑),我是易编程·终身成长社群的一名“创始团队·嘉宾”

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出 在数字化时代,文本到语音(Text-to-Speech, TTS)技术已成为人机交互的关键桥梁,无论是为视障人士提供辅助阅读,还是为智能助手注入声音的灵魂,TTS 技术都扮演着至关重要的角色。从最初的拼接式方法到参数化技术,再到现今的深度学习解决方案,TTS 技术经历了一段长足的进步。这篇文章将带您穿越时

[数据集][目标检测]血细胞检测数据集VOC+YOLO格式2757张4类别

数据集格式:Pascal VOC格式+YOLO格式(不包含分割路径的txt文件,仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数):2757 标注数量(xml文件个数):2757 标注数量(txt文件个数):2757 标注类别数:4 标注类别名称:["Platelets","RBC","WBC","sickle cell"] 每个类别标注的框数:

如何将一个文件里不包含某个字符的行输出到另一个文件?

第一种: grep -v 'string' filename > newfilenamegrep -v 'string' filename >> newfilename 第二种: sed -n '/string/!'p filename > newfilenamesed -n '/string/!'p filename >> newfilename

Detectorn2预训练模型复现:数据准备、训练命令、日志分析与输出目录

Detectorn2预训练模型复现:数据准备、训练命令、日志分析与输出目录 在深度学习项目中,目标检测是一项重要的任务。本文将详细介绍如何使用Detectron2进行目标检测模型的复现训练,涵盖训练数据准备、训练命令、训练日志分析、训练指标以及训练输出目录的各个文件及其作用。特别地,我们将演示在训练过程中出现中断后,如何使用 resume 功能继续训练,并将我们复现的模型与Model Zoo中的