Skip to content

Commit

Permalink
Merge pull request #1 from chen42/chiyu1
Browse files Browse the repository at this point in the history
Chiyu1
  • Loading branch information
chen42 authored May 10, 2018
2 parents 85388d6 + 0d0865b commit f37d0e0
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 25 deletions.
Empty file added src/.Rhistory
Empty file.
170 changes: 169 additions & 1 deletion src/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,147 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int
}
#endif // OPENCV

void write_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes, char *save_name)
{
int i;
FILE *f_img;
f_img = fopen(save_name, "w");
fprintf(f_img, "%s\n", save_name);
fprintf(f_img,"#object left right top bottom prob\n");
for(i = 0; i < num; ++i){
int class = max_index(probs[i], classes);
float prob = probs[i][class];
int prob_int = prob*100;
//write the class only if its prob is greater than zero
if(prob){
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;
fprintf(f_img, "%s %d %d %d %d %d\n", names[class], left, right, top, bot, prob_int);
}
}
fclose(f_img);
}

void test_detector_file(char *datacfg, char *cfgfile, char *weightfile, char *filelistname, float thresh, char *outfile)
{
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);
clock_t time;
char buff[256];
char *input = buff;
int j;
float nms=.4;


list *plist = get_paths(filelistname);
//int N = plist->size;
char **paths = (char **)list_to_array(plist);

int m = plist->size;
int i=0;


for(i = 0; i < m; ++i){
//char *path = paths[i];
strncpy(input, paths[i], 256);


image im = load_image_color(input,0,0);
image sized = resize_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 *X = sized.data;
time=clock();
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
//get_region_boxes(l, im.w, im.h, net.w, net.h, thresh, probs, boxes, 0, 0, hier_thresh, 1);
//get_region_boxes(l, w, h, thresh, probs, boxes, 0, map);
get_region_boxes(l, 1, 1, thresh, probs, boxes, 0, 0);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort_v2(boxes, probs, l.w*l.h*l.n, l.classes, nms);
draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, names, alphabet, l.classes);
// if output directory is specified then save the images
// Example command
// ./darknet detector test_file cfg/combine9k.data cfg/yolo9000.cfg data/yolo9000.weights imgs_v/imagelist.txt -thresh 0.01 -outdir img_out_cmu
if(outfile){
//TODO mimic draw detection and annotations to txt file
char buff2[256];
char buff3[256];
char * imgName = buff2;
char * saveName = buff3;
//strncpy(saveName, outfile, 256);
strncpy(imgName, input, 256);
int imgName_len = strlen(imgName);
//extract filename
int dir_len = 0;
int ext_len = 0;
int jj = 0;
for(jj = 0; jj < imgName_len; jj++){
if(imgName[jj] == '/'){
dir_len = jj;
}
if(imgName[jj] == '.'){
ext_len = jj;
}
}
imgName[ext_len] = '\0';
imgName += dir_len;
//concatenate and give to save_image
strcat(saveName, imgName);
printf("saving as: %s \n", saveName);
save_image(im, saveName);
//write detections to txt file
strcat(saveName, ".txt");
write_detections(im, l.w*l.h*l.n, thresh, boxes, probs, names, alphabet, l.classes, saveName);
}
else{
save_image(im, "predictions");
#ifdef OPENCV
//cvNamedWindow("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;
printf("\n");
}
}

void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, int dont_show)
{
list *options = read_data_cfg(datacfg);
Expand All @@ -1058,6 +1199,8 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
double time;
char buff[256];
char *input = buff;
char buff3[256];
char * saveName = buff3;
int j;
float nms=.45; // 0.4F
while(1){
Expand Down Expand Up @@ -1093,7 +1236,30 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
int nboxes = 0;
detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes);

char buff2[256];
char * imgName = buff2;
strncpy(imgName, input, 256);
int imgName_len = strlen(imgName);
//extract filename
int dir_len = 0;
int ext_len = 0;
int jj = 0;
for(jj = 0; jj < imgName_len; jj++){
if(imgName[jj] == '/'){
dir_len = jj;
}
if(imgName[jj] == '.'){
ext_len = jj;
}
}
imgName[ext_len] = '\0';
imgName += dir_len;

strncpy(saveName, imgName, 256);
strcat(saveName, ".txt");

draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, saveName);
free_detections(dets, nboxes);
save_image(im, "predictions");
if (!dont_show) {
Expand Down Expand Up @@ -1137,6 +1303,7 @@ void run_detector(int argc, char **argv)
int http_stream_port = find_int_arg(argc, argv, "-http_port", -1);
char *out_filename = find_char_arg(argc, argv, "-out_filename", 0);
char *outfile = find_char_arg(argc, argv, "-out", 0);
char *outdir = find_char_arg(argc, argv, "-outdir", 0);
char *prefix = find_char_arg(argc, argv, "-prefix", 0);
float thresh = find_float_arg(argc, argv, "-thresh", .25); // 0.24
float hier_thresh = find_float_arg(argc, argv, "-hier", .5);
Expand Down Expand Up @@ -1182,6 +1349,7 @@ void run_detector(int argc, char **argv)
if (weights[strlen(weights) - 1] == 0x0d) weights[strlen(weights) - 1] = 0;
char *filename = (argc > 6) ? argv[6]: 0;
if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, dont_show);
else if(0==strcmp(argv[2], "test_file")) test_detector_file(datacfg, cfg, weights, filename, thresh, outdir);//Using this option
else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear, dont_show);
else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile);
else if(0==strcmp(argv[2], "recall")) validate_detector_recall(datacfg, cfg, weights);
Expand Down
80 changes: 57 additions & 23 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#include "utils.h"
#include "blas.h"
#include "cuda.h"

