DarkNet,Visual Studio 2015工程配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m_buddy/article/details/82634136

1. 前言

最近使用DarkNet做yolo目标检测,为了方便梳理代码的结构,理顺其内部的实现过程,这里将DarkNet的源码提出来,融合到Visual Studio工程中去,这里将这个过程记录下来。
PS:这里使用的系统是Win10 x64,Visual Studio 2015,Win32控制台应用程序

2. 整合过程

2.1 配置Pthread库

下载Windows版本的Pthread库,将编译好的.lib与.dll提供出来给DarkNet使用。之后便是配置Pthread库,其配置过程与配置Opencv的过程类似,这里就不多说了(网上很多opencv配置的文章,贯通一下就好)。需要注意的是需要将bin目录添加到系统的环境变量,并且重启电脑。

2.2 配置DarkNet

将Git clone下来文件解压并打开,得到下图,并且将下图中红框圈中的文件夹复制到工程目录
这里写图片描述
(1)将include目录添加到工程包含目录
(2)将src中的代码添加到工程中去,移除compare.c、demo.c、demo.h

2.3 修改代码

由于DarkNet是在Linux环境下开发编译的,移植到windows上难免会有问题,因而首先第一步便是注释掉

#include <unistd.h>
#include <sys/time.h>

再来修改utils.c文件中的what_time_is_it_now()函数

double what_time_is_it_now()
{
    clock_t start;
    start = clock();
    return (double)(start);
}

其实这一部分就是一个计时函数,我这里用clock()函数代替了,其他的计时函数请参考下面连接:【C/C++】计时函数比较

2.4 添加main函数

这里我目前主要用测试函数,所以main函数只有测试功能,其它功能各位阅读源码自行添加吧。
PS:由于DarkNet是纯C语言编写的库,所以要是你添加main函数选成了CPP文件那就GG了,而且也不能包含C++的库。还是改成c文件吧。这里我给大家一个main函数的demo

// DarkNet.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "include\darknet.h"
#include <stdio.h>
#include <stdlib.h>

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;
    while (1) {
        if (filename) {
            strncpy(input, filename, 256);
        }
        else {
            printf("Enter Image Path: ");
            fflush(stdout);
            input = fgets(input, 256, stdin);
            if (!input) return;
            strtok(input, "\n");
        }
        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)*1.0/1000.0);
        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 OPENCV
            cvNamedWindow("predictions", CV_WINDOW_NORMAL);
            if (fullscreen) {
                cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
            }
            show_image(im, "predictions", 0);
#endif
        }

        free_image(im);
        free_image(sized);
        if (filename) break;
    }
}

//主函数
int main()
{
    char* datacfg = "./cfg/voc.data";
    char* cfg = "./cfg/yolov3-spp.cfg";
    char* weights = "yolov3-spp_40000.weights";
    char* filename = "./test_case/test1.jpg";
    float thresh = 0.5;
    float hier_thresh = 0.5;
    char *outfile = "detection_out.png";
    int fullscreen=0;
    test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, outfile, fullscreen);
    return 0;
}

在此之外还可以参考:Darknet windows移植(YOLO v2)

3. 运行结果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/m_buddy/article/details/82634136