CARLY RAE JEPSEN IS THE BEST POP ARTIST OF ALL TIME DON'T @ ME

This commit is contained in:
Joseph Redmon
2018-01-22 18:09:36 -08:00
parent e3931c75cd
commit b40bbdc7b2
10 changed files with 311 additions and 58 deletions

View File

@ -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);
@ -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;
@ -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);
@ -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);
@ -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;
@ -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);
@ -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);

View File

@ -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);
@ -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);

View File

@ -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);
@ -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;
@ -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);
@ -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);