|
| 1 | +using OnnxStack.Core.Image; |
| 2 | +using OnnxStack.StableDiffusion.Config; |
| 3 | +using OnnxStack.StableDiffusion.Enums; |
| 4 | +using OnnxStack.StableDiffusion.Models; |
| 5 | +using OnnxStack.StableDiffusion.Pipelines; |
| 6 | +using SixLabors.ImageSharp; |
| 7 | + |
| 8 | +namespace OnnxStack.Console.Runner |
| 9 | +{ |
| 10 | + public sealed class ControlNetExample : IExampleRunner |
| 11 | + { |
| 12 | + private readonly string _outputDirectory; |
| 13 | + private readonly StableDiffusionConfig _configuration; |
| 14 | + |
| 15 | + public ControlNetExample(StableDiffusionConfig configuration) |
| 16 | + { |
| 17 | + _configuration = configuration; |
| 18 | + _outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(ControlNetExample)); |
| 19 | + Directory.CreateDirectory(_outputDirectory); |
| 20 | + } |
| 21 | + |
| 22 | + public int Index => 11; |
| 23 | + |
| 24 | + public string Name => "ControlNet Example"; |
| 25 | + |
| 26 | + public string Description => "ControlNet Example"; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// ControlNet Example |
| 30 | + /// </summary> |
| 31 | + public async Task RunAsync() |
| 32 | + { |
| 33 | + // Load Control Image |
| 34 | + var controlImage = await File.ReadAllBytesAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\OpenPose.png"); |
| 35 | + |
| 36 | + // Create ControlNet |
| 37 | + var controlNet = ControlNetModel.Create("D:\\Repositories\\controlnet_onnx\\controlnet\\openpose.onnx"); |
| 38 | + |
| 39 | + // Create Pipeline |
| 40 | + var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable_diffusion_onnx", ModelType.ControlNet); |
| 41 | + |
| 42 | + // Prompt |
| 43 | + var promptOptions = new PromptOptions |
| 44 | + { |
| 45 | + Prompt = "Stormtrooper flexing", |
| 46 | + DiffuserType = DiffuserType.ControlNet, |
| 47 | + InputContolImage = new InputImage(controlImage) |
| 48 | + }; |
| 49 | + |
| 50 | + // Run pipeline |
| 51 | + var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet); |
| 52 | + |
| 53 | + // Create Image from Tensor result |
| 54 | + var image = result.ToImage(); |
| 55 | + |
| 56 | + // Save Image File |
| 57 | + var outputFilename = Path.Combine(_outputDirectory, $"Output.png"); |
| 58 | + await image.SaveAsPngAsync(outputFilename); |
| 59 | + |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments