Created
January 21, 2019 16:28
-
-
Save elbruno/a52724258fd7d4ae268d97edbeff9b26 to your computer and use it in GitHub Desktop.
CVUwpMainPageWebCamAndOnnx.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override async void OnNavigatedTo(NavigationEventArgs e) | |
{ | |
InitModel(); | |
await CameraPreview.StartAsync(); | |
CameraPreview.CameraHelper.FrameArrived += CameraHelper_FrameArrived; | |
} | |
private async void CameraHelper_FrameArrived(object sender, Microsoft.Toolkit.Uwp.Helpers.FrameEventArgs e) | |
{ | |
if (e.VideoFrame.SoftwareBitmap == null) return; | |
try | |
{ | |
if (_objectDetection?.Model == null) return; | |
await LoadAndEvaluateModelAsync(e.VideoFrame); | |
} | |
catch (Exception exception) | |
{ Debug.WriteLine(exception); } | |
} | |
private ObjectDetection _objectDetection; | |
private async void InitModel() | |
{ | |
_objectDetection = new ObjectDetection(); | |
var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/model.onnx")); | |
await _objectDetection.Init(modelFile); | |
} | |
private async Task LoadAndEvaluateModelAsync(VideoFrame videoFrame) | |
{ | |
var result = await _objectDetection.PredictImageAsync(videoFrame); | |
var message = $"{DateTime.Now.ToLongTimeString()}{Environment.NewLine}============================={Environment.NewLine}"; | |
message = result.Aggregate(message, (current, predictionModel) => current + $" {predictionModel.TagName} {(predictionModel.Probability * 100.0f):#0.00}% {Environment.NewLine}"); | |
Debug.WriteLine(message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment