我如何在没有I / O的情况下使用dlib人脸检测来处理字节数组中的数据?

问题描述 投票:0回答:1

我已经使用了面部检测代码:

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;

但是我想通过删除IO部分来加快速度:

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

我曾尝试使用LoadImageData,Bitmap.ToArray2D,ToBytes和其他选项,但随后程序无法检测到面部或无法获得真实的字节数。我应该如何使用它来获得正确的结果?

c# .net-core dlib
1个回答
0
投票
//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);
© www.soinside.com 2019 - 2024. All rights reserved.