Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created January 22, 2019 17:52
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/e30e36110af8915516a4650347f70f53 to your computer and use it in GitHub Desktop.
Save elbruno/e30e36110af8915516a4650347f70f53 to your computer and use it in GitHub Desktop.
CVOnnxWin10DrawFrames.cs
private async Task LoadAndEvaluateModelAsync(VideoFrame videoFrame)
{
_objectDetection.ProbabilityThreshold = 0.5F;
_stopwatch = Stopwatch.StartNew();
_predictions = await _objectDetection.PredictImageAsync(videoFrame);
_stopwatch.Stop();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
DrawFrames();
});
}
private void DrawFrames()
{
OverlayCanvas.Children.Clear();
var message = $"{DateTime.Now.ToLongTimeString()} - {1000f / _stopwatch.ElapsedMilliseconds,4:f1} fps{Environment.NewLine}============================={Environment.NewLine}";
if (_predictions.Count > 0)
foreach (var prediction in _predictions)
DrawFrame(prediction, OverlayCanvas);
TextBlockResults.Text = message;
}
private void DrawFrame(PredictionModel prediction, Canvas overlayCanvas)
{
_overlayCanvasActualWidth = (uint)CameraPreview.ActualWidth;
_overlayCanvasActualHeight = (uint)CameraPreview.ActualHeight;
var x = (uint)Math.Max(prediction.Box.Left, 0);
var y = (uint)Math.Max(prediction.Box.Top, 0);
var w = (uint)Math.Min(_overlayCanvasActualWidth - x, prediction.Box.Width);
var h = (uint)Math.Min(_overlayCanvasActualHeight - y, prediction.Box.Height);
Debug.WriteLine($"\tOverLay Canvas \tActual Width: {_overlayCanvasActualWidth}, Actual Height: {_overlayCanvasActualHeight}");
Debug.WriteLine(prediction.GetExtendedDescription());
Debug.WriteLine($"\tOriginal\tY: {y}, x: {x}, Height: {h}, Width: {w}");
// fit to current size
var factorSize = 100u;
x = _overlayCanvasActualWidth * x / factorSize;
y = _overlayCanvasActualHeight * y / factorSize;
w = _overlayCanvasActualWidth * w / factorSize;
h = _overlayCanvasActualHeight * h / factorSize;
Debug.WriteLine($"\tScaled\tY: {y}, x: {x}, Height: {h}, Width: {w}");
var rectStroke = _lineBrushGreen;
switch (prediction.TagName)
{
case "Venom":
rectStroke = _lineBrushGray;
break;
case "Rocket_Racoon":
rectStroke = _lineBrushYellow;
break;
case "Iron_Fist":
rectStroke = _lineBrushGreen;
break;
}
var r = new Windows.UI.Xaml.Shapes.Rectangle
{
Tag = prediction,
Width = w,
Height = h,
Fill = _fillBrush,
Stroke = rectStroke,
StrokeThickness = _lineThickness,
Margin = new Thickness(x, y, 0, 0)
};
var tb = new TextBlock
{
Margin = new Thickness(x + 4, y + 4, 0, 0),
Text = $"{prediction}",
FontWeight = FontWeights.Bold,
Width = 126,
Height = 21,
HorizontalTextAlignment = TextAlignment.Center
};
var textBack = new Windows.UI.Xaml.Shapes.Rectangle
{
Width = 150,
Height = 29,
Fill = rectStroke,
Margin = new Thickness(x, y, 0, 0)
};
overlayCanvas.Children.Add(textBack);
overlayCanvas.Children.Add(tb);
overlayCanvas.Children.Add(r);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment