PaddleOCR/deploy/cpp_infer/include/utility.h

106 lines
3.5 KiB
C
Raw Normal View History

2020-07-13 16:59:21 +08:00
// 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.
#pragma once
#include <opencv2/imgproc.hpp>
2020-07-13 17:15:54 +08:00
2020-07-13 16:59:21 +08:00
namespace PaddleOCR {
struct OCRPredictResult {
std::vector<std::vector<int>> box;
std::string text;
float score = -1.0;
float cls_score;
int cls_label = -1;
};
2022-08-10 14:05:02 +08:00
struct StructurePredictResult {
2022-09-20 11:40:05 +08:00
std::vector<float> box;
2022-09-09 20:24:08 +08:00
std::vector<std::vector<int>> cell_box;
2022-08-10 14:05:02 +08:00
std::string type;
std::vector<OCRPredictResult> text_res;
std::string html;
float html_score = -1;
2022-09-19 17:37:41 +08:00
float confidence;
2022-08-10 14:05:02 +08:00
};
2020-07-13 16:59:21 +08:00
class Utility {
public:
static std::vector<std::string> ReadDict(const std::string &path) noexcept;
2021-08-11 21:09:57 +08:00
static void VisualizeBboxes(const cv::Mat &srcimg,
const std::vector<OCRPredictResult> &ocr_result,
const std::string &save_path) noexcept;
2020-07-13 17:15:54 +08:00
2022-09-09 20:24:08 +08:00
static void VisualizeBboxes(const cv::Mat &srcimg,
2022-09-20 11:40:05 +08:00
const StructurePredictResult &structure_result,
const std::string &save_path) noexcept;
2022-09-09 20:24:08 +08:00
2020-07-13 16:59:21 +08:00
template <class ForwardIterator>
inline static size_t argmax(ForwardIterator first,
ForwardIterator last) noexcept {
2020-07-13 16:59:21 +08:00
return std::distance(first, std::max_element(first, last));
}
2021-05-26 14:49:17 +08:00
static void GetAllFiles(const char *dir_name,
std::vector<std::string> &all_inputs) noexcept;
2022-04-03 16:56:16 +08:00
static cv::Mat
GetRotateCropImage(const cv::Mat &srcimage,
const std::vector<std::vector<int>> &box) noexcept;
2022-04-03 16:56:16 +08:00
static std::vector<int> argsort(const std::vector<float> &array) noexcept;
2021-11-03 15:20:22 +08:00
static std::string basename(const std::string &filename) noexcept;
static bool PathExists(const std::string &path) noexcept;
static void CreateDir(const std::string &path) noexcept;
2022-04-22 21:26:15 +08:00
static void
print_result(const std::vector<OCRPredictResult> &ocr_result) noexcept;
2022-08-10 14:05:02 +08:00
static cv::Mat crop_image(cv::Mat &img,
const std::vector<int> &area) noexcept;
static cv::Mat crop_image(cv::Mat &img,
const std::vector<float> &area) noexcept;
2022-08-10 14:05:02 +08:00
static void sorted_boxes(std::vector<OCRPredictResult> &ocr_result) noexcept;
2022-08-10 14:05:02 +08:00
static std::vector<int>
xyxyxyxy2xyxy(const std::vector<std::vector<int>> &box) noexcept;
static std::vector<int> xyxyxyxy2xyxy(const std::vector<int> &box) noexcept;
2022-09-09 20:24:08 +08:00
static float fast_exp(float x) noexcept;
2022-09-19 17:37:41 +08:00
static std::vector<float>
activation_function_softmax(std::vector<float> &src) noexcept;
static float iou(std::vector<int> &box1, std::vector<int> &box2) noexcept;
static float iou(std::vector<float> &box1, std::vector<float> &box2) noexcept;
2022-09-19 17:37:41 +08:00
2022-08-10 14:05:02 +08:00
private:
static bool comparison_box(const OCRPredictResult &result1,
const OCRPredictResult &result2) noexcept {
2022-08-10 14:05:02 +08:00
if (result1.box[0][1] < result2.box[0][1]) {
return true;
} else if (result1.box[0][1] == result2.box[0][1]) {
return result1.box[0][0] < result2.box[0][0];
} else {
return false;
}
}
2020-07-13 16:59:21 +08:00
};
} // namespace PaddleOCR