Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 0d68062

Browse files
committed
Basic ControlNet console example
1 parent f2e3305 commit 0d68062

File tree

11 files changed

+98
-6
lines changed

11 files changed

+98
-6
lines changed

Assets/Samples/OpenPose.png

25.9 KB
Loading

OnnxStack.Console/AppService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public AppService(IServiceProvider serviceProvider, IEnumerable<Type> exampleRun
1212
_exampleRunners = exampleRunnerTypes
1313
.Select(serviceProvider.GetService)
1414
.Cast<IExampleRunner>()
15+
.OrderBy(x => x.Index)
1516
.ToList();
1617
}
1718

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

OnnxStack.Console/Examples/StableDebug.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public StableDebug(StableDiffusionConfig configuration)
1717
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDebug));
1818
}
1919

20+
public int Index => 0;
21+
2022
public string Name => "Stable Diffusion Debug";
2123

2224
public string Description => "Stable Diffusion Debugger";

OnnxStack.Console/Examples/StableDiffusionBatch.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public StableDiffusionBatch(StableDiffusionConfig configuration)
1919
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionBatch));
2020
}
2121

22+
public int Index => 2;
23+
2224
public string Name => "Stable Diffusion Batch Demo";
2325

2426
public string Description => "Creates a batch of images from the text prompt provided using all Scheduler types";

OnnxStack.Console/Examples/StableDiffusionExample.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public StableDiffusionExample(StableDiffusionConfig configuration)
1717
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionExample));
1818
}
1919

20+
public int Index => 1;
21+
2022
public string Name => "Stable Diffusion Demo";
2123

2224
public string Description => "Creates images from the text prompt provided using all Scheduler types";

OnnxStack.Console/Examples/StableDiffusionGenerator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public StableDiffusionGenerator(StableDiffusionConfig configuration)
2020
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionGenerator));
2121
}
2222

23+
public int Index => 3;
24+
2325
public string Name => "Stable Diffusion Generator";
2426

2527
public string Description => "Generator images from fixed prompts using all Scheduler types";

OnnxStack.Console/Examples/UpscaleExample.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public UpscaleExample(ImageUpscalerConfig configuration, IUpscaleService imageUp
2121
Directory.CreateDirectory(_outputDirectory);
2222
}
2323

24+
public int Index => 10;
25+
2426
public string Name => "Image Upscale Demo";
2527

2628
public string Description => "Upscales images";

OnnxStack.Console/Examples/VideoToVideoExample.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public VideoToVideoExample(StableDiffusionConfig configuration, IVideoService vi
2121
Directory.CreateDirectory(_outputDirectory);
2222
}
2323

24+
public int Index => 4;
25+
2426
public string Name => "Video To Video Demo";
2527

2628
public string Description => "Video Stable Diffusion Inference";

OnnxStack.Console/IExampleRunner.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace OnnxStack.Console
77
/// </summary>
88
internal interface IExampleRunner
99
{
10+
int Index { get; }
11+
1012
/// <summary>
1113
/// Gets the name of the Example.
1214
/// </summary>

0 commit comments

Comments
 (0)