Git Product home page Git Product logo

Comments (15)

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

Not support to load image data from Stream.
But DlibDotNet.Extensions supports to load from Bitmap.
https://github.com/takuya-takeuchi/DlibDotNet/tree/master/src/DlibDotNet.Extensions

However, these days. I did not take care of it and it may be include issues.
If you face issue, please let me know.

from dlibdotnet.

zhuxb711 avatar zhuxb711 commented on May 26, 2024

It seems that DlibDotNet.Extenstions could not be used in UWP ?

from dlibdotnet.

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

Yes.
For now, it support WinForms and WPF's Bitmap.

So you have to convert UWP writeablebitmap to byte array.

from dlibdotnet.

zhuxb711 avatar zhuxb711 commented on May 26, 2024

Hi ! I make a DlibDotNet.Extenstions by using .NET Core , However, it still don't work beacuse
In DlibDotNet.Extenstions, the WriteableBitmap is
namespace:

"System.Windows.Media.Imaging"

But in UWP, the WriteableBitmap is
namespace:

"Windows.UI.Xaml.Media.Imaging"

UWP could only use "Windows.UI.Xaml.Media.Imaging" rather than "System.Windows.Media.Imaging"

In short , DlibDotNet.Extentions could not convert WriteableBitmap in UWP to Array2D properly

So, How could I use Dlib to process the camera frame in UWP without save and read Image file frequently. The I/O cost too much

Thanks.

from dlibdotnet.

zhuxb711 avatar zhuxb711 commented on May 26, 2024

I tried every method that I thought.
In UWP, convert WriteableBitmap(parameter name "writeableBitmap") to byte[] (parameter name "ByteArray")

Use this method:
Dlib.LoadImageData<RgbPixel>(ByteArray , writeableBitmap.PixelHeight , writeableBitmap.PixelWidth , 1) to load image from WriteableBitmap.
Result:"No faces detected".

Then I tried to overload your methods Dlib.LoadImage(byte [] str)
Removed some parts of your methods such as File.Exist() and get byte[] from parameter str directly
Result: SEHException

Oh, I don't want to do this : CameraImage->SaveImage-> Dlib.LoadImage(string path) LoadImage->FaceDetect

What I expect is : CameraImage->Load Image in Memory(Without Disk I/O)->FaceDetect

Please help me .Thank you very much.

from dlibdotnet.

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

How dou you convert WPF WriteableBitmap to byte[]?
If you get Array2d from WriteableBitmap, you can check whether image is valid by using ImageWindow.

And If you can, please show sample code and sample image.

from dlibdotnet.

zhuxb711 avatar zhuxb711 commented on May 26, 2024

Sample Code Like this
temp is the Image that need to process

SoftwareBitmap temp;
WriteableBitmap wb = new WriteableBitmap(temp.PixelWidth, temp.PixelHeight);
temp.CopyToBuffer(wb.PixelBuffer);
var img = Dlib.LoadImageData<RgbPixel>(wb.ToByteArray(), (uint)wb.PixelHeight, (uint)wb.PixelWidth, 3);
ImageWindow im = new ImageWindow(img);
im.WaitUntilClosed();

ImageWindow show this:
image

image
WriteableBitmapEX provide the method to convert WriteableBitmap to byte[]

Tips: WriteableBitmap in UWP is different from WPF
By the way, I'm not quite understand "steps" parameter in
LoadImageData<T>(byte[] data, uint rows, uint columns, uint steps)

from dlibdotnet.

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

Thank you for your good report.
I'm in office now.
please wait untill I go home and check code :)

from dlibdotnet.

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

I'm sorry for delay response.

The last argument of LoadImageData should be specify horizontal byte length.
In the most case, it could be channel x width.
But windows bitmap data requires to be the width of a single row of pixels (a scan line), rounded up to a four-byte boundary.
In this case, the step is 32 if width is 10 and channle is 3.

stride = width * channel
if (stride % 4 != 0)
    stride = stride  + (stride  % 4);

from dlibdotnet.

zhuxb711 avatar zhuxb711 commented on May 26, 2024

Here is the result
Because WriteableBitmap in UWP is RGBA8, So the Image has 4 channels
if (stride % 4 != 0) Always False
And we can see the result is gray and separate
image

