takuya-takeuchi / UltraFaceDotNet

C# version of Ultra-Light-Fast-Generic-Face-Detector-1MB for Windows, MacOS, Linux, iOS and Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to output to byte array?

dev-ptdi opened this issue · comments

I am new to UltraFaceDotNet, how to input and output from/to byte array?

using (var ultraFace = UltraFace.Create(param))// config model input,
{
using var frame = Cv2.ImRead(_jpg); // --> I need from byte array

                using var inMat = NcnnDotNet.Mat.FromPixels(frame.Data, NcnnDotNet.PixelType.Bgr2Rgb, frame.Cols, frame.Rows);

                var faceInfos = ultraFace.Detect(inMat).ToArray();
                for (var j = 0; j < faceInfos.Length; j++)
                {
                    var face = faceInfos[j];
                    var pt1 = new Point<float>(face.X1, face.Y1);
                    var pt2 = new Point<float>(face.X2, face.Y2);
                    Cv2.Rectangle(frame, pt1, pt2, new Scalar<double>(0, 255, 0), 2);
                }

               Cv2.ImShow("UltraFace", frame);
                Cv2.WaitKey();
                Cv2.ImWrite("result.jpg";, frame);   // -- HOW to output to byte array?
            }

@dev-ptdi
frame.Data is IntPtr indicate pixel data. You can copy data from it by Marshal.Copy.

I don't think we can use frame.Data with Marshal.Copy since we have to know the array size before we can copy to byte[]

                byte[] managedArray = new byte[size];   // Here we don;t know the size
                Marshal.Copy(frame.Data, managedArray, 0, size);

if we generate and use temporary file to read / save then delete the file, it would be an IO issue

@dev-ptdi

var total = frame.Total();
byte[] managedArray = new byte[total ];
Marshal.Copy(frame.Data, managedArray, 0, total );

NcnnDotNet 0.0.0.20210417 is now release and it support convert to Mat from byte[]