/// <summary> /// Download Model Method /// </summary> public async void DownloadModels(MLCustomRemoteModel customRemoteModel) { MLModelDownloadStrategy strategy = new MLModelDownloadStrategy.Factory() .NeedWifi() .SetRegion(MLModelDownloadStrategy.RegionDrEurope) .Create(); MLModelDownloadListener modelDownloadListener = new MLModelDownloadListener(this, DownloadCase); System.Threading.Tasks.Task downloadTask = MLLocalModelManager.Instance.DownloadModelAsync(customRemoteModel, strategy, modelDownloadListener); try { await downloadTask; if (downloadTask.IsCompleted) { //Detection success Toast.MakeText(this, "Model download successful", ToastLength.Short).Show(); } else { //Detection success Toast.MakeText(this, "Model download failed", ToastLength.Short).Show(); } } catch (System.Exception e) { //Operation failed Log.Debug(Tag, "Download operation failed: " + e.Message); } }
/// <summary> /// Mixed Mode Analyze /// Recommended /// </summary> private void PictureAnalysis(Bitmap bitmap) { this.localModel = new MLCustomLocalModel.Factory(ModelName).SetAssetPathFile(ModelFullName).Create(); this.remoteModel = new MLCustomRemoteModel.Factory(RemoteModelName).Create(); DownloadModels(remoteModel); MLLocalModelManager.Instance .IsModelExist(remoteModel) .ContinueWithTask(new CustomModelContinuation( delegate(Huawei.Hmf.Tasks.Task task) { if (!task.IsSuccessful) { throw task.Exception; } Java.Lang.Boolean isDownloaded = task.Result.JavaCast <Java.Lang.Boolean>(); MLModelExecutorSettings settings = null; if ((bool)isDownloaded) { Toast.MakeText(this, "Executing Remote Model", ToastLength.Short).Show(); settings = new MLModelExecutorSettings.Factory(remoteModel).Create(); } else { Toast.MakeText(this, "Model download failed. Executing Local Model", ToastLength.Long).Show(); settings = new MLModelExecutorSettings.Factory(localModel).Create(); } try { this.modelExecutor = MLModelExecutor.GetInstance(settings); ExecutorImpl(modelExecutor, bitmap); } catch (System.Exception e) { Log.Info(Tag, e.ToString()); } } )); }