Skip to content

Commit 8a1274d

Browse files
committed
1. Ditch std::unique_ptr, use FrameBuffer directly.
2. Use old Postprocess API
1 parent ffb1b07 commit 8a1274d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

tensorflow_lite_support/cc/task/vision/image_transformer.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ StatusOr<FrameBuffer> ImageTransformer::Transform(
152152
return InferWithFallback(frame_buffer, roi);
153153
}
154154

155-
StatusOr<std::unique_ptr<FrameBuffer>> ImageTransformer::Postprocess() {
156-
std::unique_ptr<FrameBuffer> postprocessed_frame_buffer;
155+
StatusOr<FrameBuffer> ImageTransformer::Postprocess(
156+
const std::vector<const TfLiteTensor*>& /*output_tensors*/,
157+
const FrameBuffer& /*frame_buffer*/, const BoundingBox& /*roi*/) {
157158
const int kRgbPixelBytes = 3;
158159
const TfLiteTensor* output_tensor =
159160
TfLiteEngine::GetOutput(GetTfLiteEngine()->interpreter(), 0);
@@ -171,9 +172,9 @@ StatusOr<std::unique_ptr<FrameBuffer>> ImageTransformer::Postprocess() {
171172
"Size mismatch or unsupported padding bytes between pixel data "
172173
"and output tensor.");
173174
}
174-
175-
postprocessed_data.insert(postprocessed_data.end(), &output_tensor[0],
176-
&output_tensor[output_byte_size / sizeof(uint8)]);
175+
const uint8* output_data = AssertAndReturnTypedTensor<uint8>(output_tensor);
176+
postprocessed_data.insert(postprocessed_data.end(), &output_data[0],
177+
&output_data[output_byte_size / sizeof(uint8)]);
177178
} else { // Denormalize to [0, 255] range.
178179
if (output_tensor->bytes / sizeof(float) !=
179180
output_byte_size / sizeof(uint8)) {
@@ -213,11 +214,12 @@ StatusOr<std::unique_ptr<FrameBuffer>> ImageTransformer::Postprocess() {
213214
/*buffer=*/postprocessed_data.data(),
214215
/*stride=*/{output_tensor->dims->data[2] * kRgbPixelBytes,
215216
kRgbPixelBytes}};
216-
postprocessed_frame_buffer = FrameBuffer::Create(
217+
auto postprocessed_frame_buffer = FrameBuffer::Create(
217218
{postprocessed_plane}, to_buffer_dimension, FrameBuffer::Format::kRGB,
218219
FrameBuffer::Orientation::kTopLeft);
219220

220-
return postprocessed_frame_buffer;
221+
FrameBuffer postprocessed_result = *postprocessed_frame_buffer.get();
222+
return postprocessed_result;
221223
}
222224
} // namespace vision
223225
} // namespace task

tensorflow_lite_support/cc/task/vision/image_transformer.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace vision {
6161
// A CLI demo tool is available for easily trying out this API, and provides
6262
// example usage. See:
6363
// examples/task/vision/desktop/image_classifier_demo.cc
64-
class ImageTransformer : public BaseVisionTaskApi<::tflite::task::vision::FrameBuffer> {
64+
class ImageTransformer : public BaseVisionTaskApi<FrameBuffer> {
6565
public:
6666
using BaseVisionTaskApi::BaseVisionTaskApi;
6767

@@ -85,7 +85,7 @@ class ImageTransformer : public BaseVisionTaskApi<::tflite::task::vision::FrameB
8585
// only supported colorspace for now),
8686
// - rotate it according to its `Orientation` so that inference is performed
8787
// on an "upright" image.
88-
tflite::support::StatusOr<::tflite::task::vision::FrameBuffer> Transform(
88+
tflite::support::StatusOr<FrameBuffer> Transform(
8989
const FrameBuffer& frame_buffer);
9090

9191
// Same as above, except that the transformation is performed based on the
@@ -99,15 +99,17 @@ class ImageTransformer : public BaseVisionTaskApi<::tflite::task::vision::FrameB
9999
// `frame_buffer` data before any `Orientation` flag gets applied. Also, the
100100
// region of interest is not clamped, so this method will return a non-ok
101101
// status if the region is out of these bounds.
102-
tflite::support::StatusOr<::tflite::task::vision::FrameBuffer> Transform(
102+
tflite::support::StatusOr<FrameBuffer> Transform(
103103
const FrameBuffer& frame_buffer, const BoundingBox& roi);
104104

105105
protected:
106106
// The options used to build this ImageTransformer.
107107
std::unique_ptr<ImageTransformerOptions> options_;
108108

109109
// Post-processing to transform the raw model outputs into image results.
110-
tflite::support::StatusOr<std::unique_ptr<::tflite::task::vision::FrameBuffer>> Postprocess();
110+
tflite::support::StatusOr<FrameBuffer> Postprocess(
111+
const std::vector<const TfLiteTensor*>& output_tensors,
112+
const FrameBuffer& frame_buffer, const BoundingBox& roi) override;
111113

112114
// Performs sanity checks on the provided ImageTransformerOptions.
113115
static absl::Status SanityCheckOptions(const ImageTransformerOptions& options);

0 commit comments

Comments
 (0)