Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created January 21, 2019 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/a52724258fd7d4ae268d97edbeff9b26 to your computer and use it in GitHub Desktop.
Save elbruno/a52724258fd7d4ae268d97edbeff9b26 to your computer and use it in GitHub Desktop.
CVUwpMainPageWebCamAndOnnx.cs
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