我正在尝试开发一个应用程序,它可以通过网络摄像头检测人的上半身和下半身。我尝试查看 emgu 的人脸检测并下载了“haarcascade_upperbody.xml”和“haarcascade_lowerbody.xml”。我尝试编写与给出的面部检测相同的代码
但问题是它不会检测到我的身体,也不再是实时的。延迟3秒?
这是我的代码。我希望有一个人可以帮助我:
private void ProcessFrame(object sender, EventArgs arg)
{
Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
FittingBox.Image = ImageFrame;
long detectionTime;
List<Rectangle> upper = new List<Rectangle>();
List<Rectangle> lower = new List<Rectangle>();
Detect(ImageFrame,"haarcascade_upperbody.xml","haarcascade_lowerbody.xml",upper,lower,out detectionTime);
foreach (Rectangle up in upper)
ImageFrame.Draw(up, new Bgr(Color.Red), 2);
foreach (Rectangle low in lower)
ImageFrame.Draw(low, new Bgr(Color.Blue), 2);
}
public static void Detect(Image<Bgr, Byte> image, String upperFileName, String lowerFileName, List<Rectangle> upperbody, List<Rectangle> lowerbody, out long detectionTime)
{
Stopwatch watch;
if (GpuInvoke.HasCuda)
{
using (GpuCascadeClassifier upper = new GpuCascadeClassifier(upperFileName))
using (GpuCascadeClassifier lower = new GpuCascadeClassifier(lowerFileName))
{
watch = Stopwatch.StartNew();
using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
{
Rectangle[] upperRegion = upper.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
upperbody.AddRange(upperRegion);
foreach (Rectangle f in upperRegion)
{
using (GpuImage<Gray, Byte> upperImg = gpuGray.GetSubRect(f))
{
using (GpuImage<Gray, Byte> clone = upperImg.Clone())
{
Rectangle[] lowerRegion = lower.DetectMultiScale(clone, 1.1, 10, Size.Empty);
foreach (Rectangle e in lowerRegion)
{
Rectangle lowerRect = e;
lowerRect.Offset(f.X, f.Y);
lowerbody.Add(lowerRect);
}
}
}
}
}
watch.Stop();
}
}
else
{
using (CascadeClassifier upper = new CascadeClassifier(upperFileName))
using (CascadeClassifier lower = new CascadeClassifier(lowerFileName))
{
watch = Stopwatch.StartNew();
using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>())
{
gray._EqualizeHist();
Rectangle[] upperDeteced = upper.DetectMultiScale(
gray,
1.1,
10,
new Size(50, 50),
Size.Empty);
foreach (Rectangle f in upperDeteced)
{
gray.ROI = f;
Rectangle[] lowerDetected = lower.DetectMultiScale(
gray,
1.1,
10,
new Size(50, 50),
Size.Empty);
gray.ROI = Rectangle.Empty;
foreach (Rectangle e in lowerDetected)
{
Rectangle lowerRect = e;
lowerRect.Offset(f.X, f.Y);
lowerbody.Add(lowerRect);
}
}
}
watch.Stop();
}
}
detectionTime = watch.ElapsedMilliseconds;
}