Skip to content

Commit

Permalink
CARLY RAE JEPSEN IS THE BEST POP ARTIST OF ALL TIME DON'T @ ME
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreddie committed Jan 23, 2018
1 parent e3931c7 commit b40bbdc
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 58 deletions.
39 changes: 30 additions & 9 deletions examples/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
list *options = read_data_cfg(datacfg);

char *backup_directory = option_find_str(options, "backup", "/backup/");
int tag = option_find_int_quiet(options, "tag", 0);
char *label_list = option_find_str(options, "labels", "data/labels.list");
char *train_list = option_find_str(options, "train", "data/train.list");
int classes = option_find_int(options, "classes", 2);

char **labels = get_labels(label_list);
char **labels;
if(!tag){
labels = get_labels(label_list);
}
list *plist = get_paths(train_list);
char **paths = (char **)list_to_array(plist);
printf("%d\n", plist->size);
Expand Down Expand Up @@ -76,7 +80,11 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
args.n = imgs;
args.m = N;
args.labels = labels;
args.type = CLASSIFICATION_DATA;
if (tag){
args.type = TAG_DATA;
} else {
args.type = CLASSIFICATION_DATA;
}

data train;
data buffer;
Expand Down Expand Up @@ -385,15 +393,13 @@ void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
}
}
image im = load_image_color(paths[i], 0, 0);
image resized = resize_min(im, net->w);
image crop = crop_image(resized, (resized.w - net->w)/2, (resized.h - net->h)/2, net->w, net->h);
image crop = center_crop_image(im, net->w, net->h);
//show_image(im, "orig");
//show_image(crop, "cropped");
//cvWaitKey(0);
float *pred = network_predict(net, crop.data);
if(net->hierarchy) hierarchy_predictions(pred, net->outputs, net->hierarchy, 1, 1);

if(resized.data != im.data) free_image(resized);
free_image(im);
free_image(crop);
top_k(pred, classes, topk, indexes);
Expand Down Expand Up @@ -955,6 +961,8 @@ void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_inde
void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
{
#ifdef OPENCV
char *base = basecfg(cfgfile);
image **alphabet = load_alphabet();
printf("Classifier Demo\n");
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
Expand Down Expand Up @@ -988,8 +996,8 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
int *indexes = calloc(top, sizeof(int));

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow("Classifier", CV_WINDOW_NORMAL);
cvResizeWindow("Classifier", 512, 512);
cvNamedWindow(base, CV_WINDOW_NORMAL);
cvResizeWindow(base, 512, 512);
float fps = 0;
int i;

Expand All @@ -998,8 +1006,8 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
gettimeofday(&tval_before, NULL);

image in = get_image_from_stream(cap);
image in_s = resize_image(in, net->w, net->h);
show_image(in, "Classifier");
//image in_s = resize_image(in, net->w, net->h);
image in_s = letterbox_image(in, net->w, net->h);

float *predictions = network_predict(net, in_s.data);
if(net->hierarchy) hierarchy_predictions(predictions, net->outputs, net->hierarchy, 1, 1);
Expand All @@ -1009,11 +1017,24 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
printf("\033[1;1H");
printf("\nFPS:%.0f\n",fps);

int lh = in.h*.03;
int toph = 3*lh;

float rgb[3] = {1,1,1};
for(i = 0; i < top; ++i){
printf("%d\n", toph);
int index = indexes[i];
printf("%.1f%%: %s\n", predictions[index]*100, names[index]);

char buff[1024];
sprintf(buff, "%3.1f%%: %s\n", predictions[index]*100, names[index]);
image label = get_label(alphabet, buff, lh);
draw_label(in, toph, lh, label, rgb);
toph += 2*lh;
free_image(label);
}

show_image(in, base);
free_image(in_s);
free_image(in);

Expand Down
174 changes: 174 additions & 0 deletions examples/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,177 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
}
}

