Skip to content

Commit cadc265

Browse files
authored
Fix WarpBbox and memory leak in TextRecognizer (#1745)
1 parent 0f5b149 commit cadc265

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

csrc/mmdeploy/apis/cxx/mmdeploy/text_recognizer.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#ifndef MMDEPLOY_CSRC_MMDEPLOY_APIS_CXX_TEXT_RECOGNIZER_HPP_
44
#define MMDEPLOY_CSRC_MMDEPLOY_APIS_CXX_TEXT_RECOGNIZER_HPP_
55

6+
#include <numeric>
7+
68
#include "mmdeploy/common.hpp"
79
#include "mmdeploy/text_detector.hpp"
810
#include "mmdeploy/text_recognizer.h"
@@ -40,9 +42,12 @@ class TextRecognizer : public NonMovable {
4042
const TextDetection* p_bboxes{};
4143
const int* p_bbox_count{};
4244

45+
auto n_total_bboxes = static_cast<int>(images.size());
46+
4347
if (!bboxes.empty()) {
4448
p_bboxes = bboxes.data();
4549
p_bbox_count = bbox_count.data();
50+
n_total_bboxes = std::accumulate(bbox_count.begin(), bbox_count.end(), 0);
4651
}
4752

4853
TextRecognition* results{};
@@ -53,7 +58,7 @@ class TextRecognizer : public NonMovable {
5358
throw_exception(static_cast<ErrorCode>(ec));
5459
}
5560

56-
std::shared_ptr<TextRecognition> data(results, [count = images.size()](auto p) {
61+
std::shared_ptr<TextRecognition> data(results, [count = n_total_bboxes](auto p) {
5762
mmdeploy_text_recognizer_release_result(p, count);
5863
});
5964

csrc/mmdeploy/codebase/mmocr/warp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class WarpBbox {
2121
if (det.is_object() && det.contains("bbox")) {
2222
auto bbox = from_value<std::vector<cv::Point>>(det["bbox"]);
2323
auto patch = warp(mmdeploy::cpu::Mat2CVMat(ori_img), bbox);
24-
return Value{{"ori_img", cpu::CVMat2Mat(patch, PixelFormat::kBGR)}};
24+
return Value{{"ori_img", cpu::CVMat2Mat(patch, ori_img.pixel_format())}};
2525
} else { // whole image as a bbox
2626
return Value{{"ori_img", ori_img}};
2727
}

csrc/mmdeploy/utils/opencv/opencv_utils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ Mat CVMat2Mat(const cv::Mat& mat, PixelFormat format) {
3838
}
3939

4040
cv::Mat Mat2CVMat(const Mat& mat) {
41-
std::map<DataType, int> type_mapper{{DataType::kFLOAT, CV_32F},
42-
{DataType::kHALF, CV_16U},
43-
{DataType::kINT8, CV_8U},
44-
{DataType::kINT32, CV_32S}};
45-
auto type = CV_MAKETYPE(type_mapper[mat.type()], mat.channel());
41+
static const std::map<DataType, int> type_mapper{{DataType::kFLOAT, CV_32F},
42+
{DataType::kHALF, CV_16U},
43+
{DataType::kINT8, CV_8U},
44+
{DataType::kINT32, CV_32S}};
45+
auto type = CV_MAKETYPE(type_mapper.at(mat.type()), mat.channel());
4646
auto format = mat.pixel_format();
4747
if (PixelFormat::kBGR == format || PixelFormat::kRGB == format) {
4848
return cv::Mat(mat.height(), mat.width(), type, mat.data<void>());

demo/csrc/cpp/utils/visualize.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ class Visualize {
6464
cv::Rect rect(origin + cv::Point2f(0, text_size.height + 2 * thickness),
6565
origin + cv::Point2f(text_size.width, 0));
6666
rect &= cv::Rect({}, img_.size());
67-
img_(rect) *= .35f;
68-
cv::putText(img_, text, origin + cv::Point2f(0, text_size.height), font_face, font_scale,
69-
cv::Scalar::all(255), thickness, cv::LINE_AA);
67+
if (rect.area() > 0) {
68+
img_(rect) *= .35f;
69+
cv::putText(img_, text, origin + cv::Point2f(0, text_size.height), font_face, font_scale,
70+
cv::Scalar::all(255), thickness, cv::LINE_AA);
71+
}
7072
return text_size.height;
7173
}
7274

@@ -120,11 +122,11 @@ class Visualize {
120122

121123
void add_text_det(mmdeploy_point_t bbox[4], float score, const char* text, size_t text_size,
122124
int index) {
123-
printf("bbox[%d]: (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)\n", index, //
124-
bbox[0].x, bbox[0].y, //
125-
bbox[1].x, bbox[1].y, //
126-
bbox[2].x, bbox[2].y, //
127-
bbox[3].x, bbox[3].y);
125+
printf("bbox[%d]: (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), %.2f\n", index, //
126+
bbox[0].x, bbox[0].y, //
127+
bbox[1].x, bbox[1].y, //
128+
bbox[2].x, bbox[2].y, //
129+
bbox[3].x, bbox[3].y, score);
128130
std::vector<cv::Point> poly_points;
129131
cv::Point2f center{};
130132
for (int i = 0; i < 4; ++i) {

0 commit comments

Comments
 (0)