7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Yolo v3 Multiple-inputに対応させる(複数枚画像)

Last updated at Posted at 2018-10-01

#1. 概要
Yolo v3で複数枚の画像を識別しようとすると、毎回モデルの読み込みが入って面倒ですよね。

どうやら以下の人のリファクタリングされたソースコードを用いれば、Multiple inputも出来るみたいですが、
なるべく本家のソースコードをいじってやりたいなーと。

#2. 理想

こんな感じに入力したらtest.txt内の画像が全て識別されればうれしいなと。

./darknet detector test ./cfg/datasets.data ./cfg/testing.cfg ./cfg/backup/classifier_100.weights  < ./cfg/test.txt

なおtest.txtの中身は以下のような感じ

/home/user/pic/a.png
/home/user/pic/b.png
/home/user/pic/c.png
...

#3. ソースコードの変更
OpenCVのフラグを立ててMakeをしておく必要がある。darknetフォルダ直下にて作業を行う。

$ vi examples/detector.c

563 void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
564 {
565     list *options = read_data_cfg(datacfg);
566     char *name_list = option_find_str(options, "names", "data/names.list");
567     char **names = get_labels(name_list);
568
569     image **alphabet = load_alphabet();
570     network *net = load_network(cfgfile, weightfile, 0);
571     set_batch_network(net, 1);
572     srand(2222222);
573     double time;
574     char buff[256];
575     char *input = buff;
576     float nms=.45;
577     while(1){
578         if(filename){
579             strncpy(input, filename, 256);
580         } else {
581             printf("Enter Image Path: ");
582             fflush(stdout);
583             input = fgets(input, 256, stdin);
584             if(!input) return;
585             strtok(input, "\n");
586         }
587         image im = load_image_color(input,0,0);
588         image sized = letterbox_image(im, net->w, net->h);
589         //image sized = resize_image(im, net->w, net->h);
590         //image sized2 = resize_max(im, net->w);
591         //image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
592         //resize_network(net, sized.w, sized.h);
593         layer l = net->layers[net->n-1];
594
595
596         float *X = sized.data;
597         time=what_time_is_it_now();
598         network_predict(net, X);
599         printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);
600         int nboxes = 0;
601         detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
602         //printf("%d\n", nboxes);
603         //if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
604         if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
605         draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
606         free_detections(dets, nboxes);
607         if(outfile){
608             save_image(im, outfile);
609         }
610         else{
611             save_image(im, "predictions");
612 #ifdef OPENCV
613             /*cvNamedWindow("predictions", CV_WINDOW_NORMAL);
614             if(fullscreen){
615                 cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
616             }
617             show_image(im, "predictions");
618             cvWaitKey(0);
619             cvDestroyAllWindows();*/
620 #endif
621         }
622
623         free_image(im);
624         free_image(sized);
625         if (filename) break;
626     }
627 }

取り合えず、opencvのところを全てコメントアウトしておけばおk。
コメントアウトしなければ毎回、識別結果の画像が開いてしまう。

後はsave_image(im, "predictions");こいつをwhile毎にRenameしてやれば、識別した画像のデータが全て保存される。

7
5
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?