#include <stdio.h>
#include <math.h>

#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
Expand Down Expand Up @@ -230,13 +231,32 @@ image **load_alphabet()
return alphabets;
}

void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, char *save_name)
{
int i, j;
// char video_name[200] = {0};
// strcat(video_name,filename);
// strcat(video_name,".txt");

FILE *pFile;
pFile=fopen(save_name, "a");
fprintf(pFile,"#object left right top bottom prob\n");

for (i = 0; i < num; ++i) {
box b = dets[i].bbox;
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;

char labelstr[4096] = { 0 };
int class_id = -1;

for (j = 0; j < classes; ++j) {
if (dets[i].prob[j] > thresh) {
if (class_id < 0) {
Expand All @@ -247,6 +267,10 @@ void draw_detections_v3(image im, detection *dets, int num, float thresh, char *
strcat(labelstr, ", ");
strcat(labelstr, names[j]);
}

int prob_j = dets[i].prob[j]*100;

fprintf(pFile, "%s %d %d %d %d %d\n", names[j], left, right, top, bot, prob_j);
printf("%s: %.0f%%\n", names[j], dets[i].prob[j] * 100);
}
}
Expand Down Expand Up @@ -274,18 +298,10 @@ void draw_detections_v3(image im, detection *dets, int num, float thresh, char *
rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
box b = dets[i].bbox;

//printf("%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;


//int b_x_center = (left + right) / 2;
//int b_y_center = (top + bot) / 2;
Expand All @@ -310,13 +326,15 @@ void draw_detections_v3(image im, detection *dets, int num, float thresh, char *
}
}
}
fclose(pFile);
}

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_id = max_index(probs[i], classes);
float prob = probs[i][class_id];
if(prob > thresh){
Expand Down Expand Up @@ -378,9 +396,30 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,
void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
int i, j;

// char video_name[200] = {0};
// strcat(video_name,filename);
// strcat(video_name,"cv.txt");

FILE *pFile;
pFile=fopen("yolo_v3_video_txt", "a");
fprintf(pFile,"#object left right top bottom prob\n");

if (!show_img) return;

for (i = 0; i < num; ++i) {

box b = dets[i].bbox;
int left = (b.x - b.w / 2.)*show_img->width;
int right = (b.x + b.w / 2.)*show_img->width;
int top = (b.y - b.h / 2.)*show_img->height;
int bot = (b.y + b.h / 2.)*show_img->height;

if (left < 0) left = 0;
if (right > show_img->width - 1) right = show_img->width - 1;
if (top < 0) top = 0;
if (bot > show_img->height - 1) bot = show_img->height - 1;

char labelstr[4096] = { 0 };
int class_id = -1;
for (j = 0; j < classes; ++j) {
Expand All @@ -393,6 +432,10 @@ void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float t
strcat(labelstr, ", ");
strcat(labelstr, names[j]);
}

int prob_j = dets[i].prob[j]*100;
fprintf(pFile, "%s %d %d %d %d %d\n", names[j], left, right, top, bot, prob_j);

printf("%s: %.0f%%\n", names[j], dets[i].prob[j] * 100);
}
}
Expand All @@ -418,19 +461,9 @@ void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float t
rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
box b = dets[i].bbox;
//printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);

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

if (left < 0) left = 0;
if (right > show_img->width - 1) right = show_img->width - 1;
if (top < 0) top = 0;
if (bot > show_img->height - 1) bot = show_img->height - 1;


//int b_x_center = (left + right) / 2;
//int b_y_center = (top + bot) / 2;
//int b_width = right - left;
Expand Down Expand Up @@ -465,6 +498,7 @@ void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float t
cvPutText(show_img, labelstr, pt_text, &font, black_color);
}
}
fclose(pFile);
}

void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
Expand Down
2 changes: 1 addition & 1 deletion src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void draw_bbox(image a, box bbox, int w, float r, float g, float b);
void draw_label(image a, int r, int c, image label, const float *rgb);
void write_label(image a, int r, int c, image *characters, char *string, float *rgb);
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **labels, int classes);
void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes);
void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, char *save_name);
image image_distance(image a, image b);
void scale_image(image m, float s);
image crop_image(image im, int dx, int dy, int w, int h);
Expand Down

0 comments on commit f37d0e0

Please sign in to comment.