void censor_detector(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename, int class, float thresh, int skip)
{
image **alphabet = load_alphabet();
char *base = basecfg(cfgfile);
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
list *options = read_data_cfg(datacfg);

srand(2222222);
CvCapture * cap;

int w = 1280;
int h = 720;

if(filename){
cap = cvCaptureFromFile(filename);
}else{
cap = cvCaptureFromCAM(cam_index);
}

if(w){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
}
if(h){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
}

int top = option_find_int(options, "top", 1);

char *label_list = option_find_str(options, "labels", 0);
char *name_list = option_find_str(options, "names", label_list);
char **names = get_labels(name_list);

int *indexes = calloc(top, sizeof(int));

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow(base, CV_WINDOW_NORMAL);
cvResizeWindow(base, 512, 512);
float fps = 0;
int i;
int count = 0;
float nms = .45;

while(1){
image in = get_image_from_stream(cap);
//image in_s = resize_image(in, net->w, net->h);
image in_s = letterbox_image(in, net->w, net->h);
layer l = net->layers[net->n-1];

int nboxes = num_boxes(net);

float *X = in_s.data;
network_predict(net, X);
detection *dets = get_network_boxes(net, in.w, in.h, thresh, 0, 0, 0);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);

for(i = 0; i < nboxes; ++i){
if(dets[i].prob[class] > thresh){
box b = dets[i].bbox;
int left = b.x-b.w/2.;
int top = b.y-b.h/2.;
censor_image(in, left, top, b.w, b.h);
}
}
show_image(in, base);
cvWaitKey(10);
free_detections(dets, num_boxes(net));


free_image(in_s);
free_image(in);


float curr = 0;
fps = .9*fps + .1*curr;
for(i = 0; i < skip; ++i){
image in = get_image_from_stream(cap);
free_image(in);
}
}
}

void extract_detector(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename, int class, float thresh, int skip)
{
image **alphabet = load_alphabet();
char *base = basecfg(cfgfile);
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
list *options = read_data_cfg(datacfg);

srand(2222222);
CvCapture * cap;

int w = 1280;
int h = 720;

if(filename){
cap = cvCaptureFromFile(filename);
}else{
cap = cvCaptureFromCAM(cam_index);
}

if(w){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
}
if(h){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
}

int top = option_find_int(options, "top", 1);

char *label_list = option_find_str(options, "labels", 0);
char *name_list = option_find_str(options, "names", label_list);
char **names = get_labels(name_list);

int *indexes = calloc(top, sizeof(int));

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow(base, CV_WINDOW_NORMAL);
cvResizeWindow(base, 512, 512);
float fps = 0;
int i;
int count = 0;
float nms = .45;

while(1){
image in = get_image_from_stream(cap);
//image in_s = resize_image(in, net->w, net->h);
image in_s = letterbox_image(in, net->w, net->h);
layer l = net->layers[net->n-1];

int nboxes = num_boxes(net);
show_image(in, base);

float *X = in_s.data;
network_predict(net, X);
detection *dets = get_network_boxes(net, in.w, in.h, thresh, 0, 0, 1);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);

for(i = 0; i < nboxes; ++i){
if(dets[i].prob[class] > thresh){
box b = dets[i].bbox;
int size = b.w*in.w > b.h*in.h ? b.w*in.w : b.h*in.h;
int dx = b.x*in.w-size/2.;
int dy = b.y*in.h-size/2.;
image bim = crop_image(in, dx, dy, size, size);
char buff[2048];
sprintf(buff, "results/extract/%07d", count);
++count;
save_image(bim, buff);
free_image(bim);
}
}
free_detections(dets, num_boxes(net));


free_image(in_s);
free_image(in);


float curr = 0;
fps = .9*fps + .1*curr;
for(i = 0; i < skip; ++i){
image in = get_image_from_stream(cap);
free_image(in);
}
}
}

