使用darknet批量測試圖片并保存在指定文件夾下
測試時(shí):Makefile前五行一定全調(diào)為0
當(dāng)我們使用darknet框架使用測試語句時(shí),系統(tǒng)調(diào)用程序語句,我們需要的是加入可以連續(xù)調(diào)用圖片的系統(tǒng),在模型載入內(nèi)存的情況下,完成圖片檢測。
1.用下面代碼替換detector.c文件(example文件夾下)的void test_detector函數(shù)(注意有3處要改成自己的路徑) 全部復(fù)制并代替,三處修改路徑寫對 此段代碼來自https://blog.csdn.net/mieleizhi0522/article/details/79989754
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
{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 = load_network(cfgfile, weightfile, 0);set_batch_network(net, 1);srand(2222222);double time;char buff[256];char *input = buff;float nms=.45;int i=0;while(1){if(filename){strncpy(input, filename, 256);image im = load_image_color(input,0,0);image sized = letterbox_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];float *X = sized.data;time=what_time_is_it_now();network_predict(net, X);printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);int nboxes = 0;detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);//printf("%d\n", nboxes);//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);draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);free_detections(dets, nboxes);if(outfile){save_image(im, outfile);}else{save_image(im, "predictions");
#ifdef OPENCVcvNamedWindow("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);if (filename) break;} else {printf("Enter Image Path: ");fflush(stdout);input = fgets(input, 256, stdin);if(!input) return;strtok(input, "\n");list *plist = get_paths(input);char **paths = (char **)list_to_array(plist);printf("Start Testing!\n");int m = plist->size;if(access("/home/FENGsl/darknet/data/out",0)==-1)//"/home/FENGsl/darknet/data"修改成自己的路徑{if (mkdir("/home/FENGsl/darknet/data/out",0777))//"/home/FENGsl/darknet/data"修改成自己的路徑{printf("creat file bag failed!!!");}}for(i = 0; i < m; ++i){char *path = paths[i];image im = load_image_color(path,0,0);image sized = letterbox_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];float *X = sized.data;time=what_time_is_it_now();network_predict(net, X);printf("Try Very Hard:");printf("%s: Predicted in %f seconds.\n", path, what_time_is_it_now()-time);int nboxes = 0;detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);//printf("%d\n", nboxes);//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);draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);free_detections(dets, nboxes);if(outfile){save_image(im, outfile);}else{char b[2048];sprintf(b,"/home/FENGsl/darknet/data/out/%s",GetFilename(path));//"/home/FENGsl/darknet/data"修改成自己的路徑save_image(im, b);printf("save %s successfully!\n",GetFilename(path));
#ifdef OPENCVcvNamedWindow("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);if (filename) break;}}}
}
2.在前面添加GetFilename(char p)函數(shù)(注意后面的注釋)
全部復(fù)制(包括頭文件) 此段代碼來自https://blog.csdn.net/mieleizhi0522/article/details/79989754
#include "darknet.h"
#include <sys/stat.h>
#include<stdio.h>
#include<time.h>
#include<sys/types.h>
static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};char *GetFilename(char *p)
{ static char name[20]={""};char *q = strrchr(p,'/') + 1;strncpy(name,q,6);//注意后面的6,如果你的測試集的圖片的名字字符(不包括后綴)是其他長度,請改為你需要的長度(官方的默認(rèn)的長度是6)return name;
}
3.在darknet下重新make 一定要記住重新make,在darknet文件下
4.建立一個(gè)含有圖片的文件夾
①文件名為6位的字符串 ②建立一個(gè)圖片絕對路徑文本
ls -R /home/******/YOLO-master/darknet/data/input/* > input.txt
展示:
5.執(zhí)行批量測試命令如下
命令:./darknet detect cfg/yolov3.cfg yolov3.weights Enter Image Path:輸入input.txt的路徑
./darknet detect cfg/yolov3.cfg yolov3.weights
layer filters size input output0 conv 32 3 x 3 / 1 608 x 608 x 3 -> 608 x 608 x 32 0.639 BFLOPs1 conv 64 3 x 3 / 2 608 x 608 x 32 -> 304 x 304 x 64 3.407 BFLOPs. . . . . . . 105 conv 255 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 255 0.754 BFLOPs106 yolo
Loading weights from yolov3.weights...Done!
Enter Image Path:
6.之后就完成了,生成的圖片在out文件下
我的效果展示
圖片視頻相互轉(zhuǎn)換
視頻轉(zhuǎn)圖片
import cv2cap=cv2.VideoCapture("./test/test.mp4")
i=1
while True:ret,im=cap.read()cv2.imwrite("./input/%06d.jpg"%i,im)i = i+1print(i)if i == 72:break
圖片轉(zhuǎn)視頻
import cv2
import osim_dir = './output_yolov3'
num = 72 #這里是幀數(shù)out = cv2.VideoWriter('aa.avi', 0, 29,(1280,720)) #每一個(gè)圖片的大小必須一致與確定for i in range(1,num):print(str("%06d"%i))im_name = os.path.join(im_dir, str("%06d"%i)+'.jpg')frame = cv2.imread(im_name)cv2.imshow("frame",frame)out.write(frame)# print(im_name)out.release()
print('finish')
總結(jié)
以上是生活随笔 為你收集整理的亲试:darknet_yolov3批量测试图片并保存在自定义文件夹下与图片视频相互转换 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。