|
| 1 | +using OnnxStack.Core.Image; |
| 2 | +using OnnxStack.FeatureExtractor.Pipelines; |
| 3 | +using OnnxStack.StableDiffusion.Config; |
| 4 | +using SixLabors.ImageSharp; |
| 5 | +using System.Diagnostics; |
| 6 | + |
| 7 | +namespace OnnxStack.Console.Runner |
| 8 | +{ |
| 9 | + public sealed class FeatureExtractorExample : IExampleRunner |
| 10 | + { |
| 11 | + private readonly string _outputDirectory; |
| 12 | + private readonly StableDiffusionConfig _configuration; |
| 13 | + |
| 14 | + public FeatureExtractorExample(StableDiffusionConfig configuration) |
| 15 | + { |
| 16 | + _configuration = configuration; |
| 17 | + _outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(FeatureExtractorExample)); |
| 18 | + Directory.CreateDirectory(_outputDirectory); |
| 19 | + } |
| 20 | + |
| 21 | + public int Index => 13; |
| 22 | + |
| 23 | + public string Name => "Feature Extractor Example"; |
| 24 | + |
| 25 | + public string Description => "Simple exmaple using ControlNet feature extractors"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// ControlNet Example |
| 29 | + /// </summary> |
| 30 | + public async Task RunAsync() |
| 31 | + { |
| 32 | + // Load Control Image |
| 33 | + var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp"); |
| 34 | + |
| 35 | + var pipelines = new[] |
| 36 | + { |
| 37 | + FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\canny.onnx"), |
| 38 | + FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\hed.onnx"), |
| 39 | + FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\depth.onnx", true), |
| 40 | + |
| 41 | + // FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\depth-anything-large-hf\\onnx\\model.onnx", normalize: true, sampleSize: 504), |
| 42 | + // FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\sentis-MiDaS\\dpt_beit_large_512.onnx", normalize: true, sampleSize: 384), |
| 43 | + }; |
| 44 | + |
| 45 | + foreach (var pipeline in pipelines) |
| 46 | + { |
| 47 | + var timestamp = Stopwatch.GetTimestamp(); |
| 48 | + OutputHelpers.WriteConsole($"Load pipeline`{pipeline.Name}`", ConsoleColor.Cyan); |
| 49 | + |
| 50 | + // Run Pipeline |
| 51 | + var imageFeature = await pipeline.RunAsync(inputImage); |
| 52 | + |
| 53 | + OutputHelpers.WriteConsole($"Generating image", ConsoleColor.Cyan); |
| 54 | + |
| 55 | + // Save Image |
| 56 | + await imageFeature.Image.SaveAsPngAsync(Path.Combine(_outputDirectory, $"{pipeline.Name}.png")); |
| 57 | + |
| 58 | + |
| 59 | + OutputHelpers.WriteConsole($"Unload pipeline", ConsoleColor.Cyan); |
| 60 | + |
| 61 | + //Unload |
| 62 | + await pipeline.UnloadAsync(); |
| 63 | + |
| 64 | + OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments