Load model file content from model file path with the help of GetResourceContents in browsers.

This can handle the model files that are provided via a custom ResourceProviderFn.

PiperOrigin-RevId: 492308453
This commit is contained in:
Jiuqiang Tang 2022-12-01 15:00:00 -08:00 committed by Copybara-Service
parent af990c3da1
commit ead41132a8

View File

@ -99,11 +99,21 @@ const tflite::Model* ModelResources::GetTfLiteModel() const {
absl::Status ModelResources::BuildModelFromExternalFileProto() {
if (model_file_->has_file_name()) {
#ifdef __EMSCRIPTEN__
// In browsers, the model file may require a custom ResourceProviderFn to
// provide the model content. The open() method may not work in this case.
// Thus, loading the model content from the model file path in advance with
// the help of GetResourceContents.
MP_RETURN_IF_ERROR(mediapipe::GetResourceContents(
model_file_->file_name(), model_file_->mutable_file_content()));
model_file_->clear_file_name();
#else
// If the model file name is a relative path, searches the file in a
// platform-specific location and returns the absolute path on success.
ASSIGN_OR_RETURN(std::string path_to_resource,
mediapipe::PathToResourceAsFile(model_file_->file_name()));
model_file_->set_file_name(path_to_resource);
#endif // __EMSCRIPTEN__
}
ASSIGN_OR_RETURN(
model_file_handler_,