|
| 1 | +using OnnxStack.Core.Video; |
| 2 | +using OnnxStack.FeatureExtractor.Pipelines; |
| 3 | +using OnnxStack.StableDiffusion.Config; |
| 4 | +using OnnxStack.StableDiffusion.Enums; |
| 5 | +using OnnxStack.StableDiffusion.Pipelines; |
| 6 | + |
| 7 | +namespace OnnxStack.Console.Runner |
| 8 | +{ |
| 9 | + public sealed class VideoToVideoStreamExample : IExampleRunner |
| 10 | + { |
| 11 | + private readonly string _outputDirectory; |
| 12 | + private readonly StableDiffusionConfig _configuration; |
| 13 | + |
| 14 | + public VideoToVideoStreamExample(StableDiffusionConfig configuration) |
| 15 | + { |
| 16 | + _configuration = configuration; |
| 17 | + _outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(VideoToVideoStreamExample)); |
| 18 | + Directory.CreateDirectory(_outputDirectory); |
| 19 | + } |
| 20 | + |
| 21 | + public int Index => 4; |
| 22 | + |
| 23 | + public string Name => "Video To Video Stream Demo"; |
| 24 | + |
| 25 | + public string Description => "Video Stream Stable Diffusion Inference"; |
| 26 | + |
| 27 | + public async Task RunAsync() |
| 28 | + { |
| 29 | + |
| 30 | + // Read Video |
| 31 | + var videoFile = "C:\\Users\\Deven\\Pictures\\gidsgphy.gif"; |
| 32 | + var videoInfo = await VideoHelper.ReadVideoInfoAsync(videoFile); |
| 33 | + |
| 34 | + // Loop though the appsettings.json model sets |
| 35 | + foreach (var modelSet in _configuration.ModelSets) |
| 36 | + { |
| 37 | + OutputHelpers.WriteConsole($"Loading Model `{modelSet.Name}`...", ConsoleColor.Cyan); |
| 38 | + |
| 39 | + // Create Pipeline |
| 40 | + var pipeline = PipelineBase.CreatePipeline(modelSet); |
| 41 | + |
| 42 | + // Preload Models (optional) |
| 43 | + await pipeline.LoadAsync(); |
| 44 | + |
| 45 | + // Add text and video to prompt |
| 46 | + var promptOptions = new PromptOptions |
| 47 | + { |
| 48 | + Prompt = "Iron Man", |
| 49 | + DiffuserType = DiffuserType.ImageToImage |
| 50 | + }; |
| 51 | + |
| 52 | + |
| 53 | + // Create Video Stream |
| 54 | + var videoStream = VideoHelper.ReadVideoStreamAsync(videoFile, videoInfo.FrameRate); |
| 55 | + |
| 56 | + // Create Pipeline Stream |
| 57 | + var pipelineStream = pipeline.GenerateVideoStreamAsync(videoStream, promptOptions, progressCallback:OutputHelpers.ProgressCallback); |
| 58 | + |
| 59 | + // Write Video Stream |
| 60 | + await VideoHelper.WriteVideoStreamAsync(videoInfo, pipelineStream, Path.Combine(_outputDirectory, $"{modelSet.PipelineType}.mp4")); |
| 61 | + |
| 62 | + //Unload |
| 63 | + await pipeline.UnloadAsync(); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments