public async Task <BearModelOutput> EvaluateAsync(BearModelInput input) { binding.Bind("data", input.data); var result = await session.EvaluateAsync(binding, "0"); var output = new BearModelOutput(); output.classLabel = result.Outputs["classLabel"] as TensorString; output.loss = result.Outputs["loss"] as IList <Dictionary <string, float> >; return(output); }
private async void RecognizeBear() { // 加载模型 StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/BearModel.onnx")); BearModelModel model = await BearModelModel.CreateFromStreamAsync(modelFile); // 构建输入数据 BearModelInput bearModelInput = await GetInputData(); // 推理 BearModelOutput output = await model.EvaluateAsync(bearModelInput); tbBearType.Text = output.classLabel.GetAsVectorView().ToList().FirstOrDefault(); }
private async Task <BearModelInput> GetInputData() { // 将图片控件重绘到图片上 RenderTargetBitmap rtb = new RenderTargetBitmap(); await rtb.RenderAsync(imgBear); // 取得所有像素值 var pixelBuffer = await rtb.GetPixelsAsync(); // 构造模型需要的输入格式 SoftwareBitmap softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, rtb.PixelWidth, rtb.PixelHeight); VideoFrame videoFrame = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); ImageFeatureValue imageFeatureValue = ImageFeatureValue.CreateFromVideoFrame(videoFrame); BearModelInput bearModelInput = new BearModelInput(); bearModelInput.data = imageFeatureValue; return(bearModelInput); }