"official/projects/movinet/train.py" did not exist on "4ab6f1b1fd6bc89d50012dd08860ea72fb256c00"
utility.cpp 14.2 KB
Newer Older
Your Name's avatar
Your Name 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
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <dirent.h>
#include <utility.h>
#include <iostream>
#include <ostream>

#include <vector>

#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif

liucong's avatar
liucong committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace migraphxSamples
{

std::vector<std::string> Utility::ReadDict(const std::string& path)
{
    std::ifstream in(path);
    std::string line;
    std::vector<std::string> m_vec;
    if(in)
    {
        while(getline(in, line))
        {
            m_vec.push_back(line);
        }
    }
    else
    {
        std::cout << "no such label file: " << path << ", exit the program..." << std::endl;
        exit(1);
Your Name's avatar
Your Name committed
47
    }
liucong's avatar
liucong committed
48
    return m_vec;
Your Name's avatar
Your Name committed
49
50
}

liucong's avatar
liucong committed
51
52
53
54
55
56
57
58
59
60
61
62
63
void Utility::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]));
        }
Your Name's avatar
Your Name committed
64

liucong's avatar
liucong committed
65
66
67
68
        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);
    }
Your Name's avatar
Your Name committed
69

liucong's avatar
liucong committed
70
71
    cv::imwrite(save_path, img_vis);
    std::cout << "The detection visualized image saved in " + save_path << std::endl;
Your Name's avatar
Your Name committed
72
73
}

