Skip to content

Commit 8b08aae

Browse files
committed
Updated to load training image in batch
1 parent 09cf5ab commit 8b08aae

File tree

6 files changed

+58
-18
lines changed

6 files changed

+58
-18
lines changed

Fritz.Chatbot/Commands/PredictHatCommand.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ public PredictHatCommand(IConfiguration configuration, ScreenshotTrainingService
3838
_Repository = repository;
3939
}
4040

41-
public string TwitchScreenshotUrl => $"https://static-cdn.jtvnw.net/previews-ttv/live_user_{_TwitchChannel}-1280x720.jpg?_=";
42-
43-
4441
public async Task Execute(IChatService chatService, string userName, ReadOnlyMemory<char> rhs)
4542
{
4643

@@ -51,7 +48,7 @@ public async Task Execute(IChatService chatService, string userName, ReadOnlyMem
5148
var client = new CustomVisionPredictionClient()
5249
{
5350
ApiKey = _CustomVisionKey,
54-
Endpoint = _AzureEndpoint
51+
Endpoint = _AzureEndpoint,
5552
};
5653

5754
var obsImage = await _TrainHat.GetScreenshotFromObs();
@@ -75,19 +72,20 @@ public async Task Execute(IChatService chatService, string userName, ReadOnlyMem
7572

7673
}
7774

75+
if (DateTime.UtcNow.Subtract(result.Created).TotalSeconds > Cooldown.Value.TotalSeconds) {
76+
await chatService.SendMessageAsync($"I previously predicted this hat about {DateTime.UtcNow.Subtract(result.Created).TotalSeconds} seconds ago");
77+
}
7878

7979
var bestMatch = result.Predictions.OrderByDescending(p => p.Probability).FirstOrDefault();
80-
if (bestMatch == null || bestMatch.Probability <= 0.3D) {
80+
if (bestMatch == null || bestMatch.Probability < 0.7D) {
8181
await chatService.SendMessageAsync("csharpAngry 404 Hat Not Found! Let's ask a moderator to !addhat so we can identify it next time");
8282
// do we store the image?
8383
return;
8484
}
8585

8686
await chatService.SendMessageAsync($"csharpClip I think (with {bestMatch.Probability.ToString("0.0%")} certainty) Jeff is currently wearing his {bestMatch.TagName} hat csharpClip");
87-
if (bestMatch.Probability >= 0.6D) {
88-
var desc = await _Repository.GetDescription(bestMatch.TagName);
89-
if (!string.IsNullOrEmpty(desc)) await chatService.SendMessageAsync(desc);
90-
}
87+
var desc = await _Repository.GetDescription(bestMatch.TagName);
88+
if (!string.IsNullOrEmpty(desc)) await chatService.SendMessageAsync(desc);
9189

9290
}
9391

Fritz.Chatbot/Commands/TrainHatCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task Execute(IChatService chatService, string userName, bool isMode
2626
if (!(isModerator || isBroadcaster)) return;
2727

2828
_TrainHat.StartTraining();
29-
await chatService.SendMessageAsync("Started taking screenshots, 1 every ten seconds for the next 150 seconds");
29+
await chatService.SendMessageAsync($"Started taking screenshots, 1 every {ScreenshotTrainingService.TrainingIntervalInSeconds} seconds for the next {ScreenshotTrainingService.TrainingIntervalInSeconds * ScreenshotTrainingService.TrainingCount} seconds");
3030

3131
}
3232

Fritz.Chatbot/ScreenshotTrainingService.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.ComponentModel.DataAnnotations;
1313
using System.IO;
1414
using System.Linq;
15+
using System.Net.Http;
1516
using System.Text;
1617
using System.Threading;
1718
using System.Threading.Tasks;
@@ -20,6 +21,8 @@ namespace Fritz.Chatbot
2021
{
2122
public class ScreenshotTrainingService : IHostedService, ITrainHat
2223
{
24+
public const int TrainingCount = 15;
25+
public const int TrainingIntervalInSeconds = 2;
2326

2427
// TODO: Track how many images are loaded -- 5k is the maximum for the FREE service
2528
private string _CustomVisionKey = "";
@@ -32,8 +35,11 @@ public class ScreenshotTrainingService : IHostedService, ITrainHat
3235
private bool _CurrentlyTraining = false;
3336
private byte _TrainingCount = 0;
3437
private Task _TrainingTask;
38+
private byte _TotalPictures = 0;
3539
private byte _RetryCount = 0;
3640

41+
private readonly Queue<MemoryStream> _ImagesToUpload = new Queue<MemoryStream>();
42+
3743
public ScreenshotTrainingService(IConfiguration configuration, ILoggerFactory loggerFactory, IServiceProvider services)
3844
{
3945

@@ -69,17 +75,25 @@ private async Task Train(CancellationToken token)
6975
while (!token.IsCancellationRequested)
7076
{
7177

72-
if (_CurrentlyTraining && _TrainingCount == 15)
78+
if (_CurrentlyTraining && _TotalPictures == TrainingCount)
7379
{
7480
_CurrentlyTraining = false;
7581
_Logger.LogTrace("Completed screenshot training");
82+
await UploadCachedScreenshots();
83+
if (!_CurrentlyTraining) _TotalPictures = 0;
7684

7785
}
7886
else if (_CurrentlyTraining)
7987
{
8088

81-
await AddScreenshot(true);
82-
await Task.Delay(TimeSpan.FromSeconds(10));
89+
if (_ImagesToUpload.Count == 5) {
90+
await UploadCachedScreenshots();
91+
}
92+
93+
var imageStream = await GetScreenshotFromObs();
94+
_TotalPictures++;
95+
_ImagesToUpload.Enqueue((MemoryStream)imageStream);
96+
await Task.Delay(TimeSpan.FromSeconds(TrainingIntervalInSeconds));
8397

8498
}
8599
else
@@ -93,6 +107,36 @@ private async Task Train(CancellationToken token)
93107

94108
}
95109

110+
private async Task UploadCachedScreenshots()
111+
{
112+
113+
if (!_ImagesToUpload.Any()) return;
114+
115+
var trainingClient = new CustomVisionTrainingClient()
116+
{
117+
ApiKey = _CustomVisionKey,
118+
Endpoint = _AzureEndpoint
119+
};
120+
121+
var listToLoad = new List<ImageFileCreateEntry>();
122+
while (_ImagesToUpload.Any())
123+
{
124+
var imgStream = _ImagesToUpload.Dequeue();
125+
listToLoad.Add(new ImageFileCreateEntry(contents: imgStream.ToArray()));
126+
}
127+
128+
var result = await trainingClient.CreateImagesFromFilesWithHttpMessagesAsync(_AzureProjectId, new ImageFileCreateBatch()
129+
{
130+
Images = listToLoad
131+
});
132+
133+
_TotalPictures -= (byte)result.Body.Images.Where(r => r.Status != "OK").Count();
134+
if (_TotalPictures >= TrainingCount) _CurrentlyTraining = true;
135+
136+
Console.WriteLine(result.ToString());
137+
138+
}
139+
96140
public void StartTraining()
97141
{
98142

Fritz.ObsProxy/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"ImageFolderOrg": "C:\\dev\\stream\\Screenshots",
1111
"BotUrl": "http://localhost:62574/obshub",
1212
"ObsIpAddress": "127.0.0.1:4444",
13-
"CameraSource": "Webcam-ChromaKey",
13+
"CameraSource": "Razer Kiyo",
1414
"ObsTest": "false"
1515
}

Fritz.StreamTools/StartupServices/ConfigureServices.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ private static void RegisterGitHubServices(IServiceCollection services, IConfigu
104104
services.AddHostedService<GitHubService>();
105105

106106
services.AddSingleton<ScreenshotTrainingService>();
107-
//var provider = services.BuildServiceProvider();
108-
//var svc = provider.GetRequiredService<ScreenshotTrainingService>();
109107
services.AddHostedService<ScreenshotTrainingService>(s => s.GetRequiredService<ScreenshotTrainingService>());
110108
services.AddTransient<HatDescriptionRepository>();
111109
}

Fritz.StreamTools/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@
162162
},
163163
{
164164
"command": "coffee",
165-
"response": "Jeff drinks Madrinas coffee - Use code: FRITZ to receive 20% off of your order at https://www.madrinascoffee.com"
165+
"response": "HTTP 418 - Jeff drinks Madrinas coffee - Use code: FRITZ to receive 20% off of your order at https://www.madrinascoffee.com"
166166
},
167167
{
168168
"command": "madrinas",
169-
"response": "Madrinas coffee - Fuel for the grind, fuel for your mind. Use code: FRITZ to receive 20% off of your order at https://www.madrinascoffee.com"
169+
"response": "HTTP 418 - Madrinas coffee - Fuel for the grind, fuel for your mind. Use code: FRITZ to receive 20% off of your order at https://www.madrinascoffee.com"
170170
},
171171
{
172172
"command": "defend",

0 commit comments

Comments
 (0)