我試圖開發一個應用程序,可以通過網絡攝像頭檢測到人的上半身和下半身。我試着看emgu的臉部檢測並下載了「haarcascade_upperbody.xml」和「haarcascade_lowerbody.xml」。我試圖編碼與面部檢測相同的東西emgucv身體檢測使用hacascascade
但問題是,它不會檢測到我的身體,它不再是實時的。它延遲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;
}
你能明確表達出什麼問題嗎?至於「實時」,還不夠清楚。你的照片的分辨率是多少?每秒多少幀?它是否檢測到其他上身? –
問題是系統無法檢測到任何...我想繪製一個矩形上捕獲的上半身,但它不繪製...我所有的代碼都在上面陳述... – newbie07
你試過一個非常簡單這樣做之前測試?就像試圖檢測上身?因爲現在,你似乎試圖找到上半身,然後,如果你找到它,試着找到下半身。在一個圖像中發現這2個的機率非常低。要找人,你應該使用HoGDescriptor中的PeopleDetector。 –