Esempio n. 1
0
        private void btnSaveBP_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "XML文件(*.xml)|*.xml";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                bp.Write(new FileStorage(sfd.FileName, FileStorage.Mode.Write));
                MessageBox.Show("导出完成");
            }
        }
Esempio n. 2
0
        private void annTraining()
        {
            string       finalOutput = "";
            int          features    = 16;
            int          classes     = 26;
            Matrix <int> layers      = new Matrix <int>(6, 1);

            layers[0, 0] = features;
            layers[1, 0] = classes * 16;
            layers[2, 0] = classes * 8;
            layers[3, 0] = classes * 4;
            layers[4, 0] = classes * 2;
            layers[5, 0] = classes;

            FileStorage fileStorageRead = new FileStorage(@"ANN_Model.xml", FileStorage.Mode.Read);

            ann.Read(fileStorageRead.GetRoot(0));
            ann.SetLayerSizes(layers);
            ann.SetActivationFunction(ANN_MLP.AnnMlpActivationFunction.SigmoidSym, 0, 0);
            ann.SetTrainMethod(ANN_MLP.AnnMlpTrainMethod.Backprop, 0, 0);
            ann.Train(allFeatureOfSample, DataLayoutType.RowSample, annAllResponse);

            FileStorage fileStorageWrite = new FileStorage(@"ANN_Model.xml", FileStorage.Mode.Write);

            ann.Write(fileStorageWrite);

            Matrix <float> testSample = new Matrix <float>(1, 16);

            for (int q = 0; q < 16; q++)
            {
                testSample[0, q] = allFeatureOfSample[12, q];
            }
            float real = ann.Predict(testSample);

            finalOutput += labelArray[(int)real];
            label5.Text  = finalOutput.ToString();
            SpeechSynthesizer reader1 = new SpeechSynthesizer();


            if (label5.Text != " ")
            {
                reader1.Dispose();
                reader1 = new SpeechSynthesizer();
                reader1.SpeakAsync(finalOutput.ToString());
            }
            else
            {
                MessageBox.Show("No Text Present!");
            }

            System.IO.File.WriteAllText(@"ANNResult.txt", real.ToString());
        }
        bool Save(string filename)
        {
            FileStorage fs = new FileStorage(filename, FileStorage.Mode.Write);

            if (!fs.IsOpened)
            {
                return(false);
            }

            ANN_MLP network = new ANN_MLP();

            network.Write(fs);
            fs.Write("values" + values);
            fs.ReleaseAndGetString();
            return(true);
        }
        private async void moduleFeatureExtraction(int first, int last)
        {
            string fghfh = "";

            double[,] RawData = new double[16, 3780];
            int mid  = (first + last) / 2;
            int low  = mid - 8;;
            int high = mid + 8;

            for (int i = 0; i < 16; i++)
            {
                for (int j = 0; j < 26; j++)
                {
                    if (j == adasas)
                    {
                        response[i, j] = 1;
                    }
                    if (j != adasas)
                    {
                        response[i, j] = 0;
                    }
                }
            }
            adasas++;
            if (low < first)
            {
                low++;
            }
            if (high > last)
            {
                low++;
            }
            int length = high - low;

            for (int k = (low); k < (high); k++)
            {
                string            frameName             = "gesture//" + k + ".jpeg";
                Image <Bgr, byte> featurExtractionInput = new Image <Bgr, byte>(frameName);
                //pictureBox3.Image = featurExtractionInput.Bitmap;
                //label4.Text = k.ToString();
                await Task.Delay(1000 / Convert.ToInt32(2));

                float[] desc = new float[3780];
                desc = GetVector(featurExtractionInput);

                int i = k - (low);
                for (int j = 0; j < 3780; j++)
                {
                    double val = Convert.ToDouble(desc[j]);
                    RawData.SetValue(val, i, j);
                }

                if (k == (high - 1))
                {
                    Matrix <Double> DataMatrix   = new Matrix <Double>(RawData);
                    Matrix <Double> Mean         = new Matrix <Double>(1, 3780);
                    Matrix <Double> EigenValues  = new Matrix <Double>(1, 3780);
                    Matrix <Double> EigenVectors = new Matrix <Double>(3780, 3780);
                    CvInvoke.PCACompute(DataMatrix, Mean, EigenVectors, 16);
                    Matrix <Double> result = new Matrix <Double>(16, 16);
                    CvInvoke.PCAProject(DataMatrix, Mean, EigenVectors, result);


                    String        filePath = @"test.xml";
                    StringBuilder sb       = new StringBuilder();
                    (new XmlSerializer(typeof(Matrix <double>))).Serialize(new StringWriter(sb), result);
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(sb.ToString());

                    System.IO.File.WriteAllText(filePath, sb.ToString());
                    Matrix <double> matrix = (Matrix <double>)(new XmlSerializer(typeof(Matrix <double>))).Deserialize(new XmlNodeReader(xDoc));

                    string djf = null;
                    djf  = System.IO.File.ReadAllText(@"g.txt");
                    djf += Environment.NewLine;
                    djf += Environment.NewLine;
                    for (int p = 0; p < 16; p++)
                    {
                        for (int q = 0; q < 16; q++)
                        {
                            djf += p + " , " + q + "  " + matrix[p, q].ToString() + "    ";
                        }
                        djf += Environment.NewLine;
                    }
                    Matrix <float> masjhdb   = result.Convert <float>();
                    TrainData      trainData = new TrainData(masjhdb, DataLayoutType.RowSample, response);
                    int            features  = 16;
                    int            classes   = 26;
                    Matrix <int>   layers    = new Matrix <int>(6, 1);
                    layers[0, 0] = features;
                    layers[1, 0] = classes * 16;
                    layers[2, 0] = classes * 8;
                    layers[3, 0] = classes * 4;
                    layers[4, 0] = classes * 2;
                    layers[5, 0] = classes;
                    ANN_MLP     ann             = new ANN_MLP();
                    FileStorage fileStorageRead = new FileStorage(@"abc.xml", FileStorage.Mode.Read);
                    ann.Read(fileStorageRead.GetRoot(0));
                    ann.SetLayerSizes(layers);
                    ann.SetActivationFunction(ANN_MLP.AnnMlpActivationFunction.SigmoidSym, 0, 0);
                    ann.SetTrainMethod(ANN_MLP.AnnMlpTrainMethod.Backprop, 0, 0);
                    ann.Train(masjhdb, DataLayoutType.RowSample, response);
                    FileStorage fileStorageWrite = new FileStorage(@"abc.xml", FileStorage.Mode.Write);
                    ann.Write(fileStorageWrite);
                    Matrix <float> hehe = new Matrix <float>(1, 16);
                    for (int q = 0; q < 16; q++)
                    {
                        hehe[0, q] = masjhdb[11, q];
                    }
                    float real = ann.Predict(hehe);

                    fghfh += array[(int)real];
                    SpeechSynthesizer reader = new SpeechSynthesizer();

                    if (richTextBox1.Text != " ")
                    {
                        reader.Dispose();
                        reader = new SpeechSynthesizer();
                        reader.SpeakAsync(fghfh.ToString());
                    }
                    else
                    {
                        MessageBox.Show("No Text Present!");
                    }
                    richTextBox1.Text = fghfh.ToString();
                    System.IO.File.WriteAllText(@"g.txt", real.ToString());
                }
            }
        }