liucong's avatar
liucong committed
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
void Utility::VisualizeBboxes(const cv::Mat& srcimg,
                              const StructurePredictResult& structure_result,
                              const std::string& save_path)
{
    cv::Mat img_vis;
    srcimg.copyTo(img_vis);
    img_vis = crop_image(img_vis, structure_result.box);
    for(int n = 0; n < structure_result.cell_box.size(); n++)
    {
        if(structure_result.cell_box[n].size() == 8)
        {
            cv::Point rook_points[4];
            for(int m = 0; m < structure_result.cell_box[n].size(); m += 2)
            {
                rook_points[m / 2] = cv::Point(int(structure_result.cell_box[n][m]),
                                               int(structure_result.cell_box[n][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);
        }
        else if(structure_result.cell_box[n].size() == 4)
        {
            cv::Point rook_points[2];
            rook_points[0] = cv::Point(int(structure_result.cell_box[n][0]),
                                       int(structure_result.cell_box[n][1]));
            rook_points[1] = cv::Point(int(structure_result.cell_box[n][2]),
                                       int(structure_result.cell_box[n][3]));
            cv::rectangle(img_vis, rook_points[0], rook_points[1], CV_RGB(0, 255, 0), 2, 8, 0);
        }
Your Name's avatar
Your Name committed
104
105
    }

liucong's avatar
liucong committed
106
107
    cv::imwrite(save_path, img_vis);
    std::cout << "The table visualized image saved in " + save_path << std::endl;
Your Name's avatar
Your Name committed
108
109
110
}

// list all files under a directory
liucong's avatar
liucong committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
void Utility::GetAllFiles(const char* dir_name, std::vector<std::string>& all_inputs)
{
    if(NULL == dir_name)
    {
        std::cout << " dir_name is null ! " << std::endl;
        return;
    }
    struct stat s;
    stat(dir_name, &s);
    if(!S_ISDIR(s.st_mode))
    {
        std::cout << "dir_name is not a valid directory !" << std::endl;
        all_inputs.push_back(dir_name);
        return;
Your Name's avatar
Your Name committed
125
    }
liucong's avatar
liucong committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    else
    {
        struct dirent* filename; // return value for readdir()
        DIR* dir;                // return value for opendir()
        dir = opendir(dir_name);
        if(NULL == dir)
        {
            std::cout << "Can not open dir " << dir_name << std::endl;
            return;
        }
        std::cout << "Successfully opened the dir !" << std::endl;
        while((filename = readdir(dir)) != NULL)
        {
            if(strcmp(filename->d_name, ".") == 0 || strcmp(filename->d_name, "..") == 0)
                continue;
            // img_dir + std::string("/") + all_inputs[0];
            all_inputs.push_back(dir_name + std::string("/") + std::string(filename->d_name));
        }
Your Name's avatar
Your Name committed
144
145
146
    }
}

liucong's avatar
liucong committed
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
cv::Mat Utility::GetRotateCropImage(const cv::Mat& srcimage, std::vector<std::vector<int>> box)
{
    cv::Mat image;
    srcimage.copyTo(image);
    std::vector<std::vector<int>> points = box;

    int x_collect[4] = {box[0][0], box[1][0], box[2][0], box[3][0]};
    int y_collect[4] = {box[0][1], box[1][1], box[2][1], box[3][1]};
    int left         = int(*std::min_element(x_collect, x_collect + 4));
    int right        = int(*std::max_element(x_collect, x_collect + 4));
    int top          = int(*std::min_element(y_collect, y_collect + 4));
    int bottom       = int(*std::max_element(y_collect, y_collect + 4));

    cv::Mat img_crop;
    image(cv::Rect(left, top, right - left, bottom - top)).copyTo(img_crop);

    for(int i = 0; i < points.size(); i++)
    {
        points[i][0] -= left;
        points[i][1] -= top;
    }

    int img_crop_width =
        int(sqrt(pow(points[0][0] - points[1][0], 2) + pow(points[0][1] - points[1][1], 2)));
    int img_crop_height =
        int(sqrt(pow(points[0][0] - points[3][0], 2) + pow(points[0][1] - points[3][1], 2)));

    cv::Point2f pts_std[4];
    pts_std[0] = cv::Point2f(0., 0.);
    pts_std[1] = cv::Point2f(img_crop_width, 0.);
    pts_std[2] = cv::Point2f(img_crop_width, img_crop_height);
    pts_std[3] = cv::Point2f(0.f, img_crop_height);

    cv::Point2f pointsf[4];
    pointsf[0] = cv::Point2f(points[0][0], points[0][1]);
    pointsf[1] = cv::Point2f(points[1][0], points[1][1]);
    pointsf[2] = cv::Point2f(points[2][0], points[2][1]);
    pointsf[3] = cv::Point2f(points[3][0], points[3][1]);

    cv::Mat M = cv::getPerspectiveTransform(pointsf, pts_std);

    cv::Mat dst_img;
    cv::warpPerspective(
        img_crop, dst_img, M, cv::Size(img_crop_width, img_crop_height), cv::BORDER_REPLICATE);

    if(float(dst_img.rows) >= float(dst_img.cols) * 1.5)
    {
        cv::Mat srcCopy = cv::Mat(dst_img.rows, dst_img.cols, dst_img.depth());
        cv::transpose(dst_img, srcCopy);
        cv::flip(srcCopy, srcCopy, 0);
        return srcCopy;
    }
    else
    {
        return dst_img;
    }
Your Name's avatar
Your Name committed
203
204
}

liucong's avatar
liucong committed
205
206
207
208
209
210
std::vector<int> Utility::argsort(const std::vector<float>& array)
{
    const int array_len(array.size());
    std::vector<int> array_index(array_len, 0);
    for(int i = 0; i < array_len; ++i)
        array_index[i] = i;
Your Name's avatar
Your Name committed
211

liucong's avatar
liucong committed
212
213
214
    std::sort(array_index.begin(), array_index.end(), [&array](int pos1, int pos2) {
        return (array[pos1] < array[pos2]);
    });
Your Name's avatar
Your Name committed
215

liucong's avatar
liucong committed
216
    return array_index;
Your Name's avatar
Your Name committed
217
218
}

liucong's avatar
liucong committed
219
220
221
222
223
224
std::string Utility::basename(const std::string& filename)
{
    if(filename.empty())
    {
        return "";
    }
Your Name's avatar
Your Name committed
225

liucong's avatar
liucong committed
226
227
    auto len   = filename.length();
    auto index = filename.find_last_of("/\\");
Your Name's avatar
Your Name committed
228

liucong's avatar
liucong committed
229
230
231
232
    if(index == std::string::npos)
    {
        return filename;
    }
Your Name's avatar
Your Name committed
233

liucong's avatar
liucong committed
234
235
    if(index + 1 >= len)
    {
Your Name's avatar
Your Name committed
236

liucong's avatar
liucong committed
237
238
        len--;
        index = filename.substr(0, len).find_last_of("/\\");
Your Name's avatar
Your Name committed
239

liucong's avatar
liucong committed
240
241
242
243
        if(len == 0)
        {
            return filename;
        }
Your Name's avatar
Your Name committed
244

liucong's avatar
liucong committed
245
246
247
248
        if(index == 0)
        {
            return filename.substr(1, len - 1);
        }
Your Name's avatar
Your Name committed
249

liucong's avatar
liucong committed
250
251
252
253
        if(index == std::string::npos)
        {
            return filename.substr(0, len);
        }
Your Name's avatar
Your Name committed
254

liucong's avatar
liucong committed
255
256
        return filename.substr(index + 1, len - index - 1);
    }
Your Name's avatar
Your Name committed
257

liucong's avatar
liucong committed
258
    return filename.substr(index + 1, len - index);
Your Name's avatar
Your Name committed
259
260
}

liucong's avatar
liucong committed
261
262
bool Utility::PathExists(const std::string& path)
{
Your Name's avatar
Your Name committed
263
#ifdef _WIN32
liucong's avatar
liucong committed
264
265
    struct _stat buffer;
    return (_stat(path.c_str(), &buffer) == 0);
Your Name's avatar
Your Name committed
266
#else
liucong's avatar
liucong committed
267
268
    struct stat buffer;
    return (stat(path.c_str(), &buffer) == 0);
Your Name's avatar
Your Name committed
269
270
271
#endif // !_WIN32
}

liucong's avatar
liucong committed
272
273
void Utility::CreateDir(const std::string& path)
{
Your Name's avatar
Your Name committed
274
#ifdef _WIN32
liucong's avatar
liucong committed
275
    _mkdir(path.c_str());
Your Name's avatar
Your Name committed
276
#else
liucong's avatar
liucong committed
277
    mkdir(path.c_str(), 0777);
Your Name's avatar
Your Name committed
278
279
280
#endif // !_WIN32
}

liucong's avatar
liucong committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
void Utility::print_result(const std::vector<OCRPredictResult>& ocr_result)
{
    for(int i = 0; i < ocr_result.size(); i++)
    {
        std::cout << i << "\t";
        // det
        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 << "] ";
        }
        // rec
        if(ocr_result[i].score != -1.0)
        {
            std::cout << "rec text: " << ocr_result[i].text << " rec score: " << ocr_result[i].score
                      << " ";
Your Name's avatar
Your Name committed
306
307
        }

liucong's avatar
liucong committed
308
309
310
311
312
313
314
        // cls
        if(ocr_result[i].cls_label != -1)
        {
            std::cout << "cls label: " << ocr_result[i].cls_label
                      << " cls score: " << ocr_result[i].cls_score;
        }
        std::cout << std::endl;
Your Name's avatar
Your Name committed
315
316
317
    }
}

liucong's avatar
liucong committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
cv::Mat Utility::crop_image(cv::Mat& img, const std::vector<int>& box)
{
    cv::Mat crop_im;
    int crop_x1 = std::max(0, box[0]);
    int crop_y1 = std::max(0, box[1]);
    int crop_x2 = std::min(img.cols - 1, box[2] - 1);
    int crop_y2 = std::min(img.rows - 1, box[3] - 1);

    crop_im                = cv::Mat::zeros(box[3] - box[1], box[2] - box[0], 16);
    cv::Mat crop_im_window = crop_im(cv::Range(crop_y1 - box[1], crop_y2 + 1 - box[1]),
                                     cv::Range(crop_x1 - box[0], crop_x2 + 1 - box[0]));
    cv::Mat roi_img        = img(cv::Range(crop_y1, crop_y2 + 1), cv::Range(crop_x1, crop_x2 + 1));
    crop_im_window += roi_img;
    return crop_im;
Your Name's avatar
Your Name committed
332
333
}

liucong's avatar
liucong committed
334
335
336
337
cv::Mat Utility::crop_image(cv::Mat& img, const std::vector<float>& box)
{
    std::vector<int> box_int = {(int)box[0], (int)box[1], (int)box[2], (int)box[3]};
    return crop_image(img, box_int);
Your Name's avatar
Your Name committed
338
339
}

liucong's avatar
liucong committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
void Utility::sorted_boxes(std::vector<OCRPredictResult>& ocr_result)
{
    std::sort(ocr_result.begin(), ocr_result.end(), Utility::comparison_box);
    if(ocr_result.size() > 0)
    {
        for(int i = 0; i < ocr_result.size() - 1; i++)
        {
            for(int j = i; j > 0; j--)
            {
                if(abs(ocr_result[j + 1].box[0][1] - ocr_result[j].box[0][1]) < 10 &&
                   (ocr_result[j + 1].box[0][0] < ocr_result[j].box[0][0]))
                {
                    std::swap(ocr_result[i], ocr_result[i + 1]);
                }
            }
Your Name's avatar
Your Name committed
355
356
357
358
        }
    }
}

liucong's avatar
liucong committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
std::vector<int> Utility::xyxyxyxy2xyxy(std::vector<std::vector<int>>& box)
{
    int x_collect[4] = {box[0][0], box[1][0], box[2][0], box[3][0]};
    int y_collect[4] = {box[0][1], box[1][1], box[2][1], box[3][1]};
    int left         = int(*std::min_element(x_collect, x_collect + 4));
    int right        = int(*std::max_element(x_collect, x_collect + 4));
    int top          = int(*std::min_element(y_collect, y_collect + 4));
    int bottom       = int(*std::max_element(y_collect, y_collect + 4));
    std::vector<int> box1(4, 0);
    box1[0] = left;
    box1[1] = top;
    box1[2] = right;
    box1[3] = bottom;
    return box1;
Your Name's avatar
Your Name committed
373
374
}

liucong's avatar
liucong committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
std::vector<int> Utility::xyxyxyxy2xyxy(std::vector<int>& box)
{
    int x_collect[4] = {box[0], box[2], box[4], box[6]};
    int y_collect[4] = {box[1], box[3], box[5], box[7]};
    int left         = int(*std::min_element(x_collect, x_collect + 4));
    int right        = int(*std::max_element(x_collect, x_collect + 4));
    int top          = int(*std::min_element(y_collect, y_collect + 4));
    int bottom       = int(*std::max_element(y_collect, y_collect + 4));
    std::vector<int> box1(4, 0);
    box1[0] = left;
    box1[1] = top;
    box1[2] = right;
    box1[3] = bottom;
    return box1;
Your Name's avatar
Your Name committed
389
390
}

liucong's avatar
liucong committed
391
392
393
394
395
396
397
398
399
float Utility::fast_exp(float x)
{
    union
    {
        uint32_t i;
        float f;
    } v{};
    v.i = (1 << 23) * (1.4426950409 * x + 126.93490512f);
    return v.f;
Your Name's avatar
Your Name committed
400
401
}

liucong's avatar
liucong committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
std::vector<float> Utility::activation_function_softmax(std::vector<float>& src)
{
    int length = src.size();
    std::vector<float> dst;
    dst.resize(length);
    const float alpha = float(*std::max_element(&src[0], &src[0 + length]));
    float denominator{0};

    for(int i = 0; i < length; ++i)
    {
        dst[i] = fast_exp(src[i] - alpha);
        denominator += dst[i];
    }

    for(int i = 0; i < length; ++i)
    {
        dst[i] /= denominator;
    }
    return dst;
Your Name's avatar
Your Name committed
421
422
}

liucong's avatar
liucong committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
float Utility::iou(std::vector<int>& box1, std::vector<int>& box2)
{
    int area1 = std::max(0, box1[2] - box1[0]) * std::max(0, box1[3] - box1[1]);
    int area2 = std::max(0, box2[2] - box2[0]) * std::max(0, box2[3] - box2[1]);

    // computing the sum_area
    int sum_area = area1 + area2;

    // find the each point of intersect rectangle
    int x1 = std::max(box1[0], box2[0]);
    int y1 = std::max(box1[1], box2[1]);
    int x2 = std::min(box1[2], box2[2]);
    int y2 = std::min(box1[3], box2[3]);

    // judge if there is an intersect
    if(y1 >= y2 || x1 >= x2)
    {
        return 0.0;
    }
    else
    {
        int intersect = (x2 - x1) * (y2 - y1);
        return intersect / (sum_area - intersect + 0.00000001);
    }
Your Name's avatar
Your Name committed
447
448
}

liucong's avatar
liucong committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
float Utility::iou(std::vector<float>& box1, std::vector<float>& box2)
{
    float area1 = std::max((float)0.0, box1[2] - box1[0]) * std::max((float)0.0, box1[3] - box1[1]);
    float area2 = std::max((float)0.0, box2[2] - box2[0]) * std::max((float)0.0, box2[3] - box2[1]);

    // computing the sum_area
    float sum_area = area1 + area2;

    // find the each point of intersect rectangle
    float x1 = std::max(box1[0], box2[0]);
    float y1 = std::max(box1[1], box2[1]);
    float x2 = std::min(box1[2], box2[2]);
    float y2 = std::min(box1[3], box2[3]);

    // judge if there is an intersect
    if(y1 >= y2 || x1 >= x2)
    {
        return 0.0;
    }
    else
    {
        float intersect = (x2 - x1) * (y2 - y1);
        return intersect / (sum_area - intersect + 0.00000001);
    }
Your Name's avatar
Your Name committed
473
474
475
}

} // namespace migraphxSamples