简体   繁体   中英

How can I use dlib face detection without I/O on data saveload from byte array?

I have worked face detection code:

var bytes = Convert.FromBase64String(image);
File.WriteAllBytes("testin.jpg", bytes);
var dmgreal = Dlib.LoadImage<RgbPixel>("testin.jpg");

using (var fd = Dlib.GetFrontalFaceDetector())
{
    var faces = fd.Operator(dmgreal);
    foreach (var face in faces)
    {
        Dlib.DrawRectangle(dmgreal, face, color: new RgbPixel(0, 255, 255), thickness: 4);
    }
}

Dlib.SaveJpeg(dmgreal, "testout.jpg");
var imageArray = File.ReadAllBytes("testout.jpg");                     
var nImage = Convert.ToBase64String(imageArray, 0, imageArray.Length);
CoordStreamHub.WorkItem = "data:image/jpg;base64," + nImage;

But I want to speed up this by deleteing IO parts:

File.WriteAllBytes("testin.jpg", bytes);
var dmgreal = Dlib.LoadImage<RgbPixel>("testin.jpg");
Dlib.SaveJpeg(dmgreal, "testout.jpg");
var imageArray = File.ReadAllBytes("testout.jpg");       

I've tried to use LoadImageData, Bitmap.ToArray2D, ToBytes and other options, but then program doesn't detect faces or doesn't get me true bytes. How should I use its to get right result?

//var img = Dlib.LoadImage<RgbPixel>(inputFilePath);

Bitmap bitmap = (Bitmap)Bitmap.FromFile(inputFilePath);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData data = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);

var array = new byte[data.Stride * data.Height];
Marshal.Copy(data.Scan0, array, 0, array.Length);

Array2D<BgrPixel> img = Dlib.LoadImageData<BgrPixel>(array, (uint)bitmap.Height, (uint)bitmap.Width, (uint)data.Stride);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM