forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediapipe_model_path.diff
385 lines (361 loc) · 15.2 KB
/
mediapipe_model_path.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
diff --git a/mediapipe/framework/encode_binary_proto.bzl b/mediapipe/framework/encode_binary_proto.bzl
index 3af435f..5a4a4a1 100644
--- a/mediapipe/framework/encode_binary_proto.bzl
+++ b/mediapipe/framework/encode_binary_proto.bzl
@@ -104,6 +104,7 @@ def _encode_binary_proto_impl(ctx):
"--encode=" + ctx.attr.message_type,
"--proto_path=" + ctx.genfiles_dir.path,
"--proto_path=" + ctx.bin_dir.path,
+ "--proto_path=" + ctx.file.input.owner.workspace_root,
"--proto_path=.",
] + path_list + file_list +
["<", textpb.path, ">", binarypb.path],
diff --git a/mediapipe/graphs/object_detection_3d/calculators/BUILD b/mediapipe/graphs/object_detection_3d/calculators/BUILD
index 8f80312..45b8c53 100644
--- a/mediapipe/graphs/object_detection_3d/calculators/BUILD
+++ b/mediapipe/graphs/object_detection_3d/calculators/BUILD
@@ -68,7 +68,7 @@ cc_library(
"//mediapipe/gpu:gl_calculator_helper",
"//mediapipe/gpu:shader_util",
"//mediapipe/modules/objectron/calculators:camera_parameters_cc_proto",
- "//mediapipe/util/android:asset_manager_util",
+ "//mediapipe/util:resource_util",
],
alwayslink = 1,
)
diff --git a/mediapipe/graphs/object_detection_3d/calculators/gl_animation_overlay_calculator.cc b/mediapipe/graphs/object_detection_3d/calculators/gl_animation_overlay_calculator.cc
index 9bc43ba..088f8f8 100644
--- a/mediapipe/graphs/object_detection_3d/calculators/gl_animation_overlay_calculator.cc
+++ b/mediapipe/graphs/object_detection_3d/calculators/gl_animation_overlay_calculator.cc
@@ -12,12 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#if defined(__ANDROID__)
-#include "mediapipe/util/android/asset_manager_util.h"
-#else
#include <fstream>
#include <iostream>
-#endif
#include "mediapipe/framework/calculator_framework.h"
#include "mediapipe/framework/port/ret_check.h"
@@ -27,6 +23,7 @@
#include "mediapipe/graphs/object_detection_3d/calculators/gl_animation_overlay_calculator.pb.h"
#include "mediapipe/graphs/object_detection_3d/calculators/model_matrix.pb.h"
#include "mediapipe/modules/objectron/calculators/camera_parameters.pb.h"
+#include "mediapipe/util/resource_util.h";
namespace mediapipe {
@@ -40,12 +37,7 @@ namespace {
#define GLCHECK(command) command
#endif
-// For ease of use, we prefer ImageFrame on Android and GpuBuffer otherwise.
-#if defined(__ANDROID__)
typedef ImageFrame AssetTextureFormat;
-#else
-typedef GpuBuffer AssetTextureFormat;
-#endif
enum { ATTRIB_VERTEX, ATTRIB_TEXTURE_POSITION, ATTRIB_NORMAL, NUM_ATTRIBUTES };
static const int kNumMatrixEntries = 16;
@@ -68,12 +60,12 @@ static const float kModelMatrix[] = {0.83704215, -0.36174262, 0.41049102, 0.0,
// MODEL_MATRICES (TimedModelMatrixProtoList, optional):
// If provided, will set the model matrices for the objects to be rendered
// during future rendering calls.
-// TEXTURE (ImageFrame on Android / GpuBuffer on iOS, semi-optional):
+// TEXTURE (ImageFrame, semi-optional):
// Texture to use with animation file. Texture is REQUIRED to be passed into
// the calculator, but can be passed in as a Side Packet OR Input Stream.
//
// Input side packets:
-// TEXTURE (ImageFrame on Android / GpuBuffer on iOS, semi-optional):
+// TEXTURE (ImageFrame, semi-optional):
// Texture to use with animation file. Texture is REQUIRED to be passed into
// the calculator, but can be passed in as a Side Packet OR Input Stream.
// ANIMATION_ASSET (String, required):
@@ -185,15 +177,7 @@ class GlAnimationOverlayCalculator : public CalculatorBase {
TriangleMesh *triangle_mesh);
void Normalize3f(float input[3]);
-#if !defined(__ANDROID__)
- // Asset loading routine for all non-Android platforms.
bool LoadAnimation(const std::string &filename);
-#else
- // Asset loading for all Android platforms.
- bool LoadAnimationAndroid(const std::string &filename,
- std::vector<TriangleMesh> *mesh);
- bool ReadBytesFromAsset(AAsset *asset, void *buffer, int num_bytes_to_read);
-#endif
};
REGISTER_CALCULATOR(GlAnimationOverlayCalculator);
@@ -331,113 +315,15 @@ void GlAnimationOverlayCalculator::InitializePerspectiveMatrix(
perspective_matrix_[14] = 2.0f * z_far * z_near * denom;
}
-#if defined(__ANDROID__)
-// Helper function for reading in a specified number of bytes from an Android
-// asset. Returns true if successfully reads in all bytes into buffer.
-bool GlAnimationOverlayCalculator::ReadBytesFromAsset(AAsset *asset,
- void *buffer,
- int num_bytes_to_read) {
- // Most file systems use block sizes of 4KB or 8KB; ideally we'd choose a
- // small multiple of the block size for best input streaming performance, so
- // we go for a reasobably safe buffer size of 8KB = 8*1024 bytes.
- static const int kMaxChunkSize = 8192;
-
- int bytes_left = num_bytes_to_read;
- int bytes_read = 1; // any value > 0 here just to start looping.
-
- // Treat as uint8_t array so we can deal in single byte arithmetic easily.
- uint8_t *currBufferIndex = reinterpret_cast<uint8_t *>(buffer);
- while (bytes_read > 0 && bytes_left > 0) {
- bytes_read = AAsset_read(asset, (void *)currBufferIndex,
- std::min(bytes_left, kMaxChunkSize));
- bytes_left -= bytes_read;
- currBufferIndex += bytes_read;
- }
- // At least log any I/O errors encountered.
- if (bytes_read < 0) {
- LOG(ERROR) << "Error reading from AAsset: " << bytes_read;
- return false;
- }
- if (bytes_left > 0) {
- // Reached EOF before reading in specified number of bytes.
- LOG(WARNING) << "Reached EOF before reading in specified number of bytes.";
- return false;
- }
- return true;
-}
-
-// The below asset streaming code is Android-only, making use of the platform
-// JNI helper classes AAssetManager and AAsset.
-bool GlAnimationOverlayCalculator::LoadAnimationAndroid(
- const std::string &filename, std::vector<TriangleMesh> *meshes) {
- mediapipe::AssetManager *mediapipe_asset_manager =
- Singleton<mediapipe::AssetManager>::get();
- AAssetManager *asset_manager = mediapipe_asset_manager->GetAssetManager();
- if (!asset_manager) {
- LOG(ERROR) << "Failed to access Android asset manager.";
- return false;
- }
-
- // New read-bytes stuff here! First we open file for streaming.
- AAsset *asset = AAssetManager_open(asset_manager, filename.c_str(),
- AASSET_MODE_STREAMING);
- if (!asset) {
- LOG(ERROR) << "Failed to open animation asset: " << filename;
- return false;
- }
-
- // And now, while we are able to stream in more frames, we do so.
- frame_count_ = 0;
- int32 lengths[3];
- while (ReadBytesFromAsset(asset, (void *)lengths, sizeof(lengths[0]) * 3)) {
- // About to start reading the next animation frame. Stream it in here.
- // Each frame stores first the object counts of its three arrays
- // (vertices, texture coordinates, triangle indices; respectively), and
- // then stores each of those arrays as a byte dump, in order.
- meshes->emplace_back();
- TriangleMesh &triangle_mesh = meshes->back();
- // Try to read in vertices (4-byte floats)
- triangle_mesh.vertices.reset(new float[lengths[0]]);
- if (!ReadBytesFromAsset(asset, (void *)triangle_mesh.vertices.get(),
- sizeof(float) * lengths[0])) {
- LOG(ERROR) << "Failed to read vertices for frame " << frame_count_;
- return false;
- }
- // Try to read in texture coordinates (4-byte floats)
- triangle_mesh.texture_coords.reset(new float[lengths[1]]);
- if (!ReadBytesFromAsset(asset, (void *)triangle_mesh.texture_coords.get(),
- sizeof(float) * lengths[1])) {
- LOG(ERROR) << "Failed to read tex-coords for frame " << frame_count_;
- return false;
- }
- // Try to read in indices (2-byte shorts)
- triangle_mesh.index_count = lengths[2];
- triangle_mesh.triangle_indices.reset(new int16[lengths[2]]);
- if (!ReadBytesFromAsset(asset, (void *)triangle_mesh.triangle_indices.get(),
- sizeof(int16) * lengths[2])) {
- LOG(ERROR) << "Failed to read indices for frame " << frame_count_;
- return false;
- }
-
- // Set the normals for this triangle_mesh
- CalculateTriangleMeshNormals(lengths[0], &triangle_mesh);
-
- frame_count_++;
- }
- AAsset_close(asset);
+bool GlAnimationOverlayCalculator::LoadAnimation(const std::string &filename) {
+ auto status_or_path = mediapipe::PathToResourceAsFile(filename);
- LOG(INFO) << "Finished parsing " << frame_count_ << " animation frames.";
- if (meshes->empty()) {
- LOG(ERROR) << "No animation frames were parsed! Erroring out calculator.";
+ if (!status_or_path.ok()) {
+ LOG(ERROR) << "Error resolving asset path: " << filename;
return false;
}
- return true;
-}
-#else // defined(__ANDROID__)
-
-bool GlAnimationOverlayCalculator::LoadAnimation(const std::string &filename) {
- std::ifstream infile(filename.c_str(), std::ifstream::binary);
+ std::ifstream infile(status_or_path->c_str(), std::ifstream::binary);
if (!infile) {
LOG(ERROR) << "Error opening asset with filename: " << filename;
return false;
@@ -501,8 +387,6 @@ bool GlAnimationOverlayCalculator::LoadAnimation(const std::string &filename) {
return true;
}
-#endif
-
void GlAnimationOverlayCalculator::ComputeAspectRatioAndFovFromCameraParameters(
const CameraParametersProto &camera_parameters, float *aspect_ratio,
float *vertical_fov_degrees) {
@@ -552,22 +436,7 @@ absl::Status GlAnimationOverlayCalculator::Open(CalculatorContext *cc) {
// Try to load in the animation asset in a platform-specific manner.
const std::string &asset_name =
cc->InputSidePackets().Tag("ANIMATION_ASSET").Get<std::string>();
- bool loaded_animation = false;
-#if defined(__ANDROID__)
- if (cc->InputSidePackets().HasTag("MASK_ASSET")) {
- has_occlusion_mask_ = true;
- const std::string &mask_asset_name =
- cc->InputSidePackets().Tag("MASK_ASSET").Get<std::string>();
- loaded_animation = LoadAnimationAndroid(mask_asset_name, &mask_meshes_);
- if (!loaded_animation) {
- LOG(ERROR) << "Failed to load mask asset.";
- return absl::UnknownError("Failed to load mask asset.");
- }
- }
- loaded_animation = LoadAnimationAndroid(asset_name, &triangle_meshes_);
-#else
- loaded_animation = LoadAnimation(asset_name);
-#endif
+ bool loaded_animation = LoadAnimation(asset_name);
if (!loaded_animation) {
LOG(ERROR) << "Failed to load animation asset.";
return absl::UnknownError("Failed to load animation asset.");
diff --git a/mediapipe/util/BUILD b/mediapipe/util/BUILD
index 11f08ad..4d2db85 100644
--- a/mediapipe/util/BUILD
+++ b/mediapipe/util/BUILD
@@ -155,12 +155,8 @@ cc_library(
srcs = [
"resource_util.cc",
"resource_util_internal.h",
- ] + select({
- "//conditions:default": ["resource_util_default.cc"],
- "//mediapipe:android": ["resource_util_android.cc"],
- "//mediapipe:ios": ["resource_util_apple.cc"],
- "//mediapipe:macos": ["resource_util_default.cc"],
- }),
+ "resource_util_unity.cc",
+ ],
hdrs = [
"resource_util.h",
],
@@ -183,19 +179,7 @@ cc_library(
"//mediapipe/framework/port:statusor",
"//mediapipe/framework/port:file_helpers",
"@com_google_absl//absl/strings",
- ] + select({
- "//conditions:default": [
- "@com_google_absl//absl/flags:flag",
- ],
- "//mediapipe:android": [
- "//mediapipe/util/android:asset_manager_util",
- "//mediapipe/util/android/file/base",
- ],
- "//mediapipe:ios": [],
- "//mediapipe:macos": [
- "@com_google_absl//absl/flags:flag",
- ],
- }),
+ ],
)
cc_library(
diff --git a/mediapipe/util/resource_util.cc b/mediapipe/util/resource_util.cc
index 8f40154..dfebafd 100644
--- a/mediapipe/util/resource_util.cc
+++ b/mediapipe/util/resource_util.cc
@@ -27,6 +27,7 @@ namespace mediapipe {
namespace {
ResourceProviderFn resource_provider_ = nullptr;
+PathResolverFn path_resolver_ = nullptr;
} // namespace
absl::Status GetResourceContents(const std::string& path, std::string* output,
@@ -37,8 +38,19 @@ absl::Status GetResourceContents(const std::string& path, std::string* output,
return internal::DefaultGetResourceContents(path, output, read_as_binary);
}
+absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
+ if (path_resolver_ == nullptr) {
+ return internal::DefaultPathToResourceAsFile(path);
+ }
+ return path_resolver_(path);
+}
+
void SetCustomGlobalResourceProvider(ResourceProviderFn fn) {
resource_provider_ = std::move(fn);
}
+void SetCustomGlobalPathResolver(PathResolverFn fn) {
+ path_resolver_ = std::move(fn);
+}
+
} // namespace mediapipe
diff --git a/mediapipe/util/resource_util_custom.h b/mediapipe/util/resource_util_custom.h
index 6bc1513..e328d9a 100644
--- a/mediapipe/util/resource_util_custom.h
+++ b/mediapipe/util/resource_util_custom.h
@@ -4,15 +4,22 @@
#include <string>
#include "mediapipe/framework/port/status.h"
+#include "mediapipe/framework/port/statusor.h"
namespace mediapipe {
typedef std::function<absl::Status(const std::string&, std::string*)>
ResourceProviderFn;
+typedef std::function<absl::StatusOr<std::string>(const std::string&)>
+ PathResolverFn;
+
// Overrides the behavior of GetResourceContents.
void SetCustomGlobalResourceProvider(ResourceProviderFn fn);
+// Overfides the behavior of PathToResourceAsFile.
+void SetCustomGlobalPathResolver(PathResolverFn fn);
+
} // namespace mediapipe
#endif // MEDIAPIPE_UTIL_RESOURCE_UTIL_CUSTOM_H_
diff --git a/mediapipe/util/resource_util_internal.h b/mediapipe/util/resource_util_internal.h
index 8ae127a..91ffc9a 100644
--- a/mediapipe/util/resource_util_internal.h
+++ b/mediapipe/util/resource_util_internal.h
@@ -14,6 +14,8 @@ absl::Status DefaultGetResourceContents(const std::string& path,
std::string* output,
bool read_as_binary);
+absl::StatusOr<std::string> DefaultPathToResourceAsFile(const std::string& path);
+
} // namespace internal
} // namespace mediapipe
#endif // MEDIAPIPE_UTIL_RESOURCE_UTIL_INTERNAL_H_
diff --git a/mediapipe/util/resource_util_unity.cc b/mediapipe/util/resource_util_unity.cc
new file mode 100644
index 0000000..946be0d
--- /dev/null
+++ b/mediapipe/util/resource_util_unity.cc
@@ -0,0 +1,21 @@
+#include "mediapipe/framework/port/file_helpers.h"
+#include "mediapipe/framework/port/statusor.h"
+
+namespace mediapipe {
+
+using mediapipe::file::GetContents;
+
+namespace internal {
+
+absl::Status DefaultGetResourceContents(const std::string& path,
+ std::string* output,
+ bool read_as_binary) {
+ return GetContents(path, output, read_as_binary);
+}
+
+absl::StatusOr<std::string> DefaultPathToResourceAsFile(const std::string& path) {
+ return path;
+}
+
+} // namespace internal
+} // namespace mediapipe