private async Task StartPreviewAsync()
        {
            try
            {
                mediaCapture = new MediaCapture();
                await mediaCapture.InitializeAsync();

                displayRequest.RequestActive();
                DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
            }
            catch (UnauthorizedAccessException)
            {
                // This will be thrown if the user denied access to the camera in privacy settings
                await AlertHelper.ShowMessageAsync("The app was denied access to the camera");

                return;
            }

            try
            {
                PreviewControl.Source = mediaCapture;
                await mediaCapture.StartPreviewAsync();

                isPreviewing = true;
            }
            catch (System.IO.FileLoadException)
            {
                mediaCapture.CaptureDeviceExclusiveControlStatusChanged += _mediaCapture_CaptureDeviceExclusiveControlStatusChanged;
            }
        }
Пример #2
0
        private async Task EvaluteImageAsync(VideoFrame videoFrame)
        {
            var startTime = DateTime.Now;

            if (model == null)
            {
                var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/GoogLeNetPlaces.onnx"));

                if (modelFile != null)
                {
                    model = new GoogLeNetPlacesModel();
                    await MLHelper.CreateModelAsync(modelFile, model);
                }
            }
            var input = new GoogLeNetPlacesInput()
            {
                sceneImage = ImageFeatureValue.CreateFromVideoFrame(videoFrame)
            };

            try
            {
                var res = await model.EvaluateAsync(input) as GoogLeNetPlacesOutput;

                if (res != null)
                {
                    var results = new List <LabelResult>();
                    if (res.sceneLabelProbs != null)
                    {
                        var dict = res.sceneLabelProbs.FirstOrDefault();
                        foreach (var kv in dict)
                        {
                            results.Add(new LabelResult
                            {
                                Label  = kv.Key,
                                Result = (float)Math.Round(kv.Value * 100, 2)
                            });
                        }
                        results.Sort((p1, p2) =>
                        {
                            return(p2.Result.CompareTo(p1.Result));
                        });
                    }
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        var places                   = res.sceneLabel.GetAsVectorView().ToArray();
                        outputText.Text              = places.FirstOrDefault();
                        resultList.ItemsSource       = results;
                        previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString();
                    });
                }
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
        }
        private async Task EvaluteImageAsync(VideoFrame videoFrame)
        {
            try
            {
                var startTime = DateTime.Now;
                if (model == null)
                {
                    var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/Resnet50.onnx"));

                    if (modelFile != null)
                    {
                        model = new ResNet50Model();
                        await MLHelper.CreateModelAsync(modelFile, model);
                    }
                }

                var input = new ResNet50ModelInput()
                {
                    image = videoFrame
                };

                var res = await model.EvaluateAsync(input) as ResNet50ModelOutput;

                if (res != null)
                {
                    var results = new List <LabelResult>();
                    foreach (var kv in res.classLabelProbs)
                    {
                        results.Add(new LabelResult
                        {
                            Label  = kv.Key,
                            Result = (float)Math.Round(kv.Value * 100, 2)
                        });
                    }
                    results.Sort((p1, p2) =>
                    {
                        return(p2.Result.CompareTo(p1.Result));
                    });
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString();
                        outputText.Text        = res.classLabel.FirstOrDefault();
                        resultList.ItemsSource = results;
                    });
                }
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
        }
        private async Task EvaluteImageAsync(VideoFrame videoFrame, bool isImage)
        {
            try
            {
                var startTime = DateTime.Now;
                if (model == null)
                {
                    var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/TinyYOLO.onnx"));

                    if (modelFile != null)
                    {
                        model = new TinyYOLOModel();
                        await MLHelper.CreateModelAsync(modelFile, model);
                    }
                }

                var input = new TinyYOLOInput()
                {
                    image = ImageFeatureValue.CreateFromVideoFrame(videoFrame)
                };

                var res = await model.EvaluateAsync(input) as TinyYOLOOutput;

                if (res != null)
                {
                    var data  = res.grid.GetAsVectorView().ToList();
                    var boxes = model.ComputeBoundingBoxes(data);
                    await DrawDetectedObjectRectAsync(boxes, isImage);

                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString();
                    });
                }
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
        }
        private async void ImagePickerControl_ImagePreviewReceived(object sender, WindowsMLDemos.Common.UI.ImagePreviewReceivedEventArgs e)
        {
            try
            {
                myMap.Visibility = Visibility.Visible;
                if (rnModel == null)
                {
                    var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/RN1015k500.onnx"));

                    if (modelFile != null)
                    {
                        rnModel = new RN1015k500Model();
                        await MLHelper.CreateModelAsync(modelFile, rnModel, true);
                    }
                }
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                {
                    if (e.PreviewImage != null)
                    {
                        var output = await rnModel.EvaluateAsync(new RN1015k500Input
                        {
                            data = ImageFeatureValue.CreateFromVideoFrame(e.PreviewImage)//TensorFloat16Bit.CreateFromArray(new long[] { -1, 3, 224, 224 }, imageData)
                        }) as RN1015k500Output;


                        if (output != null)
                        {
                            var res = output.classLabel.GetAsVectorView().ToArray();
                            var b   = output.softmax_output?.ToList();
                            if (res.Length > 0)
                            {
                                var locationData = res[0].Split('\t');
                                var city         = locationData[0];
                                var lat          = float.Parse(locationData[1]);
                                var lon          = float.Parse(locationData[2]);
                                var sPoint       = new Geopoint(new BasicGeoposition
                                {
                                    Latitude  = lat,
                                    Longitude = lon
                                });


                                Geopoint pointToReverseGeocode = sPoint;

                                // Reverse geocode the specified geographic location.
                                MapLocationFinderResult result =
                                    await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);

                                // If the query returns results, display the name of the town
                                // contained in the address of the first result.
                                if (result.Status == MapLocationFinderStatus.Success)
                                {
                                    city = result.Locations[0].DisplayName;// + result.Locations[0].Address.FormattedAddress + result.Locations[0].Address.Country;
                                }
                                myMap.Center = sPoint;

                                myMap.Layers.Add(new MapElementsLayer
                                {
                                    ZIndex      = 1,
                                    MapElements = new List <MapElement>()
                                    {
                                        new MapIcon
                                        {
                                            Location = sPoint,
                                            NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1),
                                            ZIndex = 0,
                                            Title  = city,
                                            CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible,
                                            Visible = true
                                        }
                                    }
                                });
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
            rnModel.Session.Dispose();
            rnModel.LearningModel.Dispose();
            rnModel = null;
        }