@@ -47,6 +47,9 @@ namespace mediapipe {
47
47
#endif // !MEDIAPIPE_DISABLE_GPU
48
48
49
49
namespace {
50
+ constexpr char kImageFrameTag [] = " IMAGE" ;
51
+ constexpr char kGpuBufferTag [] = " IMAGE_GPU" ;
52
+
50
53
int RotationModeToDegrees (mediapipe::RotationMode_Mode rotation) {
51
54
switch (rotation) {
52
55
case mediapipe::RotationMode_Mode_UNKNOWN:
@@ -95,7 +98,7 @@ mediapipe::ScaleMode_Mode ParseScaleMode(
95
98
// Scales, rotates, and flips images horizontally or vertically.
96
99
//
97
100
// Input:
98
- // One of the following two tags:
101
+ // One of the following tags:
99
102
// IMAGE: ImageFrame representing the input image.
100
103
// IMAGE_GPU: GpuBuffer representing the input image.
101
104
//
@@ -113,7 +116,7 @@ mediapipe::ScaleMode_Mode ParseScaleMode(
113
116
// corresponding field in the calculator options.
114
117
//
115
118
// Output:
116
- // One of the following two tags:
119
+ // One of the following tags:
117
120
// IMAGE - ImageFrame representing the output image.
118
121
// IMAGE_GPU - GpuBuffer representing the output image.
119
122
//
@@ -152,7 +155,8 @@ mediapipe::ScaleMode_Mode ParseScaleMode(
152
155
// Note: To enable horizontal or vertical flipping, specify them in the
153
156
// calculator options. Flipping is applied after rotation.
154
157
//
155
- // Note: Only scale mode STRETCH is currently supported on CPU.
158
+ // Note: Input defines output, so only matchig types supported:
159
+ // IMAGE -> IMAGE or IMAGE_GPU -> IMAGE_GPU
156
160
//
157
161
class ImageTransformationCalculator : public CalculatorBase {
158
162
public:
@@ -186,7 +190,7 @@ class ImageTransformationCalculator : public CalculatorBase {
186
190
187
191
bool use_gpu_ = false ;
188
192
#if !defined(MEDIAPIPE_DISABLE_GPU)
189
- GlCalculatorHelper helper_ ;
193
+ GlCalculatorHelper gpu_helper_ ;
190
194
std::unique_ptr<QuadRenderer> rgb_renderer_;
191
195
std::unique_ptr<QuadRenderer> yuv_renderer_;
192
196
std::unique_ptr<QuadRenderer> ext_rgb_renderer_;
@@ -197,21 +201,22 @@ REGISTER_CALCULATOR(ImageTransformationCalculator);
197
201
// static
198
202
::mediapipe::Status ImageTransformationCalculator::GetContract (
199
203
CalculatorContract* cc) {
200
- RET_CHECK (cc->Inputs ().HasTag (" IMAGE" ) ^ cc->Inputs ().HasTag (" IMAGE_GPU" ));
201
- RET_CHECK (cc->Outputs ().HasTag (" IMAGE" ) ^ cc->Outputs ().HasTag (" IMAGE_GPU" ));
204
+ // Only one input can be set, and the output type must match.
205
+ RET_CHECK (cc->Inputs ().HasTag (kImageFrameTag ) ^
206
+ cc->Inputs ().HasTag (kGpuBufferTag ));
202
207
203
208
bool use_gpu = false ;
204
209
205
- if (cc->Inputs ().HasTag (" IMAGE " )) {
206
- RET_CHECK (cc->Outputs ().HasTag (" IMAGE " ));
207
- cc->Inputs ().Tag (" IMAGE " ).Set <ImageFrame>();
208
- cc->Outputs ().Tag (" IMAGE " ).Set <ImageFrame>();
210
+ if (cc->Inputs ().HasTag (kImageFrameTag )) {
211
+ RET_CHECK (cc->Outputs ().HasTag (kImageFrameTag ));
212
+ cc->Inputs ().Tag (kImageFrameTag ).Set <ImageFrame>();
213
+ cc->Outputs ().Tag (kImageFrameTag ).Set <ImageFrame>();
209
214
}
210
215
#if !defined(MEDIAPIPE_DISABLE_GPU)
211
- if (cc->Inputs ().HasTag (" IMAGE_GPU " )) {
212
- RET_CHECK (cc->Outputs ().HasTag (" IMAGE_GPU " ));
213
- cc->Inputs ().Tag (" IMAGE_GPU " ).Set <GpuBuffer>();
214
- cc->Outputs ().Tag (" IMAGE_GPU " ).Set <GpuBuffer>();
216
+ if (cc->Inputs ().HasTag (kGpuBufferTag )) {
217
+ RET_CHECK (cc->Outputs ().HasTag (kGpuBufferTag ));
218
+ cc->Inputs ().Tag (kGpuBufferTag ).Set <GpuBuffer>();
219
+ cc->Outputs ().Tag (kGpuBufferTag ).Set <GpuBuffer>();
215
220
use_gpu |= true ;
216
221
}
217
222
#endif // !MEDIAPIPE_DISABLE_GPU
@@ -259,7 +264,7 @@ ::mediapipe::Status ImageTransformationCalculator::Open(CalculatorContext* cc) {
259
264
260
265
options_ = cc->Options <ImageTransformationCalculatorOptions>();
261
266
262
- if (cc->Inputs ().HasTag (" IMAGE_GPU " )) {
267
+ if (cc->Inputs ().HasTag (kGpuBufferTag )) {
263
268
use_gpu_ = true ;
264
269
}
265
270
@@ -300,7 +305,7 @@ ::mediapipe::Status ImageTransformationCalculator::Open(CalculatorContext* cc) {
300
305
if (use_gpu_) {
301
306
#if !defined(MEDIAPIPE_DISABLE_GPU)
302
307
// Let the helper access the GL context information.
303
- MP_RETURN_IF_ERROR (helper_ .Open (cc));
308
+ MP_RETURN_IF_ERROR (gpu_helper_ .Open (cc));
304
309
#else
305
310
RET_CHECK_FAIL () << " GPU processing not enabled." ;
306
311
#endif // !MEDIAPIPE_DISABLE_GPU
@@ -328,18 +333,14 @@ ::mediapipe::Status ImageTransformationCalculator::Process(
328
333
329
334
if (use_gpu_) {
330
335
#if !defined(MEDIAPIPE_DISABLE_GPU)
331
- if (cc->Inputs ().Tag (" IMAGE_GPU" ).IsEmpty ()) {
332
- // Image is missing, hence no way to produce output image. (Timestamp
333
- // bound will be updated automatically.)
336
+ if (cc->Inputs ().Tag (kGpuBufferTag ).IsEmpty ()) {
334
337
return ::mediapipe::OkStatus ();
335
338
}
336
- return helper_ .RunInGlContext (
339
+ return gpu_helper_ .RunInGlContext (
337
340
[this , cc]() -> ::mediapipe::Status { return RenderGpu (cc); });
338
341
#endif // !MEDIAPIPE_DISABLE_GPU
339
342
} else {
340
- if (cc->Inputs ().Tag (" IMAGE" ).IsEmpty ()) {
341
- // Image is missing, hence no way to produce output image. (Timestamp
342
- // bound will be updated automatically.)
343
+ if (cc->Inputs ().Tag (kImageFrameTag ).IsEmpty ()) {
343
344
return ::mediapipe::OkStatus ();
344
345
}
345
346
return RenderCpu (cc);
@@ -354,7 +355,7 @@ ::mediapipe::Status ImageTransformationCalculator::Close(
354
355
QuadRenderer* rgb_renderer = rgb_renderer_.release ();
355
356
QuadRenderer* yuv_renderer = yuv_renderer_.release ();
356
357
QuadRenderer* ext_rgb_renderer = ext_rgb_renderer_.release ();
357
- helper_ .RunInGlContext ([rgb_renderer, yuv_renderer, ext_rgb_renderer] {
358
+ gpu_helper_ .RunInGlContext ([rgb_renderer, yuv_renderer, ext_rgb_renderer] {
358
359
if (rgb_renderer) {
359
360
rgb_renderer->GlTeardown ();
360
361
delete rgb_renderer;
@@ -376,17 +377,21 @@ ::mediapipe::Status ImageTransformationCalculator::Close(
376
377
377
378
::mediapipe::Status ImageTransformationCalculator::RenderCpu (
378
379
CalculatorContext* cc) {
379
- const auto & input_img = cc->Inputs ().Tag (" IMAGE" ).Get <ImageFrame>();
380
- cv::Mat input_mat = formats::MatView (&input_img);
381
- cv::Mat scaled_mat;
380
+ cv::Mat input_mat;
381
+ mediapipe::ImageFormat::Format format;
382
382
383
- const int input_width = input_img.Width ();
384
- const int input_height = input_img.Height ();
383
+ const auto & input = cc->Inputs ().Tag (kImageFrameTag ).Get <ImageFrame>();
384
+ input_mat = formats::MatView (&input);
385
+ format = input.Format ();
386
+
387
+ const int input_width = input_mat.cols ;
388
+ const int input_height = input_mat.rows ;
385
389
if (!output_height_ || !output_width_) {
386
390
output_height_ = input_height;
387
391
output_width_ = input_width;
388
392
}
389
393
394
+ cv::Mat scaled_mat;
390
395
if (scale_mode_ == mediapipe::ScaleMode_Mode_STRETCH) {
391
396
cv::resize (input_mat, scaled_mat, cv::Size (output_width_, output_height_));
392
397
} else {
@@ -443,18 +448,20 @@ ::mediapipe::Status ImageTransformationCalculator::RenderCpu(
443
448
}
444
449
445
450
std::unique_ptr<ImageFrame> output_frame (
446
- new ImageFrame (input_img. Format () , output_width, output_height));
451
+ new ImageFrame (format , output_width, output_height));
447
452
cv::Mat output_mat = formats::MatView (output_frame.get ());
448
453
flipped_mat.copyTo (output_mat);
449
- cc->Outputs ().Tag (" IMAGE" ).Add (output_frame.release (), cc->InputTimestamp ());
454
+ cc->Outputs ()
455
+ .Tag (kImageFrameTag )
456
+ .Add (output_frame.release (), cc->InputTimestamp ());
450
457
451
458
return ::mediapipe::OkStatus ();
452
459
}
453
460
454
461
::mediapipe::Status ImageTransformationCalculator::RenderGpu (
455
462
CalculatorContext* cc) {
456
463
#if !defined(MEDIAPIPE_DISABLE_GPU)
457
- const auto & input = cc->Inputs ().Tag (" IMAGE_GPU " ).Get <GpuBuffer>();
464
+ const auto & input = cc->Inputs ().Tag (kGpuBufferTag ).Get <GpuBuffer>();
458
465
const int input_width = input.width ();
459
466
const int input_height = input.height ();
460
467
@@ -485,11 +492,11 @@ ::mediapipe::Status ImageTransformationCalculator::RenderGpu(
485
492
{" video_frame_y" , " video_frame_uv" }));
486
493
}
487
494
renderer = yuv_renderer_.get ();
488
- src1 = helper_ .CreateSourceTexture (input, 0 );
495
+ src1 = gpu_helper_ .CreateSourceTexture (input, 0 );
489
496
} else // NOLINT(readability/braces)
490
497
#endif // iOS
491
498
{
492
- src1 = helper_ .CreateSourceTexture (input);
499
+ src1 = gpu_helper_ .CreateSourceTexture (input);
493
500
#if defined(TEXTURE_EXTERNAL_OES)
494
501
if (src1.target () == GL_TEXTURE_EXTERNAL_OES) {
495
502
if (!ext_rgb_renderer_) {
@@ -515,10 +522,10 @@ ::mediapipe::Status ImageTransformationCalculator::RenderGpu(
515
522
mediapipe::FrameRotation rotation =
516
523
mediapipe::FrameRotationFromDegrees (RotationModeToDegrees (rotation_));
517
524
518
- auto dst = helper_ .CreateDestinationTexture (output_width, output_height,
519
- input.format ());
525
+ auto dst = gpu_helper_ .CreateDestinationTexture (output_width, output_height,
526
+ input.format ());
520
527
521
- helper_ .BindFramebuffer (dst); // GL_TEXTURE0
528
+ gpu_helper_ .BindFramebuffer (dst); // GL_TEXTURE0
522
529
glActiveTexture (GL_TEXTURE1);
523
530
glBindTexture (src1.target (), src1.name ());
524
531
@@ -533,8 +540,8 @@ ::mediapipe::Status ImageTransformationCalculator::RenderGpu(
533
540
// Execute GL commands, before getting result.
534
541
glFlush ();
535
542
536
- auto output = dst.GetFrame <GpuBuffer>();
537
- cc->Outputs ().Tag (" IMAGE_GPU " ).Add (output.release (), cc->InputTimestamp ());
543
+ auto output = dst.template GetFrame <GpuBuffer>();
544
+ cc->Outputs ().Tag (kGpuBufferTag ).Add (output.release (), cc->InputTimestamp ());
538
545
539
546
#endif // !MEDIAPIPE_DISABLE_GPU
540
547
0 commit comments