main.cpp 2.27 KB
Newer Older
shangxl's avatar
shangxl committed
1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <DeepLabV3.h>
#include <SimpleLog.h>
#include <Filesystem.h>


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void MIGraphXSamplesUsage(char *programName)
{
    printf("Usage : %s <index> \n", programName);
    printf("index:\n");
    printf("\t 0) DeepLabV3 Single Image Sample.\n");
    printf("\t 1) DeepLabV3 Multiple Image Sample.\n");
}


int main(int argc,char *argv[]){

    if (argc < 2 || argc > 2)
    {
        MIGraphXSamplesUsage(argv[0]);
        return -1;
    }
    if (!strncmp(argv[1], "-h", 2))
    {
        MIGraphXSamplesUsage(argv[0]);
        return 0;
    }

shangxl's avatar
shangxl committed
31
    migraphxSamples::InitializationParameterOfSegmentation initParamOfSegmentationUnet;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    std::vector<cv::Mat> srcImages;
    switch(*argv[1]){
        case '0':
            srcImages.push_back(cv::imread("../Resource/Images/000001.jpg", 1));
            initParamOfSegmentationUnet.loadMode = 1;                                         //加载单个图片
            break;
        case '1':
            std::string folderPath = "../Resource/Images/";
            std::string imageExt = "*.jpg";
            std::vector<cv::String> imagePaths;
            cv::glob(folderPath + imageExt, imagePaths, false);
            for(const auto& path : imagePaths){
                srcImages.push_back(cv::imread(path, 1));
            }
            initParamOfSegmentationUnet.loadMode = 0;                                        //加载多个图片
            break;
    }

    migraphxSamples::DeepLabV3 deeplabv3;
shangxl's avatar
shangxl committed
51
52
53
54
55
56
57
58
59
60
61
    initParamOfSegmentationUnet.configFilePath = CONFIG_FILE;
    migraphxSamples::ErrorCode errorCode       = deeplabv3.Initialize(initParamOfSegmentationUnet);
    if(errorCode != migraphxSamples::SUCCESS)
    {
        LOG_ERROR(stdout, "fail to initialize DeepLabV3!\n");
        exit(-1);
    }
    LOG_INFO(stdout, "succeed to initialize DeepLabV3\n");


    // 推理
62
63
    std::vector<cv::Mat> maskImages;
    deeplabv3.Segmentation(srcImages, maskImages);
shangxl's avatar
shangxl committed
64
    LOG_INFO(stdout, "========== Segmentation Results ==========\n");
65
66
67
68
69
70
    LOG_INFO(stdout, "Segmentation results have been saved to build directory\n");
    for(int i = 0;i<maskImages.size();i++){
        std::string fileName ="Result_"+std::to_string(i+1)+".jpg";
        cv::imwrite(fileName, maskImages[i]);
    }
    
shangxl's avatar
shangxl committed
71
72
73
74

    return 0;

}