|
1 | 1 | using Microsoft.ML.OnnxRuntime.Tensors; |
2 | | -using OnnxStack.StableDiffusion.Config; |
3 | | -using OnnxStack.StableDiffusion.Results; |
| 2 | +using OnnxStack.StableDiffusion.Models; |
4 | 3 | using SixLabors.ImageSharp; |
5 | 4 | using SixLabors.ImageSharp.PixelFormats; |
6 | 5 | using SixLabors.ImageSharp.Processing; |
7 | 6 | using System; |
| 7 | +using System.IO; |
8 | 8 |
|
9 | 9 | namespace OnnxStack.StableDiffusion.Helpers |
10 | 10 | { |
11 | 11 | internal static class ImageHelpers |
12 | 12 | { |
13 | 13 | /// <summary> |
14 | | - /// Convert a Tensor to image. |
| 14 | + /// Converts to image. |
15 | 15 | /// </summary> |
16 | | - /// <param name="options">The options.</param> |
17 | 16 | /// <param name="imageTensor">The image tensor.</param> |
18 | 17 | /// <returns></returns> |
19 | | - public static ImageResult TensorToImage(SchedulerOptions options, DenseTensor<float> imageTensor) |
| 18 | + public static Image<Rgb24> ToImage(this DenseTensor<float> imageTensor) |
20 | 19 | { |
21 | | - var result = new Image<Rgba32>(options.Width, options.Height); |
22 | | - for (var y = 0; y < options.Height; y++) |
| 20 | + var height = imageTensor.Dimensions[2]; |
| 21 | + var width = imageTensor.Dimensions[3]; |
| 22 | + var result = new Image<Rgb24>(width, height); |
| 23 | + for (var y = 0; y < height; y++) |
23 | 24 | { |
24 | | - for (var x = 0; x < options.Width; x++) |
| 25 | + for (var x = 0; x < width; x++) |
25 | 26 | { |
26 | | - result[x, y] = new Rgba32( |
| 27 | + result[x, y] = new Rgb24( |
27 | 28 | CalculateByte(imageTensor, 0, y, x), |
28 | 29 | CalculateByte(imageTensor, 1, y, x), |
29 | 30 | CalculateByte(imageTensor, 2, y, x) |
30 | 31 | ); |
31 | 32 | } |
32 | 33 | } |
33 | | - return new ImageResult(result); |
| 34 | + return result; |
34 | 35 | } |
35 | 36 |
|
| 37 | + |
36 | 38 | /// <summary> |
37 | | - /// Converts an DenseTensor image to Image<Rgba32> |
| 39 | + /// Converts to image byte array. |
38 | 40 | /// </summary> |
39 | 41 | /// <param name="imageTensor">The image tensor.</param> |
40 | | - /// <param name="width">The width.</param> |
41 | | - /// <param name="height">The height.</param> |
42 | 42 | /// <returns></returns> |
43 | | - public static Image<Rgba32> TensorToImage(DenseTensor<float> imageTensor, int width, int height) |
| 43 | + public static byte[] ToImageBytes(this DenseTensor<float> imageTensor) |
44 | 44 | { |
45 | | - var image = new Image<Rgba32>(width, height); |
46 | | - for (var y = 0; y < height; y++) |
| 45 | + using (var image = imageTensor.ToImage()) |
| 46 | + using (var memoryStream = new MemoryStream()) |
47 | 47 | { |
48 | | - for (var x = 0; x < width; x++) |
49 | | - { |
50 | | - image[x, y] = new Rgba32( |
51 | | - CalculateByte(imageTensor, 0, y, x), |
52 | | - CalculateByte(imageTensor, 1, y, x), |
53 | | - CalculateByte(imageTensor, 2, y, x) |
54 | | - ); |
55 | | - } |
| 48 | + image.SaveAsPng(memoryStream); |
| 49 | + return memoryStream.ToArray(); |
56 | 50 | } |
57 | | - return image; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Converts to image memory stream. |
| 56 | + /// </summary> |
| 57 | + /// <param name="imageTensor">The image tensor.</param> |
| 58 | + /// <returns></returns> |
| 59 | + public static Stream ToImageStream(this DenseTensor<float> imageTensor) |
| 60 | + { |
| 61 | + using (var image = imageTensor.ToImage()) |
| 62 | + { |
| 63 | + var memoryStream = new MemoryStream(); |
| 64 | + image.SaveAsPng(memoryStream); |
| 65 | + return memoryStream; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public static DenseTensor<float> ToDenseTensor(this InputImage imageData, int width, int height) |
| 71 | + { |
| 72 | + if (!string.IsNullOrEmpty(imageData.ImagePath)) |
| 73 | + return TensorFromFile(imageData.ImagePath, width, height); |
| 74 | + if(imageData.ImageBytes != null) |
| 75 | + return TensorFromBytes(imageData.ImageBytes, width, height); |
| 76 | + if (imageData.ImageStream != null) |
| 77 | + return TensorFromStream(imageData.ImageStream, width, height); |
| 78 | + if (imageData.ToDenseTensor != null) |
| 79 | + return imageData.ImageTensor.ToDenseTensor(); // Note: Tensor Copy |
| 80 | + |
| 81 | + return null; |
58 | 82 | } |
59 | 83 |
|
60 | 84 |
|
@@ -111,31 +135,55 @@ public static void TensorToImageDebug(DenseTensor<float> imageTensor, int size, |
111 | 135 | /// <param name="width">The width.</param> |
112 | 136 | /// <param name="height">The height.</param> |
113 | 137 | /// <returns></returns> |
114 | | - public static DenseTensor<float> TensorFromImage(string filename, int width, int height) |
| 138 | + public static DenseTensor<float> TensorFromFile(string filename, int width, int height) |
115 | 139 | { |
116 | 140 | using (Image<Rgb24> image = Image.Load<Rgb24>(filename)) |
117 | 141 | { |
118 | 142 | Resize(image, width, height); |
119 | | - var imageArray = new DenseTensor<float>(new[] { 1, 3, width, height }); |
120 | | - image.ProcessPixelRows(img => |
121 | | - { |
122 | | - for (int x = 0; x < width; x++) |
123 | | - { |
124 | | - for (int y = 0; y < height; y++) |
125 | | - { |
126 | | - var pixelSpan = img.GetRowSpan(y); |
127 | | - imageArray[0, 0, y, x] = (pixelSpan[x].R / 255.0f) * 2.0f - 1.0f; |
128 | | - imageArray[0, 1, y, x] = (pixelSpan[x].G / 255.0f) * 2.0f - 1.0f; |
129 | | - imageArray[0, 2, y, x] = (pixelSpan[x].B / 255.0f) * 2.0f - 1.0f; |
130 | | - } |
131 | | - } |
132 | | - }); |
133 | | - return imageArray; |
| 143 | + return ProcessPixels(width, height, image); |
134 | 144 | } |
135 | 145 | } |
136 | 146 |
|
137 | 147 |
|
| 148 | + public static DenseTensor<float> TensorFromBytes(byte[] imageBytes, int width, int height) |
| 149 | + { |
| 150 | + using (var image = Image.Load<Rgb24>(imageBytes)) |
| 151 | + { |
| 152 | + Resize(image, width, height); |
| 153 | + return ProcessPixels(width, height, image); |
| 154 | + } |
| 155 | + } |
138 | 156 |
|
| 157 | + public static DenseTensor<float> TensorFromStream(Stream imageStream, int width, int height) |
| 158 | + { |
| 159 | + using (var image = Image.Load<Rgb24>(imageStream)) |
| 160 | + { |
| 161 | + Resize(image, width, height); |
| 162 | + return ProcessPixels(width, height, image); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + private static DenseTensor<float> ProcessPixels(int width, int height, Image<Rgb24> image) |
| 170 | + { |
| 171 | + var imageArray = new DenseTensor<float>(new[] { 1, 3, width, height }); |
| 172 | + image.ProcessPixelRows(img => |
| 173 | + { |
| 174 | + for (int x = 0; x < width; x++) |
| 175 | + { |
| 176 | + for (int y = 0; y < height; y++) |
| 177 | + { |
| 178 | + var pixelSpan = img.GetRowSpan(y); |
| 179 | + imageArray[0, 0, y, x] = (pixelSpan[x].R / 255.0f) * 2.0f - 1.0f; |
| 180 | + imageArray[0, 1, y, x] = (pixelSpan[x].G / 255.0f) * 2.0f - 1.0f; |
| 181 | + imageArray[0, 2, y, x] = (pixelSpan[x].B / 255.0f) * 2.0f - 1.0f; |
| 182 | + } |
| 183 | + } |
| 184 | + }); |
| 185 | + return imageArray; |
| 186 | + } |
139 | 187 |
|
140 | 188 |
|
141 | 189 | /// <summary> |
|
0 commit comments