forked from NickSwardh/YoloDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25feaa6
commit 82b2770
Showing
7 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Application x:Class="WebcamDemo.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:WebcamDemo" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Windows; | ||
|
||
namespace WebcamDemo | ||
{ | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly: ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<Window x:Class="WebcamDemo.MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:WebcamDemo" | ||
xmlns:skia="clr-namespace:SkiaSharp.Views.WPF;assembly=SkiaSharp.Views.WPF" | ||
mc:Ignorable="d" | ||
Title="Webcam Demo | YoloDotNet" Height="645" Width="1080" | ||
Background="Black" ResizeMode="NoResize" Closing="WindowClosing"> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="*" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
|
||
<!-- Webcam --> | ||
<skia:SKElement x:Name="WebCamFrame" | ||
Grid.Row="0" | ||
Width="1080" | ||
PaintSurface="UpdateWebcamFrame" /> | ||
|
||
<!-- Buttons --> | ||
<StackPanel Grid.Row="1" | ||
Orientation="Horizontal" | ||
HorizontalAlignment="Center" | ||
Margin="20"> | ||
|
||
<Button x:Name="Start" | ||
Content="Start Detection" | ||
Width="120" | ||
Height="30" | ||
Padding="10,0,10,0" | ||
Margin="0,0,10,0" | ||
Click="StartClick" /> | ||
|
||
<Button x:Name="Stop" | ||
Content="Stop Detection" | ||
Width="120" | ||
Height="30" | ||
Padding="10,0,10,0" | ||
Click="StopClick" /> | ||
|
||
</StackPanel> | ||
|
||
</Grid> | ||
</Window> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using Emgu.CV; | ||
using Emgu.CV.CvEnum; | ||
using Emgu.CV.Util; | ||
using SkiaSharp; | ||
using SkiaSharp.Views.Desktop; | ||
using System.Windows; | ||
using System.Windows.Threading; | ||
using YoloDotNet; | ||
using YoloDotNet.Enums; | ||
using YoloDotNet.Extensions; | ||
using YoloDotNet.Models; | ||
using YoloDotNet.Test.Common; | ||
|
||
namespace WebcamDemo | ||
{ | ||
/// <summary> | ||
/// Interaction logic for MainWindow.xaml | ||
/// </summary> | ||
public partial class MainWindow : Window | ||
{ | ||
#region Fields | ||
|
||
private readonly Yolo _yolo = default!; | ||
private SKImage _currentFrame = default!; | ||
private Dispatcher _dispatcher = default!; | ||
|
||
private bool _runDetection = false; | ||
|
||
private SKRect _rect; | ||
|
||
#endregion | ||
|
||
#region Constants | ||
|
||
const int WEBCAM_WIDTH = 1080; | ||
const int WEBCAM_HEIGHT = 608; | ||
const int FPS = 30; | ||
const string FRAME_FORMAT_EXTENSION = ".png"; | ||
|
||
#endregion | ||
|
||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
|
||
// Instantiate yolo | ||
_yolo = new Yolo(new YoloOptions() | ||
{ | ||
OnnxModel = SharedConfig.GetTestModelV11(ModelType.ObjectDetection), | ||
ModelType = ModelType.ObjectDetection, | ||
Cuda = true, | ||
PrimeGpu = false | ||
}); | ||
|
||
_dispatcher = Dispatcher.CurrentDispatcher; | ||
|
||
_currentFrame = SKImage.FromBitmap(new SKBitmap(WEBCAM_WIDTH, WEBCAM_HEIGHT)); | ||
_rect = new SKRect(0, 0, WEBCAM_WIDTH, bottom: WEBCAM_HEIGHT); | ||
|
||
// Start webcam on a separate thread | ||
Task.Run(() => WebcamAsync()); | ||
} | ||
|
||
private async Task WebcamAsync() | ||
{ | ||
// Configure webcam | ||
using var capture = new VideoCapture(0, VideoCapture.API.DShow); | ||
capture.Set(CapProp.FrameCount, FPS); | ||
capture.Set(CapProp.FrameWidth, WEBCAM_WIDTH); | ||
capture.Set(CapProp.FrameHeight, WEBCAM_HEIGHT); | ||
|
||
using var mat = new Mat(); | ||
using var buffer = new VectorOfByte(); | ||
|
||
while (true) | ||
{ | ||
// Capture current frame from webcam | ||
capture.Read(mat); | ||
|
||
// Encode mat to a valid image format and to a buffer | ||
CvInvoke.Imencode(FRAME_FORMAT_EXTENSION, mat, buffer); | ||
|
||
// "Rewind" buffer | ||
buffer.Position = 0; | ||
|
||
// Read buffer to an SKImage | ||
_currentFrame = SKImage.FromEncodedData(buffer); | ||
|
||
// Clean up | ||
buffer.Clear(); | ||
|
||
if (_runDetection) | ||
{ | ||
// Run inference on frame | ||
var results = _yolo.RunObjectDetection(_currentFrame); | ||
|
||
// Draw results | ||
_currentFrame = _currentFrame.Draw(results); | ||
} | ||
|
||
// Update GUI | ||
await _dispatcher.InvokeAsync(() => WebCamFrame.InvalidateVisual()); | ||
} | ||
} | ||
|
||
private void UpdateWebcamFrame(object sender, SKPaintSurfaceEventArgs e) | ||
{ | ||
using var canvas = e.Surface.Canvas; | ||
canvas.DrawImage(_currentFrame, _rect); | ||
canvas.Flush(); | ||
} | ||
|
||
private void StartClick(object sender, RoutedEventArgs e) | ||
=> _runDetection = true; | ||
|
||
private void StopClick(object sender, RoutedEventArgs e) | ||
=> _runDetection = false; | ||
|
||
private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e) | ||
{ | ||
_yolo?.Dispose(); | ||
_currentFrame?.Dispose(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net8.0-windows</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UseWPF>true</UseWPF> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Emgu.CV.Bitmap" Version="4.9.0.5494" /> | ||
<PackageReference Include="Emgu.CV.runtime.windows" Version="4.9.0.5494" /> | ||
<PackageReference Include="SkiaSharp.Views.WPF" Version="2.88.9" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\test\YoloDotNet.Test.Common\YoloDotNet.Test.Common.csproj" /> | ||
<ProjectReference Include="..\YoloDotNet\YoloDotNet.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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