Пример #1
0
 /// <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());
         }
     }
                           ));
 }
Пример #2
0
        /// <summary>
        /// Execute Custom Model
        /// classification
        /// </summary>
        public void ExecutorImpl(MLModelExecutor modelExecutor, Bitmap bitmap)
        {
            byte[][][][] convertedBitmap = ConvertBitmapToInputFormat(bitmap);
            byte[]       modelInputArray = FourDimenArrayToOneDimen(convertedBitmap);

            MLModelInputs inputs = null;

            try
            {
                inputs = new MLModelInputs.Factory().Add(modelInputArray).Create();
                // If the model requires multiple inputs, you need to call Add() for multiple times so that image data can be input to the inference engine at a time.
            }
            catch (MLException e)
            {
                // Handle the input data formatting exception.
                Log.Info(Tag, " input data format exception! " + e.Message);
            }

            MLModelInputOutputSettings inOutSettings = null;

            try
            {
                // according to the model requirement, set the in and out format.
                inOutSettings = new MLModelInputOutputSettings.Factory()
                                .SetInputFormat(0, MLModelDataType.Byte, new int[] { BitmapHeight *BitmapWidth * 3 })
                                .SetOutputFormat(0, MLModelDataType.Byte, new int[] { OutputSize })
                                .Create();
            }
            catch (MLException e)
            {
                Log.Info(Tag, "set input output format failed! " + e.Message);
            }
            modelExecutor.Exec(inputs, inOutSettings).AddOnSuccessListener(new OnSuccessListener(
                                                                               delegate(Java.Lang.Object myobj)
            {
                Log.Info(Tag, "interpret get result");
                MLModelOutputs mlModelOutputs = (MLModelOutputs)myobj;
                if (mlModelOutputs.Outputs.Count != 0)
                {
                    byte[] output = (byte[])mlModelOutputs.GetOutput(0);                     // index

                    var myFlo = GetFloatRest(output);
                    PrepareResult(myFlo);

                    // display the result
                    string totalResult = "";
                    foreach (var item in result)
                    {
                        if (item.Value != 0)
                        {
                            totalResult += item.Key.ToString() + ": " + item.Value.ToString();
                            totalResult += "\n";
                        }
                    }

                    DisplayResult(totalResult);
                }
            }

                                                                               )).AddOnFailureListener(new OnFailureListener(

                                                                                                           delegate(Java.Lang.Exception e)
            {
                Log.Info(Tag, " ModelExecutor.Exec() failed: " + e.Message);
            }
                                                                                                           ));
        }