FaceONNX / FaceONNX

Face recognition and analytics library based on deep neural networks and ONNX runtime
MIT License
201 stars 36 forks source link

Using SkiaSharp for imageing but not System.Drawing.Common for linux support #16

Closed bnuzhouwei closed 10 months ago

bnuzhouwei commented 1 year ago

System.Drawing is not suit for linux, for libgdiplus has memory leak problem in linux, and have been removed since .net7.

asiryan commented 1 year ago

Thank you very much for your report! You are absolutely right. I think that a little later I will transfer the stack to another core for working with images

songshizhao commented 1 year ago

sys.drawing namespace also can't be used in UWP (not supported), use bytes or stream is better, I think.

asiryan commented 10 months ago

@bnuzhouwei , @songshizhao you can use float[][,] methods

https://github.com/FaceONNX/FaceONNX/blob/9f1fd201d9801dcad20a68f4c67f3545b1faaccc/netstandard/FaceONNX/face/classes/FaceDetector.cs#L91

Webreaper commented 9 months ago

Any chance you could update the examples to use this new method? Then it would be properly cross-platform. I'm going to use ImageSharp to load the bitmap, but it would be great if there was an example showing either ImageSharp or SkiaSharp bitmaps.

Misaka12456 commented 4 months ago

Any chance you could update the examples to use this new method? Then it would be properly cross-platform. I'm going to use ImageSharp to load the bitmap, but it would be great if there was an example showing either ImageSharp or SkiaSharp bitmaps.

I asked ChatGPT and it gave me this example:

using SkiaSharp;
using System.Numerics;

public float[][,] ConvertSKBitmapToFloatArray(SKBitmap bitmap)
{
    // Ensure the bitmap is in BGR8888 format
    if (bitmap.ColorType != SKColorType.Rgb && bitmap.AlphaType != SKAlphaType.Opaque)
    {
        bitmap = bitmap.Copy(SKColorType.Rgb, SKAlphaType.Opaque);
    }

    // Lock pixels to get access to pixel data
    using (var info = bitmap.PeekPixels())
    {
        if (info == null)
        {
            throw new InvalidOperationException("Failed to peek pixels from bitmap.");
        }

        var bytes = info.GetPixels();
        var width = bitmap.Width;
        var height = bitmap.Height;

        // Convert byte array to float array
        var floatArray = new float[3, height, width];

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                var index = (y * width + x) * 4; // Each pixel is 4 bytes
                var b = bytes[index];
                var g = bytes[index + 1];
                var r = bytes[index + 2];

                // Normalize values
                floatArray[0, y, x] = b / 255.0f; // Blue
                floatArray[1, y, x] = g / 255.0f; // Green
                floatArray[2, y, x] = r / 255.0f; // Red
            }
        }

        return floatArray;
    }
}

Maybe helpful?

asiryan commented 4 months ago

Any chance you could update the examples to use this new method? Then it would be properly cross-platform. I'm going to use ImageSharp to load the bitmap, but it would be great if there was an example showing either ImageSharp or SkiaSharp bitmaps.

@Webreaper , @Misaka12456 or you can try something like this https://github.com/FaceONNX/FaceONNX/blob/main/netstandard/Examples/FaceEmbeddingsClassification/Program.cs

Webreaper commented 4 months ago

I already submitted a PR (https://github.com/FaceONNX/FaceONNX/pull/22) which fixes this and makes it cross-platform.

Misaka12456 commented 4 months ago

I already submitted a PR (#22) which fixes this and makes it cross-platform.

I actually use it in Emgu.CV Mat, but I didn't find a best solution... @asiryan Do you have any other example?

asiryan commented 4 months ago

I already submitted a PR (#22) which fixes this and makes it cross-platform.

I actually use it in Emgu.CV Mat, but I didn't find a best solution... @asiryan Do you have any other example?

Do you need an example with Emgu.CV?

Misaka12456 commented 4 months ago

yeah.... planned to use faceonnx for a real-time face detection

asiryan commented 4 months ago

@Misaka12456 I think you can find information about images and their transformations here https://www.emgu.com/wiki/index.php/Working_with_Images

Misaka12456 commented 4 months ago

@Misaka12456 I think you can find information about images and their transformations here https://www.emgu.com/wiki/index.php/Working_with_Images

thx