void network_detect(network *net, image im, float thresh, float hier_thresh, float nms, detection *dets)
{
network_predict_image(net, im);
Expand Down Expand Up @@ -674,12 +845,15 @@ void run_detector(int argc, char **argv)
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);
int class = find_int_arg(argc, argv, "-class", 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], "extract")) extract_detector(datacfg, cfg, weights, cam_index, filename, class, thresh, frame_skip);
else if(0==strcmp(argv[2], "censor")) censor_detector(datacfg, cfg, weights, cam_index, filename, class, thresh, frame_skip);
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);
Expand Down
24 changes: 17 additions & 7 deletions examples/regressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void train_regressor(char *datacfg, char *cfgfile, char *weightfile, int *gpus,

char *backup_directory = option_find_str(options, "backup", "/backup/");
char *train_list = option_find_str(options, "train", "data/train.list");
int classes = option_find_int(options, "classes", 1);

list *plist = get_paths(train_list);
char **paths = (char **)list_to_array(plist);
Expand All @@ -43,9 +44,10 @@ void train_regressor(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
args.w = net->w;
args.h = net->h;
args.threads = 32;
args.classes = classes;

args.min = net->min_crop;
args.max = net->max_crop;
args.min = net->min_ratio*net->w;
args.max = net->max_ratio*net->w;
args.angle = net->angle;
args.aspect = net->aspect;
args.exposure = net->exposure;
Expand Down Expand Up @@ -160,6 +162,10 @@ void demo_regressor(char *datacfg, char *cfgfile, char *weightfile, int cam_inde
}else{
cap = cvCaptureFromCAM(cam_index);
}
list *options = read_data_cfg(datacfg);
int classes = option_find_int(options, "classes", 1);
char *name_list = option_find_str(options, "names", 0);
char **names = get_labels(name_list);

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow("Regressor", CV_WINDOW_NORMAL);
Expand All @@ -171,19 +177,23 @@ void demo_regressor(char *datacfg, char *cfgfile, char *weightfile, int cam_inde
gettimeofday(&tval_before, NULL);

image in = get_image_from_stream(cap);
image in_s = letterbox_image(in, net->w, net->h);
show_image(in, "Regressor");
image crop = center_crop_image(in, net->w, net->h);
grayscale_image_3c(crop);
show_image(crop, "Regressor");

float *predictions = network_predict(net, in_s.data);
float *predictions = network_predict(net, crop.data);

printf("\033[2J");
printf("\033[1;1H");
printf("\nFPS:%.0f\n",fps);

printf("People: %f\n", predictions[0]);
int i;
for(i = 0; i < classes; ++i){
printf("%s: %f\n", names[i], predictions[i]);
}

free_image(in_s);
free_image(in);
free_image(crop);

cvWaitKey(10);

Expand Down
4 changes: 4 additions & 0 deletions include/darknet.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ float train_networks(network **nets, int n, data d, int interval);
void sync_nets(network **nets, int n, int interval);
void harmless_update_network_gpu(network *net);
#endif
image get_label(image **characters, char *string, int size);
void draw_label(image a, int r, int c, image label, const float *rgb);
void save_image_png(image im, const char *name);
void get_next_batch(data d, int n, int offset, float *X, float *y);
void grayscale_image_3c(image im);
Expand Down Expand Up @@ -683,8 +685,10 @@ image load_image(char *filename, int w, int h, int c);
image load_image_color(char *filename, int w, int h);
image make_image(int w, int h, int c);
image resize_image(image im, int w, int h);
void censor_image(image im, int dx, int dy, int w, int h);
image letterbox_image(image im, int w, int h);
image crop_image(image im, int dx, int dy, int w, int h);
image center_crop_image(image im, int w, int h);
image resize_min(image im, int min);
image resize_max(image im, int max);
image threshold_image(image im, float thresh);
Expand Down
Loading

0 comments on commit b40bbdc

Please sign in to comment.