我正在尝试选择并播放视频文件并使用“haar cascade”检测脸部。当我选择一个视频文件时,它会在此行显示异常“ArgumentException未处理”
int len =(int)memde.StreamLength;
这是我使用的代码:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
memde = new MediaDetClass();
System.IO.Directory.CreateDirectory("temp");
int len = (int)memde.StreamLength;
label1.Text = "Length: " + len.ToString();
trackBar1.Minimum = 0;
trackBar1.Maximum = len;
trackBar1.Value = 0;
counter = 0;
Image img;
memde.Filename = openFileDialog1.FileName;
memde.CurrentStream = 0;
float percent = 0.002f;
Image<Gray, byte> gray;
for (float i = 0.0f; i < len; i = i + (float)(percent * len))
{
counter++;
string fbitname = storagepath + counter.ToString();
memde.WriteBitmapBits(i, 850, 480, fbitname + ".bmp");
pictureBox1.Image = new Bitmap(fbitname);
img = Image.FromFile(fbitname + ".bmp");
img.Save(fbitname + ".bmp", ImageFormat.Bmp);
System.IO.File.Delete(i + fbitname + ".bmp");
Image<Bgr, Byte> image = new Image<Bgr, Byte>(new Bitmap(img));
gray = image.Convert<Gray, Byte>();
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
face,
1.2,
10,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(20, 20));
foreach (MCvAvgComp f in facesDetected[0])
{
result = image.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
image.Draw(f.rect, new Bgr(Color.Red), 2);
}
}
}
}
答案 0 :(得分:1)
您没有告诉MediaDetClass文件名,因此实例不知道它的长度。因此,在获取流的长度之前设置FileName属性。
memde = new MediaDetClass();
memde.Filename = openFileDialog1.FileName;
memde.CurrentStream = 0;
System.IO.Directory.CreateDirectory("temp");
int len = (int)memde.StreamLength;
label1.Text = "Length: " + len.ToString();
trackBar1.Minimum = 0;
trackBar1.Maximum = len;
trackBar1.Value = 0;
counter = 0;
Image img;
float percent = 0.002f;