-1

我正在开发一个带有面部检测功能的 mp3 播放器。我有两种形式。

第一种形式是媒体播放器和面部检测代码,第二种形式是用于创建播放列表的代码。

我希望当我单击创建按钮时,播放列表的名称应该显示在第一种形式的列表框中。

这是我对 form1 的一些代码

 private Capture cap;
  public Form1()
    {
        InitializeComponent();

        cap = new Capture(0);
        _dataBasePath = Directory.GetCurrentDirectory() + @"\db1.mdb";


        // adjust path to find your xml

        haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        mouth = new HaarCascade("Mouth.xml");

        lefteye = new HaarCascade("eye_left.xml");
        righteye = new HaarCascade("haarcascade_righteye_2splits.xml");
    }

    public void DataBasePath(string path)
    {
        _dataBasePath = path;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
        {
            if (nextFrame != null)
            {
                // there's only one channel (greyscale), hence the zero index
                //var faces = nextFrame.DetectHaarCascade(haar)[0];
                Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
                Image<Gray, Byte> gray = nextFrame.Convert<Gray, Byte>();
                Image<Gray, Byte> gray1 = nextFrame.Convert<Gray, Byte>();
                Image<Gray, Byte> gray2 = nextFrame.Convert<Gray, Byte>();
                var faces = grayframe.DetectHaarCascade(
                         haar, 1.4, 4,
                         HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                         new Size(nextFrame.Width / 8, nextFrame.Height / 8)
                         )[0];
                foreach (MCvAvgComp f in faces)
                {
                    //draw the face detected in the 0th (gray) channel with blue color
                    nextFrame.Draw(f.rect, new Bgr(Color.Blue), 2);
                    facesnap = f.rect;


                    int halfheight = facesnap.Height/2;
                    int start = facesnap.X;
                    int start1 = facesnap.Y;

                    Rectangle top = new Rectangle(start,start1,facesnap.Width,halfheight);
                    int start2 = top.Bottom;

                    Rectangle bottom = new Rectangle(start, start2, facesnap.Width, halfheight);


                    //Set the region of interest on the faces
                    gray.ROI = bottom;
                    MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth,
                                                    1.1, 10,
                                                  Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                                    new Size(40,30));
                    gray.ROI = Rectangle.Empty;


                    foreach (MCvAvgComp m in mouthsDetected[0])
                    {
                        Rectangle mouthRect = m.rect;
                        mouthRect.Offset(bottom.X, bottom.Y);
                        nextFrame.Draw(mouthRect, new Bgr(Color.Red), 2);
                        Rectangle mouthphoto = new Rectangle(mouthRect.X - 5, mouthRect.Y - 10, mouthRect.Width + 5, mouthRect.Height +10);
                        detectedmouth = mouthphoto;
                    }
                    int halfwidth =facesnap.Width/2;

                    Rectangle toprighteye =new Rectangle(start,start1,halfwidth,halfheight);
                    int leftx = toprighteye.Right;
                    Rectangle toplefteye = new Rectangle(leftx, start1, halfwidth, halfheight);

                    gray1.ROI =toplefteye;

                    MCvAvgComp[][] detectedlefteye = gray1.DetectHaarCascade(lefteye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30,10));
                    gray1.ROI = Rectangle.Empty;

                    foreach (MCvAvgComp eyesnapleft in detectedlefteye[0])
                    {
                        Rectangle eyeRectleft = eyesnapleft.rect;
                        eyeRectleft.Offset(toplefteye.X, toplefteye.Y);
                        nextFrame.Draw(eyeRectleft, new Bgr(Color.Green), 2);

                        lefteyesnap = eyeRectleft;


                    }

                    gray2.ROI = toprighteye;

                    MCvAvgComp[][] detectedrighteye = gray2.DetectHaarCascade(righteye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30, 10));
                    gray2.ROI = Rectangle.Empty;

                    foreach (MCvAvgComp eyesnapright in detectedrighteye[0])
                    {
                        Rectangle eyeRectright = eyesnapright.rect;
                        eyeRectright.Offset(toprighteye.X, toprighteye.Y);
                        nextFrame.Draw(eyeRectright, new Bgr(Color.Yellow), 2);

                        righteyesnap = eyeRectright;


                    }




                }


                pictureBox1.Image = nextFrame.ToBitmap();
            }
        }

    }
 private void Form1_Load(object sender, EventArgs e)
    {
        _dataBasePath = Directory.GetCurrentDirectory() + @"\db1.mdb";


        // adjust path to find your xml

        haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        mouth = new HaarCascade("Mouth.xml");

        lefteye = new HaarCascade("eye_left.xml");
        righteye = new HaarCascade("haarcascade_righteye_2splits.xml");
    }

这是form2的一些代码

namespace Face_Detection_Concept
{
    public partial class Form2 : Form
{
    #region Global Variables

    public const string Separator = ",";

    public static ArrayList searchAudio = new ArrayList();
    public static ArrayList searchVideo = new ArrayList();



    public static StreamWriter sw;
    public string fnm;
    #endregion

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        // Set default values
        cmbType.SelectedIndex = 0;
        chkIncSubDir.Checked = true;

        LoadFormats();


        txtFilename.Text = "PlayList1.wpl";
    }

当我使用此代码在按钮单击事件上访问 form1 的列表框时

new Form1().playlistviewbar.items.add(playlistname);

但这不显示播放列表的名称。有什么解决办法吗?

4

3 回答 3

1

虽然您可以通过 make in 直接公开控件public,但更好的方法是创建操作ListBox内容的方法。这使您可以灵活地更改控件而不会破坏调用表单:

public void AddPlayList(string playlistname)
{
    this.playlistviewbar.Items.Add(playlistname);
}
public string[] GetPlayLists()
{
    return this.playlistviewbar.Items;  // converting to strings if necessary.
}
于 2013-02-27T14:40:45.560 回答
0

您的Capture对象cap为空,您应该在启动计时器之前对其进行初始化,例如:

public void form_load()
    {

        cap = new Capture();

        //Init and start Timer

    }
于 2013-02-27T14:21:18.570 回答
0

将 ListBox 的 modifiers 属性设置为 public,您可以通过已知方式访问它。

将调用Form的引用传递给被调用的Form,并带着它访问LB。

// Call:
Form2 test = new Form2(this);

//Constructor Form2:
public Form2 (Form1 FormToAccess)

//Access:
FormToAccess.listBox1.... = ... ;
于 2013-02-27T14:35:41.897 回答