-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathppocr_v2.cc
executable file
·178 lines (155 loc) · 6.31 KB
/
ppocr_v2.cc
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
// Copyright (c) 2022 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 "fastdeploy/vision/ocr/ppocr/ppocr_v2.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h"
namespace fastdeploy {
namespace pipeline {
PPOCRv2::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Classifier* cls_model,
fastdeploy::vision::ocr::Recognizer* rec_model)
: detector_(det_model), classifier_(cls_model), recognizer_(rec_model) {
Initialized();
auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
preprocess_shape[1] = 32;
recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
}
PPOCRv2::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Recognizer* rec_model)
: detector_(det_model), recognizer_(rec_model) {
Initialized();
auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
preprocess_shape[1] = 32;
recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
}
bool PPOCRv2::SetClsBatchSize(int cls_batch_size) {
if (cls_batch_size < -1 || cls_batch_size == 0) {
FDERROR << "batch_size > 0 or batch_size == -1." << std::endl;
return false;
}
cls_batch_size_ = cls_batch_size;
return true;
}
int PPOCRv2::GetClsBatchSize() {
return cls_batch_size_;
}
bool PPOCRv2::SetRecBatchSize(int rec_batch_size) {
if (rec_batch_size < -1 || rec_batch_size == 0) {
FDERROR << "batch_size > 0 or batch_size == -1." << std::endl;
return false;
}
rec_batch_size_ = rec_batch_size;
return true;
}
int PPOCRv2::GetRecBatchSize() {
return rec_batch_size_;
}
bool PPOCRv2::Initialized() const {
if (detector_ != nullptr && !detector_->Initialized()) {
return false;
}
if (classifier_ != nullptr && !classifier_->Initialized()) {
return false;
}
if (recognizer_ != nullptr && !recognizer_->Initialized()) {
return false;
}
return true;
}
std::unique_ptr<PPOCRv2> PPOCRv2::Clone() const {
std::unique_ptr<PPOCRv2> clone_model = utils::make_unique<PPOCRv2>(PPOCRv2(*this));
clone_model->detector_ = detector_->Clone().release();
if (classifier_ != nullptr) {
clone_model->classifier_ = classifier_->Clone().release();
}
clone_model->recognizer_ = recognizer_->Clone().release();
return clone_model;
}
bool PPOCRv2::Predict(cv::Mat* img,
fastdeploy::vision::OCRResult* result) {
return Predict(*img, result);
}
bool PPOCRv2::Predict(const cv::Mat& img,
fastdeploy::vision::OCRResult* result) {
std::vector<fastdeploy::vision::OCRResult> batch_result(1);
bool success = BatchPredict({img},&batch_result);
if(!success){
return success;
}
*result = std::move(batch_result[0]);
return true;
};
bool PPOCRv2::BatchPredict(const std::vector<cv::Mat>& images,
std::vector<fastdeploy::vision::OCRResult>* batch_result) {
batch_result->clear();
batch_result->resize(images.size());
std::vector<std::vector<std::vector<std::array<int, 2>>>> batch_boxes(images.size());
if (!detector_->BatchPredict(images, &batch_boxes)) {
FDERROR << "There's error while detecting image in PPOCR." << std::endl;
return false;
}
for(int i_batch = 0; i_batch < batch_boxes.size(); ++i_batch) {
vision::ocr::SortBoxes(&(batch_boxes[i_batch]));
(*batch_result)[i_batch].boxes = batch_boxes[i_batch];
}
for(int i_batch = 0; i_batch < images.size(); ++i_batch) {
fastdeploy::vision::OCRResult& ocr_result = (*batch_result)[i_batch];
// Get croped images by detection result
const std::vector<std::vector<std::array<int, 2>>>& boxes = ocr_result.boxes;
const cv::Mat& img = images[i_batch];
std::vector<cv::Mat> image_list;
if (boxes.size() == 0) {
image_list.emplace_back(img);
}else{
image_list.resize(boxes.size());
for (size_t i_box = 0; i_box < boxes.size(); ++i_box) {
image_list[i_box] = vision::ocr::GetRotateCropImage(img, boxes[i_box]);
}
}
std::vector<int32_t>* cls_labels_ptr = &ocr_result.cls_labels;
std::vector<float>* cls_scores_ptr = &ocr_result.cls_scores;
std::vector<std::string>* text_ptr = &ocr_result.text;
std::vector<float>* rec_scores_ptr = &ocr_result.rec_scores;
if (nullptr != classifier_) {
for(size_t start_index = 0; start_index < image_list.size(); start_index+=cls_batch_size_) {
size_t end_index = std::min(start_index + cls_batch_size_, image_list.size());
if (!classifier_->BatchPredict(image_list, cls_labels_ptr, cls_scores_ptr, start_index, end_index)) {
FDERROR << "There's error while recognizing image in PPOCR." << std::endl;
return false;
}else{
for (size_t i_img = start_index; i_img < end_index; ++i_img) {
if(cls_labels_ptr->at(i_img) % 2 == 1 && cls_scores_ptr->at(i_img) > classifier_->GetPostprocessor().GetClsThresh()) {
cv::rotate(image_list[i_img], image_list[i_img], 1);
}
}
}
}
}
std::vector<float> width_list;
for (int i = 0; i < image_list.size(); i++) {
width_list.push_back(float(image_list[i].cols) / image_list[i].rows);
}
std::vector<int> indices = vision::ocr::ArgSort(width_list);
for(size_t start_index = 0; start_index < image_list.size(); start_index+=rec_batch_size_) {
size_t end_index = std::min(start_index + rec_batch_size_, image_list.size());
if (!recognizer_->BatchPredict(image_list, text_ptr, rec_scores_ptr, start_index, end_index, indices)) {
FDERROR << "There's error while recognizing image in PPOCR." << std::endl;
return false;
}
}
}
return true;
}
} // namesapce pipeline
} // namespace fastdeploy