示例#1
0
        public static async Task <CustomGestureModel> CreateFromStreamAsync(IRandomAccessStreamReference stream)
        {
            CustomGestureModel learningModel = new CustomGestureModel();

            learningModel.model = await LearningModel.LoadFromStreamAsync(stream);

            learningModel.session = new LearningModelSession(learningModel.model);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }
        private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            CanvasDevice       device       = CanvasDevice.GetSharedDevice();
            CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)inkDataCanvas.ActualWidth, (int)inkDataCanvas.ActualHeight, 96);

            using (var ds = renderTarget.CreateDrawingSession())
            {
                ds.Clear(Colors.Black);
                ds.DrawInk(inkDataCanvas.InkPresenter.StrokeContainer.GetStrokes());
            }

            using (var ms = new InMemoryRandomAccessStream())
            {
                await renderTarget.SaveAsync(ms, CanvasBitmapFileFormat.Jpeg, 1);

                await ms.FlushAsync();

                var decoder = await BitmapDecoder.CreateAsync(ms);

                var img = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms);

                encoder.BitmapTransform.ScaledHeight = 227;
                encoder.BitmapTransform.ScaledWidth  = 227;
                encoder.SetSoftwareBitmap(img);
                await encoder.FlushAsync();

                decoder = await BitmapDecoder.CreateAsync(ms);

                img = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);

                img = SoftwareBitmap.Convert(img, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
                var model = await CustomGestureModel.CreateFromStreamAsync(await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///CustomGesture.onnx")));

                var output = await model.EvaluateAsync(new CustomGestureInput
                {
                    data = ImageFeatureValue.CreateFromVideoFrame(VideoFrame.CreateWithSoftwareBitmap(img))
                });

                if (output != null)
                {
                    var res   = output.classLabel.GetAsVectorView().ToList();
                    var label = res.FirstOrDefault();
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                    {
                        await new MessageDialog(label).ShowAsync();
                    });
                }
            }
        }