主要修改的image.c文件,在darknet目錄下直接ctrl+f搜即可,然后打開,找到draw_detections_v3函數,加入用來計數的變量。(我的改法其實有點問題,如果置信度位數過多的話左上角第二行會重復。我懶的研究,直接把置信度位數改小,讓第二行蓋過它。)
void draw_detections_v3(.....) ..... qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_probs); int image_nut=0;//記錄螺母數量 (在image.c的370行左右) int image_bolt=0;//記錄螺栓數量 for (i = 0; i < selected_detections_num; ++i) { int width = im.h * .002; if (width < 1) width = 1; ..... char nut[30];//*左上角第一行字符串 (430行左右) char bolt[30]; //*左上角第二行字符串 if (im.c == 1) { draw_box_width_bw(im, left, top, right, bot, width, 0.8); // 1 channel Black-White } else { draw_box_width(im, left, top, right, bot, width, red, green, blue); // 3 channels RGB } .... int j; for (j = 0; j < classes; ++j) { if (selected_detections[i].det.prob[j] > thresh && j != selected_detections[i].best_class) { strcat(labelstr, ", "); strcat(labelstr, names[j]); } } char con[20]={0};//置信度添加 450行左右 sprintf(con, "%.4f", selected_detections[i].det.prob[selected_detections[i].best_class]); strcat(labelstr, ":"); strcat(labelstr, con);// if(!strcmp(names[selected_detections[i].best_class], "nut"))// { image_nut++;// 計數開始 } else if(!strcmp(names[selected_detections[i].best_class], "bolt")) { image_bolt++; } // image label = get_label_v3(alphabet, labelstr, (im.h*.02)); //draw_label(im, top + width, left, label, rgb); draw_weighted_label(im, top + width, left, label, rgb, 0.7); .... if (selected_detections[i].det.mask) { image mask = float_to_image(14, 14, 1, selected_detections[i].det.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 (i==(selected_detections_num-1))//最終結果寫入 484行左右 { sprintf(nut,"nut_num: %d",image_nut); sprintf(bolt,"bolt_num: %d",image_bolt); } image label_nut=get_label_v3(alphabet, nut, (im.h*.03));//last varible is size draw_label(im, 100, 150, label_nut, rgb); //顯示函數 free_image(label_nut); image label_bolt=get_label_v3(alphabet, bolt, (im.h*.03));//last varible is size draw_label(im, 260, 150, label_bolt, rgb); free_image(label_bolt); // } free(selected_detections); }