1- using Microsoft . Azure . CognitiveServices . Vision . CustomVision . Training ;
1+ using Fritz . StreamLib . Core ;
2+ using Fritz . StreamTools . Hubs ;
3+ using Microsoft . AspNetCore . SignalR ;
4+ using Microsoft . Azure . CognitiveServices . Vision . CustomVision . Training ;
25using Microsoft . Azure . CognitiveServices . Vision . CustomVision . Training . Models ;
36using Microsoft . Extensions . Configuration ;
7+ using Microsoft . Extensions . DependencyInjection ;
48using Microsoft . Extensions . Hosting ;
59using Microsoft . Extensions . Logging ;
610using System ;
711using System . Collections . Generic ;
812using System . ComponentModel . DataAnnotations ;
13+ using System . IO ;
14+ using System . Linq ;
915using System . Text ;
1016using System . Threading ;
1117using System . Threading . Tasks ;
@@ -18,29 +24,27 @@ public class ScreenshotTrainingService : IHostedService, ITrainHat
1824 // TODO: Track how many images are loaded -- 5k is the maximum for the FREE service
1925 private string _CustomVisionKey = "" ;
2026 private string _AzureEndpoint = "" ;
21- private string _TwitchChannel = "" ;
2227 private Guid _AzureProjectId ;
23- private readonly ILogger _Logger ;
28+ private ILogger _Logger ;
29+ private IServiceProvider _Services ;
2430 private CancellationTokenSource _TokenSource ;
2531
2632 private bool _CurrentlyTraining = false ;
2733 private byte _TrainingCount = 0 ;
2834 private Task _TrainingTask ;
2935 private byte _RetryCount = 0 ;
3036
31- public string TwitchScreenshotUrl => $ "https://static-cdn.jtvnw.net/previews-ttv/live_user_{ _TwitchChannel } -1280x720.jpg?_=";
32-
33- public ScreenshotTrainingService ( IConfiguration configuration , ILoggerFactory loggerFactory )
37+ public ScreenshotTrainingService ( IConfiguration configuration , ILoggerFactory loggerFactory , IServiceProvider services )
3438 {
3539
3640 _CustomVisionKey = configuration [ "AzureServices:HatDetection:Key" ] ;
3741 _AzureEndpoint = configuration [ "AzureServices:HatDetection:CustomVisionEndpoint" ] ;
38- _TwitchChannel = configuration [ "StreamServices:Twitch:Channel" ] ;
3942 _AzureProjectId = Guid . Parse ( configuration [ "AzureServices:HatDetection:ProjectId" ] ) ;
4043 _Logger = loggerFactory . CreateLogger ( "ScreenshotTraining" ) ;
41-
44+ _Services = services ;
4245 }
4346
47+
4448 public Task StartAsync ( CancellationToken cancellationToken )
4549 {
4650
@@ -113,11 +117,12 @@ private async Task AddScreenshot(bool @internal)
113117 Endpoint = _AzureEndpoint
114118 } ;
115119
116- var result = await trainingClient . CreateImagesFromUrlsAsync ( _AzureProjectId , new ImageUrlCreateBatch (
117- new List < ImageUrlCreateEntry > {
118- new ImageUrlCreateEntry ( TwitchScreenshotUrl + Guid . NewGuid ( ) . ToString ( ) )
119- }
120- ) ) ;
120+ var imageStream = await GetScreenshotFromObs ( ) ;
121+ // TODO: If imageStream is null, handle gracefully
122+
123+ var result = await trainingClient . CreateImagesFromDataAsync ( _AzureProjectId ,
124+ imageStream
125+ ) ;
121126
122127 if ( ! result . IsBatchSuccessful && _RetryCount < 3 ) {
123128 _Logger . LogWarning ( $ "Error while adding screenshot #{ _TrainingCount } - trying again in 10 seconds") ;
@@ -144,11 +149,37 @@ private async Task AddScreenshot(bool @internal)
144149 }
145150 catch ( Exception ex )
146151 {
147- _Logger . LogError ( $ "Error while adding screenshot: { ex . Message } ") ;
152+
153+ _Logger . LogError ( $ "Error while adding screenshot: { ex . Message } ") ;
148154 }
149155
150156 }
151157
158+ internal async Task < Stream > GetScreenshotFromObs ( )
159+ {
160+
161+ Stream result = null ;
162+
163+ ScreenshotSink . Instance . ScreenshotReceived += ( obj , args ) =>
164+ {
165+ result = args . Screenshot ;
166+ } ;
167+
168+ using ( var scope = _Services . CreateScope ( ) )
169+ {
170+ var obsContext = scope . ServiceProvider . GetRequiredService < IHubContext < ObsHub , ITakeScreenshots > > ( ) ;
171+ await obsContext . Clients . All . TakeScreenshot ( ) ;
172+ }
173+ var i = 0 ;
174+ while ( result == null ) {
175+ await Task . Delay ( 100 ) ;
176+ i ++ ;
177+ if ( i >= 100 ) break ;
178+ }
179+
180+ return result ;
181+
182+ }
152183
153184 public Task AddScreenshot ( )
154185 {
0 commit comments