Thanks for your great work, but it still need to improve
What do you think ?

from dlibdotnet.

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

Ok. I guess library may not make a correct interpretation of image.

I do not expect different type buffer is passed for type argument.

Perhaps, you convert rgba to rgb before you use LoadImageData.
It is work around.

Basically I think I should provide convert api.

from dlibdotnet.

zhuxb711 avatar zhuxb711 commented on May 26, 2024

In UWP, WriteableBitmap is not easy to Convert ARGB to RGB.
It seems that every Bitmap in UWP is ARGB
I hope DotNetDlib could provide some method or overload so that we could easily use Image in memory rather than in hard disk

from dlibdotnet.

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

FYI. I was able to reproduce issues you faced on not UWP but WinFroms. This issue is not only for UWP.

image

using (var bitmap = new Bitmap("Lenna.png"))
{
    Console.WriteLine(bitmap.PixelFormat);

    var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
    try
    {
        var array = new byte[data.Stride * data.Height];
        Marshal.Copy(data.Scan0, array, 0, array.Length);
        using (var array2D1 = Dlib.LoadImageData<RgbPixel>(array, (uint)bitmap.Height, (uint)bitmap.Width, (uint)data.Stride))
        using (var window1 = new ImageWindow(array2D1))
        using (var array2D2 = Dlib.LoadImage<RgbAlphaPixel>("Lenna.png"))
        using (var window2 = new ImageWindow(array2D2))
        {
            window1.WaitUntilClosed();
            window2.WaitUntilClosed();
        }
    }
    finally
    {
        bitmap.UnlockBits(data);
    }
}

Edit

Pixles of PixelFormat.32bppArgb are alinged as the following:

[BGRA][BGRA[[BGRA]...

If convert above to RGB,

[BGR][ABG][RAB][GRA]...

from dlibdotnet.

takuya-takeuchi avatar takuya-takeuchi commented on May 26, 2024

Added new overload of LoadImageData. Commit is fd00056 on develop branch.

Array2D<T> LoadImageData<T>(ImagePixelFormat format, byte[] data, uint rows, uint columns, uint steps)

The format can be Bgr, Bgra, Rgb or Rgba. It means the pixel format of data argument.
So you can treat Rgba value as Rgb as the following.

[BGRA][BGRA[[BGRA]...

to

[RGB][RGB][RGB][RGB]...

And the next is sample code.

image

using (var bitmap = new Bitmap("Lenna.png"))
{
    Console.WriteLine(bitmap.PixelFormat);

    var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
    try
    {
        var array = new byte[data.Stride * data.Height];
        Marshal.Copy(data.Scan0, array, 0, array.Length);
        using (var array2D1 = Dlib.LoadImageData<RgbPixel>(array, (uint)bitmap.Height, (uint)bitmap.Width, (uint)data.Stride))
        using (var window1 = new ImageWindow(array2D1))
        using (var array2D2 = Dlib.LoadImage<RgbAlphaPixel>("Lenna.png"))
        using (var window2 = new ImageWindow(array2D2))
        using (var array2D3 = Dlib.LoadImageData<RgbPixel>(ImagePixelFormat.Bgra, array, (uint)bitmap.Height, (uint)bitmap.Width, (uint)data.Stride))
        using (var window3 = new ImageWindow(array2D3))
        using (var array2D4 = Dlib.LoadImageData<RgbAlphaPixel>(ImagePixelFormat.Bgra, array, (uint)bitmap.Height, (uint)bitmap.Width, (uint)data.Stride))
        using (var window4 = new ImageWindow(array2D4))
        {
            window1.Title = "window1";
            window2.Title = "window2";
            window3.Title = "window3";
            window4.Title = "window4";
            window1.WaitUntilClosed();
            window2.WaitUntilClosed();
            window3.WaitUntilClosed();
            window4.WaitUntilClosed();
        }
    }
    finally
    {
        bitmap.UnlockBits(data);
    }
}

from dlibdotnet.

zhuxb711 avatar zhuxb711 commented on May 26, 2024

Sorry late,
I'm very appreciate your improvment
It helps me a lot !
Thank you very much

from dlibdotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.