I'm not familiar with the library you're using, but if dets
is a collection of detected face rectangles, you can probably use something like this:
var dets = faceDetector.Operator(image);
if (dets.Count() > 1)
{
MessageBox.Show("Too many faces! Why are there so many faces? I can't look. Please make it stop.");
return;
}
else
{
var g = dets.First();
Dlib.DrawRectangle(image, g, new RgbPixel { Green = 255 }, thickness: 10);
var result = image.ToBitmap();
this.pictureBox1.Invoke(new Action(() =>
{
this.pictureBox1.Image?.Dispose();
this.pictureBox1.Image = result;
}));
}
Note that Count()
and First()
are extension methods from System.Linq
so you'll need to make sure there's a using System.Linq;
directive at the top of your code file.
Also, the Invoke
code is probably better moved to the BackgroundWorker's OnRunWorkerCompleted
event (where the cross-thread invoke will no longer be needed) and you can access the PictureBox directly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…