Skip to content

Commit

Permalink
Improve screenshoting through profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Sep 24, 2023
1 parent 5bb2a36 commit d0bef4f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private async Task EditProfile(ProfileViewModel profileViewModel)

ICommand IProfilesViewModel.LaunchProfileCommand => LaunchProfileCommand;
[RelayCommand(CanExecute = nameof(CanLaunchProfile))]
private async Task LaunchProfile(ProfileViewModel profileViewModel)
private void LaunchProfile(ProfileViewModel profileViewModel)
{
Guard.IsFalse(_runningProfile == profileViewModel);
if (_runningProfile != null)
Expand All @@ -117,7 +117,7 @@ private async Task LaunchProfile(ProfileViewModel profileViewModel)

ICommand IProfilesViewModel.StopProfileCommand => StopProfileCommand;
[RelayCommand(CanExecute = nameof(CanStopProfile))]
private async Task StopProfile(ProfileViewModel profile)
private void StopProfile(ProfileViewModel profile)
{
Guard.IsNotNull(_runningProfile);
_profileRunner.Stop();
Expand Down
32 changes: 27 additions & 5 deletions SightKeeper.Services/Scoring/HotKeyProfileRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public float MinimumProbability
Guard.IsBetween(value, 0, 1);
Guard.IsLessThanOrEqualTo(value, MaximumProbability);
_minimumProbability = value;
UpdateDetectionThreshold();
}
}

Expand Down Expand Up @@ -89,6 +90,7 @@ public void Run(Profile profile)
.DisposeWithEx(_currentProfileDisposables);
_profileEditor.ProfileEdited.Where(editedProfile => editedProfile == profile).Subscribe(OnProfileEdited)
.DisposeWithEx(_currentProfileDisposables);
UpdateDetectionThreshold();
}

private void OnProfileEdited(Profile profile)
Expand All @@ -98,25 +100,38 @@ private void OnProfileEdited(Profile profile)
profileItemClass => profileItemClass.Index);
_streamDetector.Weights = profile.Weights;
_streamDetector.ProbabilityThreshold = profile.DetectionThreshold;
UpdateDetectionThreshold();
}

private void HandleDetection(byte[] imageData, ImmutableList<DetectionItem> items)
{
Guard.IsNotNull(_itemClassesIndexes);
Guard.IsNotNull(_currentProfile);
var suitableItems = items.Where(item => _itemClassesIndexes.ContainsKey(item.ItemClass)).ToImmutableList();
var suitableItems = items.Where(item => item.Probability >= _currentProfile.DetectionThreshold && _itemClassesIndexes.ContainsKey(item.ItemClass)).ToImmutableList();
if (!suitableItems.Any())
return;
var mostSuitableItem = suitableItems.MinBy(GetItemOrder);
MoveTo(mostSuitableItem.Bounding);
if (MakeScreenshots && _lastScreenshotTime + _screenshotingDelay <= DateTime.UtcNow && items.Any(item => item.Probability >= MinimumProbability && item.Probability <= MaximumProbability))
TryMakeScreenshot(imageData, items);
}

private async void TryMakeScreenshot(byte[] imageData, ImmutableList<DetectionItem> items)
{
Guard.IsNotNull(_currentProfile);
if (MakeScreenshots && !_isLoadingScreenshots && _lastScreenshotTime + _screenshotingDelay <= DateTime.UtcNow && items.Any(item => item.Probability >= MinimumProbability && item.Probability <= MaximumProbability))
{
if (_currentProfile.Weights.Library.DataSet.ScreenshotsLibrary.Screenshots == null)
_screenshotsDataAccess.LoadAll(_currentProfile.Weights.Library.DataSet.ScreenshotsLibrary);
{
_isLoadingScreenshots = true;
await _screenshotsDataAccess.LoadAll(_currentProfile.Weights.Library.DataSet.ScreenshotsLibrary);
_isLoadingScreenshots = false;
}
_lastScreenshotTime = DateTime.UtcNow;
_currentProfile.Weights.Library.DataSet.ScreenshotsLibrary.CreateScreenshot(imageData);
}
}

private bool _isLoadingScreenshots;

private float GetItemOrder(DetectionItem item)
{
Expand Down Expand Up @@ -170,7 +185,14 @@ public void Stop()
private Profile? _currentProfile;
private CompositeDisposable? _currentProfileDisposables;
private Dictionary<ItemClass, byte>? _itemClassesIndexes;
private float _minimumProbability;
private float _maximumProbability;
private float _minimumProbability = 0.2f;
private float _maximumProbability = 0.5f;
private byte _maximumFPS = _defaultMaximumFPS;

private void UpdateDetectionThreshold()
{
if (_currentProfile == null)
return;
_streamDetector.ProbabilityThreshold = Math.Min(MinimumProbability, _currentProfile.DetectionThreshold);
}
}

0 comments on commit d0bef4f

Please sign in to comment.