-
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.
Temporarily hardcoded runtime detection (just for fun)
- Loading branch information
Showing
10 changed files
with
244 additions
and
29 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
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,101 @@ | ||
using System.Collections.Immutable; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Subjects; | ||
using CommunityToolkit.Diagnostics; | ||
using Serilog; | ||
using SerilogTimings; | ||
using SightKeeper.Domain.Model; | ||
|
||
namespace SightKeeper.Application.Scoring; | ||
|
||
public sealed class StreamDetector | ||
{ | ||
public IObservable<ImmutableList<DetectionItem>> ObservableDetection => _detection; | ||
|
||
public bool IsEnabled | ||
{ | ||
get => _d != null; | ||
set | ||
{ | ||
var isEnabled = IsEnabled; | ||
if (value == isEnabled) | ||
return; | ||
if (value) | ||
{ | ||
var sleepTime = TimeSpan.FromMilliseconds(10); | ||
Guard.IsNotNull(Weights); | ||
_screenCapture.Resolution = Weights.Library.DataSet.Resolution; | ||
Guard.IsNull(_d); | ||
var previousImage = Array.Empty<byte>(); | ||
_d = Observable.Create<ImmutableList<DetectionItem>>(async observer => | ||
{ | ||
var work = true; | ||
while (work) | ||
{ | ||
using var operation = Operation.Begin("Detecting in loop"); | ||
var image = await _screenCapture.CaptureAsync(); | ||
using var imagesComparisonOperation = Operation.Begin("Comparing images"); | ||
var imagesAreEqual = image.SequenceEqual(previousImage); | ||
imagesComparisonOperation.Complete(nameof(imagesAreEqual), imagesAreEqual); | ||
if (imagesAreEqual) | ||
{ | ||
Log.Debug("Images are equal, skipping..."); | ||
continue; | ||
} | ||
previousImage = image; | ||
var result = await _detector.Detect(image, CancellationToken.None); | ||
operation.Complete(); | ||
observer.OnNext(result.ToImmutableList()); | ||
BurnTime(sleepTime); | ||
} | ||
return Disposable.Create(() => | ||
{ | ||
work = false; | ||
}); | ||
}).Subscribe(_detection); | ||
} | ||
else | ||
{ | ||
Guard.IsNotNull(_d); | ||
_d.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
private static void BurnTime(TimeSpan time) | ||
{ | ||
var start = DateTime.UtcNow; | ||
var stop = start + time; | ||
while (DateTime.UtcNow < stop) | ||
{ | ||
} | ||
} | ||
|
||
public Weights? Weights | ||
{ | ||
get => _detector.Weights; | ||
set => _detector.Weights = value; | ||
} | ||
public float ProbabilityThreshold | ||
{ | ||
get => _detector.ProbabilityThreshold; | ||
set => _detector.ProbabilityThreshold = value; | ||
} | ||
public float IoU | ||
{ | ||
get => _detector.IoU; | ||
set => _detector.IoU = value; | ||
} | ||
|
||
public StreamDetector(Detector detector, ScreenCapture screenCapture) | ||
{ | ||
_detector = detector; | ||
_screenCapture = screenCapture; | ||
} | ||
|
||
private readonly Detector _detector; | ||
private readonly ScreenCapture _screenCapture; | ||
private IDisposable? _d; | ||
private readonly Subject<ImmutableList<DetectionItem>> _detection = new(); | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.