Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.