emgu自动将无法识别的面添加到数据库

时间:2014-06-26 18:06:02

标签: c# emgucv face-recognition

我想从以下代码中做的是更改button2单击事件以自动将无法识别的面添加到数据库,因此无需按下按钮。我尝试了各种想法,但大多数都返回错误。可以使用否定识别结果来运行button2 Click事件。

private void button2_Click(object sender, System.EventArgs e)
{
    try
    {

        //Trained face counter
        ContTrain = ContTrain + 1;

        //Get a gray frame from capture device
        gray = grabber.QueryGrayFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

        //Face Detector
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
        face,
        1.2,
        10,
        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
        new Size(20, 20));

        //Action for each element detected
        foreach (MCvAvgComp f in facesDetected[0])
        {

            TrainedFace = currentFrame.Copy(f.rect).Convert<Gray, byte>();



            break;
        }

        //resize face detected image for force to compare the same size with the 
        //test image with cubic interpolation type method
        TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
        named = DateTime.Now.ToString("dd-MM-yy HH:mm:ss:ms");
        TrainedFace.Save(Application.StartupPath + "/Temp/face1.bmp");

        string dated = DateTime.Now.ToString("HH:mm:ss:ff dd-MM-yy");
        label4.Text = dated;

        //Show face added in gray scale
        imageBox1.Image = TrainedFace;
        trainingImages.Add(TrainedFace);
        labels.Add(label4.Text);

        byte[] imagepic = null;
        FileStream fsstream = new FileStream(Application.StartupPath + "/Temp/face1.bmp", FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fsstream);
        imagepic = br.ReadBytes((int)fsstream.Length);



        string myConnection = "datasource=sql4.freemysqlhosting.net;port=3306;user=sql434250;password=lE3!lQ5*";
        MySqlConnection myConn = new MySqlConnection(myConnection);
        MySqlCommand SelectCommand = new MySqlCommand("INSERT INTO `sql434250`.`facialid` (`id`, `timeanddate`, `photo1`) VALUES (NULL, @dated, @IMG);", myConn);
        MySqlDataReader myReader;


        myConn.Open();
        SelectCommand.Parameters.Add(new MySqlParameter("@IMG", imagepic));
        SelectCommand.Parameters.Add(new MySqlParameter("@dated", dated));
        myReader = SelectCommand.ExecuteReader();


        while (myReader.Read())
        {
        }
        Refresh();

    }
    catch (MySqlException ee)
    {

        MessageBox.Show(e.ToString());
        int errorcode = ee.Number;
    }

}




void FrameGrabber(object sender, EventArgs e)
{
    label3.Text = "0";
    //label4.Text = "";
    NamePersons.Add("");


    //Get the current frame form capture device
    currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

    //Convert it to Grayscale
    gray = currentFrame.Convert<Gray, Byte>();

    //Face Detector
    MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
        face,
        1.2,
        10,
        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
        new Size(20, 20));

    //Action for each element detected
    foreach (MCvAvgComp f in facesDetected[0])
    {
        t = t + 1;
        result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
        //draw the face detected in the 0th (gray) channel with blue color
        currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);


        if (trainingImages.ToArray().Length != 0)
        {
            //TermCriteria for face recognition with numbers of trained images like maxIteration
            MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);

            //Eigen face recognizer
            EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
                trainingImages.ToArray(),
                labels.ToArray(),
                3000,
                ref termCrit);

            name = recognizer.Recognize(result);

            //Draw the label for each face detected and recognized
            currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));

        }

        NamePersons[t - 1] = name;
        NamePersons.Add("");


        //Set the number of faces detected on the scene
        label3.Text = facesDetected[0].Length.ToString();

    }
    t = 0;

    //Names concatenation of persons recognized
    for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
    {
        names = names + NamePersons[nnn] + ", ";
    }
    //Show the faces procesed and recognized
    imageBoxFrameGrabber.Image = currentFrame;


    //Clear the list(vector) of names
    NamePersons.Clear();

}

0 个答案:

没有答案