diff --git a/fastdeploy/vision/ocr/ppocr/cls_postprocessor.cc b/fastdeploy/vision/ocr/ppocr/cls_postprocessor.cc index c19bcc3a6..57c60a396 100644 --- a/fastdeploy/vision/ocr/ppocr/cls_postprocessor.cc +++ b/fastdeploy/vision/ocr/ppocr/cls_postprocessor.cc @@ -20,13 +20,13 @@ namespace fastdeploy { namespace vision { namespace ocr { -bool SingleBatchPostprocessor(const float* out_data, const size_t& length, int* cls_label, float* cls_score) { +bool SingleBatchPostprocessor(const float* out_data, const size_t& length, + int* cls_label, float* cls_score) { - *cls_label = std::distance( - &out_data[0], std::max_element(&out_data[0], &out_data[length])); + *cls_label = std::distance(&out_data[0], + std::max_element(&out_data[0], &out_data[length])); - *cls_score = - float(*std::max_element(&out_data[0], &out_data[length])); + *cls_score = float(*std::max_element(&out_data[0], &out_data[length])); return true; } @@ -46,18 +46,24 @@ bool ClassifierPostprocessor::Run(const std::vector& tensors, // For Classifier, the output tensor shape = [batch,2] size_t batch = tensor.shape[0]; - size_t length = accumulate(tensor.shape.begin()+1, tensor.shape.end(), 1, std::multiplies()); + size_t length = accumulate(tensor.shape.begin() + 1, tensor.shape.end(), 1, + std::multiplies()); if (batch <= 0) { - FDERROR << "The infer outputTensor.shape[0] <=0, wrong infer result." << std::endl; + FDERROR << "The infer outputTensor.shape[0] <=0, wrong infer result." + << std::endl; return false; } if (start_index < 0 || total_size <= 0) { - FDERROR << "start_index or total_size error. Correct is: 0 <= start_index < total_size" << std::endl; + FDERROR << "start_index or total_size error. Correct is: 0 <= start_index " + "< total_size" + << std::endl; return false; } if ((start_index + batch) > total_size) { - FDERROR << "start_index or total_size error. Correct is: start_index + batch(outputTensor.shape[0]) <= total_size" << std::endl; + FDERROR << "start_index or total_size error. Correct is: start_index + " + "batch(outputTensor.shape[0]) <= total_size" + << std::endl; return false; } @@ -65,17 +71,15 @@ bool ClassifierPostprocessor::Run(const std::vector& tensors, cls_scores->resize(total_size); const float* tensor_data = reinterpret_cast(tensor.Data()); for (int i_batch = 0; i_batch < batch; ++i_batch) { - if(!SingleBatchPostprocessor(tensor_data+ i_batch * length, + SingleBatchPostprocessor(tensor_data+ i_batch * length, length, &cls_labels->at(i_batch + start_index), - &cls_scores->at(i_batch + start_index))) { - return false; - } + &cls_scores->at(i_batch + start_index)); } return true; } -} // namespace classification +} // namespace ocr } // namespace vision } // namespace fastdeploy diff --git a/fastdeploy/vision/ocr/ppocr/cls_postprocessor.h b/fastdeploy/vision/ocr/ppocr/cls_postprocessor.h index e596db71d..efcc32968 100644 --- a/fastdeploy/vision/ocr/ppocr/cls_postprocessor.h +++ b/fastdeploy/vision/ocr/ppocr/cls_postprocessor.h @@ -33,11 +33,11 @@ class FASTDEPLOY_DECL ClassifierPostprocessor { * \return true if the postprocess successed, otherwise false */ bool Run(const std::vector& tensors, - std::vector* cls_labels, std::vector* cls_scores); + std::vector* cls_labels, std::vector* cls_scores); bool Run(const std::vector& tensors, - std::vector* cls_labels, std::vector* cls_scores, - size_t start_index, size_t total_size); + std::vector* cls_labels, std::vector* cls_scores, + size_t start_index, size_t total_size); /// Set threshold for the classification postprocess, default is 0.9 void SetClsThresh(float cls_thresh) { cls_thresh_ = cls_thresh; } @@ -45,6 +45,7 @@ class FASTDEPLOY_DECL ClassifierPostprocessor { /// Get threshold value of the classification postprocess. float GetClsThresh() const { return cls_thresh_; } + private: float cls_thresh_ = 0.9; }; diff --git a/fastdeploy/vision/ocr/ppocr/cls_preprocessor.h b/fastdeploy/vision/ocr/ppocr/cls_preprocessor.h index 8d42f3d31..52b2bb737 100644 --- a/fastdeploy/vision/ocr/ppocr/cls_preprocessor.h +++ b/fastdeploy/vision/ocr/ppocr/cls_preprocessor.h @@ -35,12 +35,12 @@ class FASTDEPLOY_DECL ClassifierPreprocessor { size_t start_index, size_t end_index); /// Set mean value for the image normalization in classification preprocess - void SetMean(std::vector mean) { mean_ = mean; } + void SetMean(const std::vector& mean) { mean_ = mean; } /// Get mean value of the image normalization in classification preprocess std::vector GetMean() const { return mean_; } /// Set scale value for the image normalization in classification preprocess - void SetScale(std::vector scale) { scale_ = scale; } + void SetScale(const std::vector& scale) { scale_ = scale; } /// Get scale value of the image normalization in classification preprocess std::vector GetScale() const { return scale_; } @@ -50,11 +50,13 @@ class FASTDEPLOY_DECL ClassifierPreprocessor { bool GetIsScale() const { return is_scale_; } /// Set cls_image_shape for the classification preprocess - void SetClsImageShape(std::vector cls_image_shape) - { cls_image_shape_ = cls_image_shape; } + void SetClsImageShape(const std::vector& cls_image_shape) { + cls_image_shape_ = cls_image_shape; + } /// Get cls_image_shape for the classification preprocess std::vector GetClsImageShape() const { return cls_image_shape_; } + private: std::vector mean_ = {0.5f, 0.5f, 0.5f}; std::vector scale_ = {0.5f, 0.5f, 0.5f}; bool is_scale_ = true; diff --git a/fastdeploy/vision/ocr/ppocr/det_postprocessor.cc b/fastdeploy/vision/ocr/ppocr/det_postprocessor.cc index e83dac5e5..428142fd2 100644 --- a/fastdeploy/vision/ocr/ppocr/det_postprocessor.cc +++ b/fastdeploy/vision/ocr/ppocr/det_postprocessor.cc @@ -21,14 +21,11 @@ namespace vision { namespace ocr { bool DBDetectorPostprocessor::SingleBatchPostprocessor( - const float* out_data, - int n2, - int n3, - const std::array& det_img_info, - std::vector>* boxes_result - ) { + const float* out_data, int n2, int n3, + const std::array& det_img_info, + std::vector>* boxes_result) { int n = n2 * n3; - + // prepare bitmap std::vector pred(n, 0.0); std::vector cbuf(n, ' '); @@ -52,9 +49,9 @@ bool DBDetectorPostprocessor::SingleBatchPostprocessor( std::vector>> boxes; - boxes = - util_post_processor_.BoxesFromBitmap(pred_map, bit_map, det_db_box_thresh_, - det_db_unclip_ratio_, det_db_score_mode_); + boxes = util_post_processor_.BoxesFromBitmap( + pred_map, bit_map, det_db_box_thresh_, det_db_unclip_ratio_, + det_db_score_mode_); boxes = util_post_processor_.FilterTagDetRes(boxes, det_img_info); @@ -67,31 +64,30 @@ bool DBDetectorPostprocessor::SingleBatchPostprocessor( new_box[k++] = e; } } - boxes_result->push_back(new_box); + boxes_result->emplace_back(new_box); } return true; } -bool DBDetectorPostprocessor::Run(const std::vector& tensors, - std::vector>>* results, - const std::vector>& batch_det_img_info) { +bool DBDetectorPostprocessor::Run( + const std::vector& tensors, + std::vector>>* results, + const std::vector>& batch_det_img_info) { // DBDetector have only 1 output tensor. const FDTensor& tensor = tensors[0]; // For DBDetector, the output tensor shape = [batch, 1, ?, ?] size_t batch = tensor.shape[0]; - size_t length = accumulate(tensor.shape.begin()+1, tensor.shape.end(), 1, std::multiplies()); + size_t length = accumulate(tensor.shape.begin() + 1, tensor.shape.end(), 1, + std::multiplies()); const float* tensor_data = reinterpret_cast(tensor.Data()); - + results->resize(batch); for (int i_batch = 0; i_batch < batch; ++i_batch) { - if(!SingleBatchPostprocessor(tensor_data, - tensor.shape[2], - tensor.shape[3], - batch_det_img_info[i_batch], - &results->at(i_batch) - ))return false; + SingleBatchPostprocessor(tensor_data, tensor.shape[2], tensor.shape[3], + batch_det_img_info[i_batch], + &results->at(i_batch)); tensor_data = tensor_data + length; } return true; diff --git a/fastdeploy/vision/ocr/ppocr/det_postprocessor.h b/fastdeploy/vision/ocr/ppocr/det_postprocessor.h index 129ca6258..fc0d8c84d 100644 --- a/fastdeploy/vision/ocr/ppocr/det_postprocessor.h +++ b/fastdeploy/vision/ocr/ppocr/det_postprocessor.h @@ -42,20 +42,23 @@ class FASTDEPLOY_DECL DBDetectorPostprocessor { double GetDetDBThresh() const { return det_db_thresh_; } /// Set det_db_box_thresh for the detection postprocess, default is 0.6 - void SetDetDBBoxThresh(double det_db_box_thresh) - { det_db_box_thresh_ = det_db_box_thresh; } + void SetDetDBBoxThresh(double det_db_box_thresh) { + det_db_box_thresh_ = det_db_box_thresh; + } /// Get det_db_box_thresh of the detection postprocess double GetDetDBBoxThresh() const { return det_db_box_thresh_; } /// Set det_db_unclip_ratio for the detection postprocess, default is 1.5 - void SetDetDBUnclipRatio(double det_db_unclip_ratio) - { det_db_unclip_ratio_ = det_db_unclip_ratio; } + void SetDetDBUnclipRatio(double det_db_unclip_ratio) { + det_db_unclip_ratio_ = det_db_unclip_ratio; + } /// Get det_db_unclip_ratio_ of the detection postprocess double GetDetDBUnclipRatio() const { return det_db_unclip_ratio_; } /// Set det_db_score_mode for the detection postprocess, default is 'slow' - void SetDetDBScoreMode(std::string det_db_score_mode) - { det_db_score_mode_ = det_db_score_mode; } + void SetDetDBScoreMode(const std::string& det_db_score_mode) { + det_db_score_mode_ = det_db_score_mode; + } /// Get det_db_score_mode_ of the detection postprocess std::string GetDetDBScoreMode() const { return det_db_score_mode_; } @@ -64,17 +67,15 @@ class FASTDEPLOY_DECL DBDetectorPostprocessor { /// Get use_dilation of the detection postprocess int GetUseDilation() const { return use_dilation_; } + + private: double det_db_thresh_ = 0.3; double det_db_box_thresh_ = 0.6; double det_db_unclip_ratio_ = 1.5; std::string det_db_score_mode_ = "slow"; bool use_dilation_ = false; - - private: PostProcessor util_post_processor_; - bool SingleBatchPostprocessor(const float* out_data, - int n2, - int n3, + bool SingleBatchPostprocessor(const float* out_data, int n2, int n3, const std::array& det_img_info, std::vector>* boxes_result); }; diff --git a/fastdeploy/vision/ocr/ppocr/det_preprocessor.h b/fastdeploy/vision/ocr/ppocr/det_preprocessor.h index bf496079f..552d0628a 100644 --- a/fastdeploy/vision/ocr/ppocr/det_preprocessor.h +++ b/fastdeploy/vision/ocr/ppocr/det_preprocessor.h @@ -31,8 +31,7 @@ class FASTDEPLOY_DECL DBDetectorPreprocessor { * \param[in] batch_det_img_info_ptr The output of preprocess * \return true if the preprocess successed, otherwise false */ - bool Run(std::vector* images, - std::vector* outputs, + bool Run(std::vector* images, std::vector* outputs, std::vector>* batch_det_img_info_ptr); /// Set max_side_len for the detection preprocess, default is 960 @@ -41,12 +40,12 @@ class FASTDEPLOY_DECL DBDetectorPreprocessor { int GetMaxSideLen() const { return max_side_len_; } /// Set mean value for the image normalization in detection preprocess - void SetMean(std::vector mean) { mean_ = mean; } + void SetMean(const std::vector& mean) { mean_ = mean; } /// Get mean value of the image normalization in detection preprocess std::vector GetMean() const { return mean_; } /// Set scale value for the image normalization in detection preprocess - void SetScale(std::vector scale) { scale_ = scale; } + void SetScale(const std::vector& scale) { scale_ = scale; } /// Get scale value of the image normalization in detection preprocess std::vector GetScale() const { return scale_; } @@ -55,6 +54,7 @@ class FASTDEPLOY_DECL DBDetectorPreprocessor { /// Get is_scale of the image normalization in detection preprocess bool GetIsScale() const { return is_scale_; } + private: int max_side_len_ = 960; std::vector mean_ = {0.485f, 0.456f, 0.406f}; std::vector scale_ = {0.229f, 0.224f, 0.225f}; diff --git a/fastdeploy/vision/ocr/ppocr/ppocr_v2.cc b/fastdeploy/vision/ocr/ppocr/ppocr_v2.cc index 74894ba06..b4ae7e25f 100755 --- a/fastdeploy/vision/ocr/ppocr/ppocr_v2.cc +++ b/fastdeploy/vision/ocr/ppocr/ppocr_v2.cc @@ -23,14 +23,18 @@ PPOCRv2::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model, fastdeploy::vision::ocr::Recognizer* rec_model) : detector_(det_model), classifier_(cls_model), recognizer_(rec_model) { Initialized(); - recognizer_->GetPreprocessor().rec_image_shape_[1] = 32; + 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(); - recognizer_->GetPreprocessor().rec_image_shape_[1] = 32; + auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape(); + preprocess_shape[1] = 32; + recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape); } bool PPOCRv2::SetClsBatchSize(int cls_batch_size) { @@ -145,7 +149,7 @@ bool PPOCRv2::BatchPredict(const std::vector& images, 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().cls_thresh_) { + 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); } } diff --git a/fastdeploy/vision/ocr/ppocr/ppocr_v3.h b/fastdeploy/vision/ocr/ppocr/ppocr_v3.h index 8fcdb5646..d51ebdca4 100755 --- a/fastdeploy/vision/ocr/ppocr/ppocr_v3.h +++ b/fastdeploy/vision/ocr/ppocr/ppocr_v3.h @@ -36,7 +36,9 @@ class FASTDEPLOY_DECL PPOCRv3 : public PPOCRv2 { fastdeploy::vision::ocr::Recognizer* rec_model) : PPOCRv2(det_model, cls_model, rec_model) { // The only difference between v2 and v3 - recognizer_->GetPreprocessor().rec_image_shape_[1] = 48; + auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape(); + preprocess_shape[1] = 48; + recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape); } /** \brief Classification model is optional, so this function is set up the detection model path and recognition model path respectively. * @@ -47,7 +49,9 @@ class FASTDEPLOY_DECL PPOCRv3 : public PPOCRv2 { fastdeploy::vision::ocr::Recognizer* rec_model) : PPOCRv2(det_model, rec_model) { // The only difference between v2 and v3 - recognizer_->GetPreprocessor().rec_image_shape_[1] = 48; + auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape(); + preprocess_shape[1] = 48; + recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape); } /** \brief Clone a new PPOCRv3 with less memory usage when multiple instances of the same model are created diff --git a/fastdeploy/vision/ocr/ppocr/rec_preprocessor.h b/fastdeploy/vision/ocr/ppocr/rec_preprocessor.h index c50711588..f7d741b5d 100644 --- a/fastdeploy/vision/ocr/ppocr/rec_preprocessor.h +++ b/fastdeploy/vision/ocr/ppocr/rec_preprocessor.h @@ -38,18 +38,19 @@ class FASTDEPLOY_DECL RecognizerPreprocessor { /// Set static_shape_infer is true or not. When deploy PP-OCR /// on hardware which can not support dynamic input shape very well, /// like Huawei Ascned, static_shape_infer needs to to be true. - void SetStaticShapeInfer(bool static_shape_infer) - { static_shape_infer_ = static_shape_infer; } + void SetStaticShapeInfer(bool static_shape_infer) { + static_shape_infer_ = static_shape_infer; + } /// Get static_shape_infer of the recognition preprocess bool GetStaticShapeInfer() const { return static_shape_infer_; } /// Set mean value for the image normalization in recognition preprocess - void SetMean(std::vector mean) { mean_ = mean; } + void SetMean(const std::vector& mean) { mean_ = mean; } /// Get mean value of the image normalization in recognition preprocess std::vector GetMean() const { return mean_; } /// Set scale value for the image normalization in recognition preprocess - void SetScale(std::vector scale) { scale_ = scale; } + void SetScale(const std::vector& scale) { scale_ = scale; } /// Get scale value of the image normalization in recognition preprocess std::vector GetScale() const { return scale_; } @@ -59,11 +60,13 @@ class FASTDEPLOY_DECL RecognizerPreprocessor { bool GetIsScale() const { return is_scale_; } /// Set rec_image_shape for the recognition preprocess - void SetRecImageShape(std::vector rec_image_shape) - { rec_image_shape_ = rec_image_shape; } + void SetRecImageShape(const std::vector& rec_image_shape) { + rec_image_shape_ = rec_image_shape; + } /// Get rec_image_shape for the recognition preprocess - std::vector GetRecImageShape() const { return rec_image_shape_; } + std::vector GetRecImageShape() { return rec_image_shape_; } + private: std::vector rec_image_shape_ = {3, 48, 320}; std::vector mean_ = {0.5f, 0.5f, 0.5f}; std::vector scale_ = {0.5f, 0.5f, 0.5f};