main.cpp 3.45 KB
Newer Older
Your Name's avatar
Your Name committed
1
2
3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
liucong's avatar
liucong committed
4
5
6
#include <Crnn.h>
#include <SimpleLog.h>
#include <Filesystem.h>
Your Name's avatar
Your Name committed
7
8
9
10
11

void MIGraphXSamplesUsage(char* programName)
{
    printf("Usage : %s <index> \n", programName);
    printf("index:\n");
liucong's avatar
liucong committed
12
13
    printf("\t 0) Crnn sample.\n");
    printf("\t 1) Crnn Dynamic sample.\n");
Your Name's avatar
Your Name committed
14
15
}

liucong's avatar
liucong committed
16
17
18
void Sample_Crnn();
void Sample_Crnn_Dynamic();

Your Name's avatar
Your Name committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
    }
    switch (*argv[1])
    {
liucong's avatar
liucong committed
33
        case '0':
Your Name's avatar
Your Name committed
34
35
36
37
            {
                Sample_Crnn();
                break;
            }
liucong's avatar
liucong committed
38
        case '1':
Your Name's avatar
Your Name committed
39
40
41
42
43
44
45
46
47
48
49
            {
                Sample_Crnn_Dynamic();
                break;
            }
        default :
            {
                MIGraphXSamplesUsage(argv[0]);
                break;
            }
    }
    return 0;
liucong's avatar
liucong committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
}

void Sample_Crnn()
{
    // 创建CRNN文本识别
    migraphxSamples::Crnn crnn;
    migraphxSamples::InitializationParameterOfOcr initParamOfOcrCRNN;
    initParamOfOcrCRNN.configFilePath = CONFIG_FILE;
    migraphxSamples::ErrorCode errorCode=crnn.Initialize(initParamOfOcrCRNN, false);
    if(errorCode!=migraphxSamples::SUCCESS)
    {
        LOG_ERROR(stdout, "fail to initialize crnn!\n");
        exit(-1);
    }
    LOG_INFO(stdout, "succeed to initialize crnn\n");

    // 读取测试图片
    cv:: Mat srcImage=cv::imread("../Resource/Images/text.jpg", 1);

    // 推理
    std::vector<char> resultRaw;
    std::vector<char> resultSim;
    double time1 = cv::getTickCount();
    crnn.Infer(srcImage,resultRaw, true, false);
    crnn.Infer(srcImage,resultSim, false, false);
    double time2 = cv::getTickCount();
    double elapsedTime = (time2 - time1)*1000*0.5 / cv::getTickFrequency();
    LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime);

    // 获取推理结果
    LOG_INFO(stdout,"========== Ocr Results ==========\n");
    for(int i = 0; i < resultRaw.size(); i++)
    {
          std::cout << resultRaw.at(i);
    }    
    std::cout << " => ";
    for(int i = 0; i < resultSim.size(); i++)
    {
        std::cout << resultSim.at(i);
    }
    std::cout << std::endl;
}

void Sample_Crnn_Dynamic()
{
    // 创建CRNN文本识别
    migraphxSamples::Crnn crnn;
    migraphxSamples::InitializationParameterOfOcr initParamOfOcrCRNN;
    initParamOfOcrCRNN.configFilePath = CONFIG_FILE;
    migraphxSamples::ErrorCode errorCode=crnn.Initialize(initParamOfOcrCRNN, true);
    if(errorCode!=migraphxSamples::SUCCESS)
    {
        LOG_ERROR(stdout, "fail to initialize crnn!\n");
        exit(-1);
    }
    LOG_INFO(stdout, "succeed to initialize crnn\n");

    // 读取测试图片
    std::vector<cv::Mat> srcImages;
    cv::String folder = "../Resource/Images/CrnnDynamicPic";
    std::vector<cv::String> imagePathList;
    cv::glob(folder,imagePathList);
    for (int i = 0; i < imagePathList.size(); ++i)
    {
        cv:: Mat srcImage=cv::imread(imagePathList[i], 1);
        srcImages.push_back(srcImage); 
    }

    // 获取推理结果
    LOG_INFO(stdout,"========== Ocr Results ==========\n");
    for(int i=0; i<srcImages.size(); ++i)
    { 
        std::vector<char> resultSim;
        crnn.Infer(srcImages[i],resultSim, false, true);

        for(int i = 0; i < resultSim.size(); i++)
        {
            std::cout << resultSim.at(i);
        }
        std::cout << std::endl;
    }
    
Your Name's avatar
Your Name committed
132
}