CommonUtility.cpp 5.97 KB
Newer Older
lijian6's avatar
lijian6 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#define DLLAPI_EXPORTS
#include <CommonUtility.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <sstream>
#include <vector>

#ifdef _WIN32
#include <io.h>
#include <direct.h>
#include <Windows.h>
#else
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/time.h>
#endif

#include <SimpleLog.h>

namespace migraphxSamples
{


_Time GetCurrentTime3()
{
    _Time currentTime;

#if (defined WIN32 || defined _WIN32)
	SYSTEMTIME systemTime;
	GetLocalTime(&systemTime);

	char temp[8] = { 0 };
	sprintf(temp, "%04d", systemTime.wYear);
	currentTime.year=string(temp);
	sprintf(temp, "%02d", systemTime.wMonth);
	currentTime.month=string(temp);
	sprintf(temp, "%02d", systemTime.wDay);
	currentTime.day=string(temp);
	sprintf(temp, "%02d", systemTime.wHour);
	currentTime.hour=string(temp);
	sprintf(temp, "%02d", systemTime.wMinute);
	currentTime.minute=string(temp);
	sprintf(temp, "%02d", systemTime.wSecond);
	currentTime.second=string(temp);
	sprintf(temp, "%03d", systemTime.wMilliseconds);
	currentTime.millisecond=string(temp);
	sprintf(temp, "%d", systemTime.wDayOfWeek);
	currentTime.weekDay=string(temp);
#else
	struct timeval    tv;
	struct tm         *p;
	gettimeofday(&tv, NULL);
	p = localtime(&tv.tv_sec);

	char temp[8]={0};
    sprintf(temp,"%04d",1900+p->tm_year);
    currentTime.year=string(temp);
	sprintf(temp,"%02d",1+p->tm_mon);
	currentTime.month=string(temp);
	sprintf(temp,"%02d",p->tm_mday);
	currentTime.day=string(temp);
	sprintf(temp,"%02d",p->tm_hour);
	currentTime.hour=string(temp);
	sprintf(temp,"%02d",p->tm_min);
	currentTime.minute=string(temp);
	sprintf(temp,"%02d",p->tm_sec);
	currentTime.second=string(temp);
	sprintf(temp,"%03ld",tv.tv_usec/1000);
	currentTime.millisecond = string(temp);
    sprintf(temp, "%03ld", tv.tv_usec % 1000);
	currentTime.microsecond = string(temp);
    sprintf(temp, "%d", p->tm_wday);
    currentTime.weekDay = string(temp);
#endif
    return currentTime;
}

string GetCurrentTimeString()
{

    char timeString[256]={0};
    _Time currentTime=GetCurrentTime3();
    sprintf(timeString,"%s%s%s%s%s%s%s",currentTime.year.c_str(),currentTime.month.c_str(),
            currentTime.day.c_str(),currentTime.hour.c_str(),
            currentTime.minute.c_str(),currentTime.second.c_str(),currentTime.millisecond.c_str());

    return timeString;

}

vector<string> SplitString(string str, std::string separator)
{
    std::string::size_type pos;
    std::vector<std::string> result;
    str+=separator;//扩展字符串以方便操作
    int size=str.size();

    for(int i=0; i<size; i++)
    {
        pos=str.find(separator,i);
        if(pos<size)
        {
            std::string s=str.substr(i,pos-i);
            result.push_back(s);
            i=pos+separator.size()-1;
        }
    }
    return result;
}


bool CompareConfidence(const ResultOfDetection &L,const ResultOfDetection &R)
{
    return L.confidence > R.confidence;
}

bool CompareArea(const ResultOfDetection &L,const ResultOfDetection &R)
{
    return L.boundingBox.area() > R.boundingBox.area();
}

void NMS(vector<ResultOfDetection> &detections, float IOUThreshold)
{
    // sort
    std::sort(detections.begin(), detections.end(), CompareConfidence);

    for (int i = 0; i<detections.size(); ++i)
    {
        if (detections[i].exist)
        {
            for (int j = i + 1; j<detections.size(); ++j)
            {
                if (detections[j].exist)
                {
                    // compute IOU
                    float intersectionArea = (detections[i].boundingBox & detections[j].boundingBox).area();
                    float intersectionRate = intersectionArea / (detections[i].boundingBox.area() + detections[j].boundingBox.area() - intersectionArea);

                    if (intersectionRate>IOUThreshold)
                    {
                        detections[j].exist = false;
                    }
                }
            }
        }
    }

}

migraphx::parameter_map CreateParameterMap(migraphx::program & p)
{
    migraphx::parameter_map parameterMap;
    for (std::pair<std::string, migraphx::shape> x : p.get_parameter_shapes())
    {
        parameterMap[x.first] = migraphx::gpu::to_gpu(migraphx::generate_argument(x.second));
    }
    return parameterMap;
}

//void print_result(const std::vector<OCRPredictResult> ocr_result)
//{
//    for (int i = 0; i < ocr_result.size(); i++)
//    {
//        std::cout << i << "\t";
//        std::vector<std::vector<int>> boxes = ocr_result[i].box;
//        if (boxes.size() > 0)
//        {
//            std::cout << "det boxes: [";
//            for (int n = 0; n < boxes.size(); n++)
//            {
//                std::cout << '[' << boxes[n][0] << ',' << boxes[n][1] << "]";
//                if (n != boxes.size() - 1)
//                    std::cout << ',';
//            }
//            std::cout << "] ";
//        }
//        if (ocr_result[i].score != -1.0)
//        {
//            std::cout << "rec text: " << ocr_result[i].text << " rec score: " << ocr_result[i].score << " ";
//        }
//        if (ocr_result[i].cls_label != -1)
//        {
//            std::cout << "cls label: " << ocr_result[i].cls_label << " cls score: " << ocr_result[i].cls_score;
//        }
//    }
//}
//
//void VisualizeBboxes(const cv::Mat &srcimg,
//                     const std::vector<OCRPredictResult> &ocr_result,
//                     const std::string &save_path) {
//    cv::Mat img_vis;
//    srcimg.copyTo(img_vis);
//    for (int n = 0; n < ocr_result.size(); n++) {
//        cv::Point rook_points[4];
//        for (int m = 0; m < ocr_result[n].box.size(); m++) {
//            rook_points[m] = cv::Point(int(ocr_result[n].box[m][0]), int(ocr_result[n].box[m][1]));
//        }
//
//        const cv::Point *ppt[1] = {rook_points};
//        int npt[] = {4};
//        cv::polylines(img_vis, ppt, npt, 1, 1, CV_RGB(0, 255, 0), 2, 8, 0);
//    }
//
//    cv::imwrite(save_path, img_vis);
//    std::cout << "The detection visualized image saved in " + save_path << std::endl;
//}

}