YOLO在检测物体后会画出ROI,并在左上角标注类别。
如YOLO官网给出的预测图:
这里的物体类别描述是从配置文件中读出并画出的。想要修改让其支持中文,乍一看好像很简单,貌似只需要修改对应的配置文件就行了。理想很丰满,现实很骨感,如果这样做了,只能得到一堆乱码。
我们一步步阅读代码,来看看为什么。
代码解析
我工作中使用YOLO主要是做物体的实时检测,所以以实时检测为入口分析代码。
执行命令
./darknet detector demo cfg/coco.data cfg/yolo.cfg yolo.weights
从darknet.c的main函数看起
else if (0 == strcmp(argv[1], "detector")){
run_detector(argc, argv);
进入detector.c查看run_detector函数
else if(0==strcmp(argv[2], "demo")) {
list *options = read_data_cfg(datacfg);
int classes = option_find_int(options, "classes", 20);
char *name_list = option_find_str(options, "names", "data/names.list");
char **names = get_labels(name_list);
demo(cfg, weights, thresh, cam_index, filename, names, classes, frame_skip, prefix, hier_thresh);
}
coco.data
names = data/coco.names
coco.names
person
bicycle
car
motorbike
aeroplane
bus
train
truck
boat
......
物体类别信息就是从这得来的,是不是也从这画,我们接着看。
demo.c
void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix, float hier_thresh)
{
//skip = frame_skip;
image **alphabet = load_alphabet();
int delay = frame_skip;
demo_names = names;
demo_alphabet = alphabet;
demo_classes = classes;
demo_thresh = thresh;
demo_hier_thresh = hier_thresh;
printf("Demo\n");
net = parse_network_cfg(cfgfile);
......
while(1){
++count;
if(1){
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");
......
demo_alphabet在绘制ROI时起到重要作用,稍后会做分析。
fetch_thread负责从camera读图像数据,detect_thread负责从检测物体。
void *detect_in_thread(void *ptr)
{
float nms = .4;
laye