yolo源码解析(3):视频检测流程
代码在自己电脑中!!!!不在服务器
根据前文所说yolo代码逻辑:
├── examples
│ ├── darknet.c(主程序)
│ │── xxx1.c
│ └── xxx2.c
│
├── include
│ ├── darknet.h
│
│
├── Makefile
│
│
└── src
├── yyy1.c
├── yyy2.h
└──......
视频检测入口时darknet.c文件。
输入视频检测命令,如: ./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights ~/dataset/ca3.mp4 ,darknet.c中根据detector参数,调用detector.c中的 run_detector(int argc, char **argv); 函数.
run_detector函数内容如下
void run_detector(int argc, char **argv) { char *prefix = find_char_arg(argc, argv, "-prefix", 0); float thresh = find_float_arg(argc, argv, "-thresh", .5); float hier_thresh = find_float_arg(argc, argv, "-hier", .5); int cam_index = find_int_arg(argc, argv, "-c", 0); int frame_skip = find_int_arg(argc, argv, "-s", 0); int avg = find_int_arg(argc, argv, "-avg", 3); if(argc < 4){ fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); return; } char *gpu_list = find_char_arg(argc, argv, "-gpus", 0); char *outfile = find_char_arg(argc, argv, "-out", 0); int *gpus = 0; int gpu = 0; int ngpus = 0; if(gpu_list){ printf("%s\n", gpu_list); int len = strlen(gpu_list); ngpus = 1; int i; for(i = 0; i < len; ++i){ if (gpu_list[i] == ',') ++ngpus; } gpus = calloc(ngpus, sizeof(int)); for(i = 0; i < ngpus; ++i){ gpus[i] = atoi(gpu_list); gpu_list = strchr(gpu_list, ',')+1; } } else { gpu = gpu_index; gpus = &gpu; ngpus = 1; } int clear = find_arg(argc, argv, "-clear"); int fullscreen = find_arg(argc, argv, "-fullscreen"); 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);
代码中定义了frame_skip等变量,默认值为0.然后继续调用demo函数,demo函数位于/darknet/src/demo.c文件中,内容如下。
void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen) { //demo_frame = avg_frames; image **alphabet = load_alphabet(); demo_names = names; demo_alphabet = alphabet; demo_classes = classes; demo_thresh = thresh; demo_hier = hier; printf("Demo\n"); net = load_network(cfgfile, weightfile, 0); set_batch_network(net, 1); pthread_t detect_thread; pthread_t fetch_thread; srand(2222222); int i; demo_total = size_network(net); predictions = calloc(demo_frame, sizeof(float*)); for (i = 0; i < demo_frame; ++i){ predictions[i] = calloc(demo_total, sizeof(float)); } avg = calloc(demo_total, sizeof(float)); if(filename){ printf("video file: %s\n", filename); cap = open_video_stream(filename, 0, 0, 0, 0); }else{ cap = open_video_stream(0, cam_index, w, h, frames); } if(!cap) error("Couldn't connect to webcam.\n"); buff[0] = get_image_from_stream(cap); buff[1] = copy_image(buff[0]); buff[2] = copy_image(buff[0]); buff_letter[0] = letterbox_image(buff[0], net->w, net->h); buff_letter[1] = letterbox_image(buff[0], net->w, net->h); buff_letter[2] = letterbox_image(buff[0], net->w, net->h); int count = 0; if(!prefix){ make_window("Demo", 1352, 1013, fullscreen); } demo_time = what_time_is_it_now(); while(!demo_done){ buff_index = (buff_index + 1) %3; if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed"); if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed"); if(!prefix){ fps = 1./(what_time_is_it_now() - demo_time); demo_time = what_time_is_it_now(); display_in_thread(0); }else{ char name[256]; sprintf(name, "%s_%08d", prefix, count); save_image(buff[(buff_index + 1)%3], name); } pthread_join(fetch_thread, 0); pthread_join(detect_thread, 0); ++count; } }
yolo视频处理的参考:https://blog.csdn.net/sinat_33718563/article/details/79964758
只用cpu的情况下处理视频是在太慢,添加GPU支持,编译时遇到问题
https://github.com/AlexeyAB/darknet/issues/894 参考解决方案
首先查看安装的cuda版本:
hpclib@liu:~$ cat /usr/local/cuda/version.txt CUDA Version 9.0.176
然后查看gedit ~/.bashrc中的变量设置
export PATH=/usr/local/cuda-8.0/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
发现与当前版本不对应,改为9.0即可
编译通过。
然而检测时会出现如下错误
CUDA Error: out of memory darknet: ./src/cuda.c:36: check_error: Assertion `0' failed. 已放弃 (核心已转储)
先用yolo3-tiny即可
./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg
如若扔出现此错误,加sudo试试
视频检测
./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights
二、mysql访问
首先安装软件包libmysqlclient-dev sudo apt-get install libmysqlclient-dev
然后根据教程https://blog.csdn.net/tennysonsky/article/details/53984818
编写代码
#include <stdio.h> #include <stdlib.h> #include <mysql.h> int main (int argc, char *argv[]) { MYSQL *conn; // 步骤1: 初始化连接句柄 conn = mysql_init(NULL); if (conn == NULL) { // 如果返回NULl说明初始化失败 printf("mysql_init failed!\n"); return EXIT_FAILURE; } // 步骤2:实际进行连接 // 参数分别为,conn连接句柄,host是MySQL所在主机或地址,user用户名,password密码,database_name数据库名,后面的都是默认 conn = mysql_real_connect(conn, "192.168.1.6", "root", "TJU55b425", "rdshare", 0, NULL, 0); if (conn) { // 连接成功 printf("Connection success!\n"); } else { printf("Connection failed!\n"); } // 步骤3: 退出前关闭连接 mysql_close(conn); return 0; }
编译 gcc -I/usr/include/mysql tmysqlest.c -L/usr/lib/mysql -lmysqlclient -o mysqltest 并执行:./mysqltest
成功连接。
然后考虑将代码添加到yolo源码中。