44using OnnxStack . Core . Image ;
55using OnnxStack . Core . Model ;
66using OnnxStack . Core . Services ;
7+ using OnnxStack . Core . Video ;
78using OnnxStack . ImageUpscaler . Config ;
89using OnnxStack . ImageUpscaler . Extensions ;
910using OnnxStack . ImageUpscaler . Models ;
1011using OnnxStack . StableDiffusion . Config ;
1112using SixLabors . ImageSharp ;
1213using SixLabors . ImageSharp . PixelFormats ;
1314using SixLabors . ImageSharp . Processing ;
14- using System ;
1515using System . Collections . Generic ;
1616using System . IO ;
1717using System . Linq ;
@@ -23,17 +23,19 @@ public class UpscaleService : IUpscaleService
2323 {
2424 private readonly IOnnxModelService _modelService ;
2525 private readonly ImageUpscalerConfig _configuration ;
26+ private readonly IVideoService _videoService ;
2627
2728 /// <summary>
2829 /// Initializes a new instance of the <see cref="UpscaleService"/> class.
2930 /// </summary>
3031 /// <param name="configuration">The configuration.</param>
3132 /// <param name="modelService">The model service.</param>
3233 /// <param name="imageService">The image service.</param>
33- public UpscaleService ( ImageUpscalerConfig configuration , IOnnxModelService modelService )
34+ public UpscaleService ( ImageUpscalerConfig configuration , IOnnxModelService modelService , IVideoService videoService )
3435 {
3536 _configuration = configuration ;
3637 _modelService = modelService ;
38+ _videoService = videoService ;
3739 }
3840
3941
@@ -129,6 +131,63 @@ public async Task<Stream> GenerateAsStreamAsync(UpscaleModelSet modelOptions, In
129131 image . SaveAsPng ( memoryStream ) ;
130132 return memoryStream ;
131133 }
134+
135+
136+ /// <summary>
137+ /// Generates the upscaled video.
138+ /// </summary>
139+ /// <param name="modelOptions">The model options.</param>
140+ /// <param name="videoInput">The video input.</param>
141+ /// <returns></returns>
142+ public async Task < DenseTensor < float > > GenerateAsync ( UpscaleModelSet modelOptions , VideoInput videoInput )
143+ {
144+ DenseTensor < float > output = default ;
145+ var videoInfo = await _videoService . GetVideoInfoAsync ( videoInput ) ;
146+ var videoFrames = await _videoService . CreateFramesAsync ( videoInput , videoInfo . FPS ) ;
147+ foreach ( var frame in videoFrames . Frames )
148+ {
149+ var image = await GenerateInternalAsync ( modelOptions , new InputImage ( frame ) ) ;
150+ output = output . Concatenate ( image . ToDenseTensor ( new [ ] { 1 , 3 , image . Height , image . Width } ) ) ;
151+ }
152+ return output ;
153+ }
154+
155+
156+ /// <summary>
157+ /// Generates the upscaled video.
158+ /// </summary>
159+ /// <param name="modelOptions">The model options.</param>
160+ /// <param name="videoInput">The video input.</param>
161+ /// <returns></returns>
162+ public async Task < byte [ ] > GenerateAsByteAsync ( UpscaleModelSet modelOptions , VideoInput videoInput )
163+ {
164+ List < byte [ ] > output = new List < byte [ ] > ( ) ;
165+ var videoInfo = await _videoService . GetVideoInfoAsync ( videoInput ) ;
166+ var videoFrames = await _videoService . CreateFramesAsync ( videoInput , videoInfo . FPS ) ;
167+ foreach ( var frame in videoFrames . Frames )
168+ {
169+
170+ var image = await GenerateInternalAsync ( modelOptions , new InputImage ( frame ) ) ;
171+ var ms = new MemoryStream ( ) ;
172+ await image . SaveAsPngAsync ( ms ) ;
173+ output . Add ( ms . ToArray ( ) ) ;
174+ }
175+
176+ var videoResult = await _videoService . CreateVideoAsync ( output , videoInfo . FPS ) ;
177+ return videoResult . Data ;
178+ }
179+
180+
181+ /// <summary>
182+ /// Generates the upscaled video.
183+ /// </summary>
184+ /// <param name="modelOptions">The model options.</param>
185+ /// <param name="videoInput">The video input.</param>
186+ /// <returns></returns>
187+ public async Task < Stream > GenerateAsStreamAsync ( UpscaleModelSet modelOptions , VideoInput videoInput )
188+ {
189+ return new MemoryStream ( await GenerateAsByteAsync ( modelOptions , videoInput ) ) ;
190+ }
132191
133192
134193 /// <summary>
@@ -167,6 +226,25 @@ private async Task<Image<Rgba32>> GenerateInternalAsync(UpscaleModelSet modelSet
167226 }
168227
169228
229+ /// <summary>
230+ /// Generates an upscaled video of the source provided.
231+ /// </summary>
232+ /// <param name="modelOptions">The model options.</param>
233+ /// <param name="videoInput">The video input.</param>
234+ /// <returns></returns>
235+ public async Task < IEnumerable < Image < Rgba32 > > > GenerateInternalAsync ( UpscaleModelSet modelOptions , VideoInput videoInput )
236+ {
237+ var output = new List < Image < Rgba32 > > ( ) ;
238+ var videoInfo = await _videoService . GetVideoInfoAsync ( videoInput ) ;
239+ var videoFrames = await _videoService . CreateFramesAsync ( videoInput , videoInfo . FPS ) ;
240+ foreach ( var frame in videoFrames . Frames )
241+ {
242+ output . Add ( await GenerateInternalAsync ( modelOptions , new InputImage ( frame ) ) ) ;
243+ }
244+ return output ;
245+ }
246+
247+
170248 /// <summary>
171249 /// Creates the input parameters.
172250 /// </summary>
@@ -181,6 +259,5 @@ private UpscaleInput CreateInputParams(Image<Rgba32> imageSource, int maxTileSiz
181259 var height = imageSource . Height * scaleFactor ;
182260 return new UpscaleInput ( tiles , width , height ) ;
183261 }
184-
185262 }
186263}
0 commit comments