takuya-takeuchi / DlibDotNet

Dlib .NET wrapper written in C++ and C# for Windows, MacOS, Linux and iOS
MIT License
491 stars 135 forks source link

When will DlibDotNet.Extensions that support WriteableBitmap be released? #204

Closed kaki104 closed 4 years ago

kaki104 commented 4 years ago

Summary of your issue

DlibDotNet.Extensions 19.18.0.20200428 I installed nuget package, but it does not support WriteableBitmap. Doesn't the current version support WriteableBitmap? Can I know when it will be distributed?

Environment

Windows 10 version 2004 Visual Studio 2019 16.6.5 UWP Target version Windows 10, version 2004

What did you do when you faced the problem?

I want to do Face Detecting by converting the image file to Array2D in the UWP app.

Example code:

                using(var stream = await file.OpenAsync(FileAccessMode.Read))
                {
                    var decoder = await BitmapDecoder.CreateAsync(stream);
                    var bmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
                    bmp.SetSource(stream);

                    var array2d = bmp.ToArray2D<byte>();
                    Dlib.PyramidUp(array2d);
                    var detects = detector.Operator(array2d);
                    if (detects.Any() == false) return;
                }

Output:

Severity Code Description Project File Line Suppression State Error CS1929 'WriteableBitmap' does not contain a definition for 'ToArray2D' and the best extension method overload 'BitmapExtensions.ToArray2D(Bitmap)' requires a receiver of type 'Bitmap' PMSample C:\Users\kaki1\source\repos\PhotoManager\PMSample\MainPage.xaml.cs 73 Active

What did you intend to be?

face detection

takuya-takeuchi commented 4 years ago

@kaki104 The concept of DlibDotNet is cross platform library. WriteableBitmap is only there in Windows platform.

kaki104 commented 4 years ago

@takuya-takeuchi

What is the Windows Platform talking about? Isn't it referring to the Universal Windows Platform?

The method of reading the file in the folder selected by the user from the code used above is only possible by opening and using IStorageFile as a stream. So, it cannot be used with the method used in the example https://github.com/takuya-takeuchi/DlibDotNet/blob/master/examples/UWP/FaceDetection/MainPage.xaml.cs.

this._Result = Dlib.LoadImage<RgbPixel>("lenna.bmp");

So how do I use it in UWP apps?

takuya-takeuchi commented 4 years ago

@kaki104

What is the Windows Platform talking about?

I talked about 'WriteableBitmap' does not contain a definition for 'ToArray2D'. For a while ago, I striped out support of WriteableBitmap from DlibDotNet.Extensions nuget package. Because WriteableBitmap is not supported in Linux and OSX.

However, you can use ToArray2D by building source of DlibDotNet.Extensions.

  1. Remove/Comment out
  2. Build it
  3. Link DlibDotNet.Extensions to your project

Or you can use PyramidUp by following this steps (not test)

// bmp is bgr rather than bgra
 var width = bmp.PixelWidth;
var height = bmp.PixelHeight;
 var buffer = bmp.BackBuffer;
 var stride = bmp.BackBufferStride;
var array = byte[stride * height];
Marshal.Copy(bufffer, array); // pseudo
// Array2D does not have constructor to accept byte arrray
var matrix = new Matrix<BgrPixel>(array, height, int width , int 3);
Dlib.PyramidUp(matrix );
var detects = detector.Operator(matrix);
kaki104 commented 4 years ago

@takuya-takeuchi

The first method you've informed seems to be a build error. The second method was not available because the bmp.BackBuffer, bmp.BackBufferStride properties were missing.

I found a way to solve the problem elsewhere. https://www.bountysource.com/issues/73661016-add-new-method-to-dlib-which-used-to-load-data-from-softwarebitmap

        using (var stream = await file.OpenAsync(FileAccessMode.Read))
        {
            var decoder = await BitmapDecoder.CreateAsync(stream);
            var sb = await decoder.GetSoftwareBitmapAsync();
            _result = LoadFromSoftwareBitmap(sb);
            _loadedMatrix = new Matrix<RgbPixel>(_result);
        }

Thank you so much