2
2
3
3
#include < set>
4
4
5
- #include " mmdeploy/archive/json_archive.h"
6
5
#include " mmdeploy/archive/value_archive.h"
7
6
#include " mmdeploy/core/registry.h"
8
7
#include " mmdeploy/core/tensor.h"
9
- #include " mmdeploy/core/utils/device_utils.h"
10
8
#include " mmdeploy/core/utils/formatter.h"
11
- #include " mmdeploy/preprocess/transform/resize.h"
9
+ #include " mmdeploy/operation/managed.h"
10
+ #include " mmdeploy/operation/vision.h"
12
11
#include " mmdeploy/preprocess/transform/transform.h"
13
- #include " opencv2/imgproc.hpp"
14
- #include " opencv_utils.h"
15
12
16
13
using namespace std ;
17
14
18
- namespace mmdeploy {
15
+ namespace mmdeploy ::ocr {
19
16
20
- class RescaleToHeightImpl : public Module {
17
+ class RescaleToHeight : public transform ::Transform {
21
18
public:
22
- explicit RescaleToHeightImpl (const Value& args) noexcept {
19
+ explicit RescaleToHeight (const Value& args) noexcept {
23
20
height_ = args.value (" height" , height_);
24
21
min_width_ = args.contains (" min_width" ) && args[" min_width" ].is_number_integer ()
25
22
? args[" min_width" ].get <int >()
@@ -30,113 +27,59 @@ class RescaleToHeightImpl : public Module {
30
27
width_divisor_ = args.contains (" width_divisor" ) && args[" width_divisor" ].is_number_integer ()
31
28
? args[" width_divisor" ].get <int >()
32
29
: width_divisor_;
33
- resize_type_ = args.contains (" resize_type" ) && args[" resize_type" ].is_string ()
34
- ? args[" resize_type" ].get <string>()
35
- : resize_type_;
36
- stream_ = args[" context" ][" stream" ].get <Stream>();
30
+ resize_ = operation::Managed<operation::Resize>::Create (" bilinear" );
37
31
}
38
32
39
- ~RescaleToHeightImpl () override = default ;
33
+ ~RescaleToHeight () override = default ;
40
34
41
- Result<Value> Process ( const Value& input ) override {
42
- MMDEPLOY_DEBUG (" input: {}" , input );
35
+ Result<void > Apply ( Value& data ) override {
36
+ MMDEPLOY_DEBUG (" input: {}" , data );
43
37
auto dst_height = height_;
44
38
auto dst_min_width = min_width_;
45
39
auto dst_max_width = max_width_;
46
40
47
41
std::vector<int > img_shape; // NHWC
48
- from_value (input [" img_shape" ], img_shape);
42
+ from_value (data [" img_shape" ], img_shape);
49
43
50
44
std::vector<int > ori_shape; // NHWC
51
- from_value (input [" ori_shape" ], ori_shape);
45
+ from_value (data [" ori_shape" ], ori_shape);
52
46
53
47
auto ori_height = ori_shape[1 ];
54
48
auto ori_width = ori_shape[2 ];
55
49
auto valid_ratio = 1 .f ;
56
50
57
- Device host{" cpu" };
58
- auto _img = input[" img" ].get <Tensor>();
59
- OUTCOME_TRY (auto img, MakeAvailableOnDevice (_img, host, stream_));
60
- stream_.Wait ().value ();
51
+ auto img = data[" img" ].get <Tensor>();
61
52
Tensor img_resize;
62
53
auto new_width = static_cast <int >(std::ceil (1 .f * dst_height / ori_height * ori_width));
63
54
auto width_divisor = width_divisor_;
64
55
if (dst_min_width > 0 ) {
65
56
new_width = std::max (dst_min_width, new_width);
66
57
}
67
58
if (dst_max_width > 0 ) {
68
- auto resize_width = std::min (dst_max_width, new_width);
59
+ new_width = std::min (dst_max_width, new_width);
69
60
}
70
61
if (new_width % width_divisor != 0 ) {
71
62
new_width = std::round (1 .f * new_width / width_divisor) * width_divisor;
72
63
}
73
- img_resize = ResizeImage (img, dst_height, new_width);
74
- Value output = input;
75
- output[" img" ] = img_resize;
76
- output[" resize_shape" ] = to_value (img_resize.desc ().shape );
77
- output[" pad_shape" ] = output[" resize_shape" ];
78
- output[" ori_shape" ] = input[" ori_shape" ];
79
- output[" scale" ] = to_value (std::vector<int >({new_width, dst_height}));
80
- output[" valid_ratio" ] = valid_ratio;
81
- MMDEPLOY_DEBUG (" output: {}" , to_json (output).dump (2 ));
82
- return output;
83
- }
84
-
85
- Tensor ResizeImage (const Tensor& img, int dst_h, int dst_w) {
86
- TensorDesc desc = img.desc ();
87
- assert (desc.shape .size () == 4 );
88
- assert (desc.data_type == DataType::kINT8 );
89
- int h = desc.shape [1 ];
90
- int w = desc.shape [2 ];
91
- int c = desc.shape [3 ];
92
- assert (c == 3 || c == 1 );
93
- cv::Mat src_mat, dst_mat;
94
- if (3 == c) { // rgb
95
- src_mat = cv::Mat (h, w, CV_8UC3, const_cast <uint8_t *>(img.data <uint8_t >()));
96
- } else { // gray
97
- src_mat = cv::Mat (h, w, CV_8UC1, const_cast <uint8_t *>(img.data <uint8_t >()));
98
- }
99
- cv::Size size{dst_w, dst_h};
100
- cv::resize (src_mat, dst_mat, size, cv::INTER_LINEAR);
101
- return Tensor ({desc.device , desc.data_type , {1 , dst_h, dst_w, c}, " " },
102
- {dst_mat.data , [mat = dst_mat](void * ptr) {}});
64
+ OUTCOME_TRY (resize_.Apply (img, img_resize, dst_height, new_width));
65
+ data[" img" ] = img_resize;
66
+ data[" resize_shape" ] = to_value (img_resize.desc ().shape );
67
+ data[" pad_shape" ] = data[" resize_shape" ];
68
+ data[" ori_shape" ] = data[" ori_shape" ];
69
+ data[" scale" ] = to_value (std::vector<int >({new_width, dst_height}));
70
+ data[" valid_ratio" ] = valid_ratio;
71
+ MMDEPLOY_DEBUG (" output: {}" , data);
72
+ return success ();
103
73
}
104
74
105
75
protected:
76
+ operation::Managed<operation::Resize> resize_;
106
77
int height_{-1 };
107
78
int min_width_{-1 };
108
79
int max_width_{-1 };
109
80
bool keep_aspect_ratio_{true };
110
81
int width_divisor_{1 };
111
- std::string resize_type_{" Resize" };
112
- Stream stream_;
113
82
};
114
83
115
- MMDEPLOY_CREATOR_SIGNATURE (RescaleToHeightImpl,
116
- std::unique_ptr<RescaleToHeightImpl>(const Value& config));
117
-
118
- MMDEPLOY_DEFINE_REGISTRY (RescaleToHeightImpl);
119
-
120
- MMDEPLOY_REGISTER_FACTORY_FUNC (RescaleToHeightImpl, (cpu, 0 ), [](const Value& config) {
121
- return std::make_unique<RescaleToHeightImpl>(config);
122
- });
123
-
124
- class RescaleToHeight : public Transform {
125
- public:
126
- explicit RescaleToHeight (const Value& args) : Transform(args) {
127
- impl_ = Instantiate<RescaleToHeightImpl>(" RescaleToHeight" , args);
128
- }
129
- ~RescaleToHeight () override = default ;
130
-
131
- Result<Value> Process (const Value& input) override { return impl_->Process (input); }
132
-
133
- private:
134
- std::unique_ptr<RescaleToHeightImpl> impl_;
135
- static const std::string name_;
136
- };
137
-
138
- MMDEPLOY_REGISTER_FACTORY_FUNC (Transform, (ResizeOCR, 0 ), [](const Value& config) {
139
- return std::make_unique<RescaleToHeight>(config);
140
- });
141
-
142
- } // namespace mmdeploy
84
+ MMDEPLOY_REGISTER_TRANSFORM (RescaleToHeight);
85
+ } // namespace mmdeploy::ocr
0 commit comments