Exemple #1
0
        /// <summary>
        /// This menu command allows the user to select a top-level directory in which subfolders are assumed to
        /// represent gestures from one subject, and subsubfolders represent gestures of a given speed. A single
        /// output file for the entire directory structure is made.
        /// </summary>
        /// <param name="sender">The sender of this event.</param>
        /// <param name="e">The arguments for this event.</param>
        /// <remarks>
        /// The top-level directory can have any name, e.g., "logs", and its subdirectories are assumed to represent
        /// individual subjects, e.g., "s01", "s02", "s03", etc. Within each subject, the directories are assumed to
        /// represent speeds, e.g., "fast", "medium", and "slow". In reality, these two tokens (the subject name and
        /// speed name) are just used to form the first two columns of the output, so they can be any string whatsoever.
        ///
        /// The gesture files themselves loaded must conform to a naming convention where each example of a particular gesture
        /// is named with the same string, followed by a numerical identifier for that example. As in:
        ///
        ///     circle01.xml        // circle gestures
        ///     circle02.xml
        ///     circle03.xml
        ///     square01.xml        // square gestures
        ///     square02.xml
        ///     square03.xml
        ///     triangle01.xml      // triangle gestures
        ///     triangle02.xml
        ///     triangle03.xml
        ///
        /// The same number of examples should be read in for each gesture category. The testing procedure will load a
        /// random subset of each gesture and test on the remaining gestures.
        ///
        /// <b>Warning.</b> This process will throw an exception if the number of gesture examples for each gesture is
        /// unbalanced.
        /// </remarks>
        private void TestDirectories_Click(object sender, EventArgs e)
        {
            FoldersForm frm = new FoldersForm();

            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                dlg.Description  = "Select top-level folder.";
                dlg.SelectedPath = Directory.GetCurrentDirectory();

                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    string       path    = String.Format("{0}\\{1}__{2}.txt", dlg.SelectedPath, dlg.SelectedPath.Substring(dlg.SelectedPath.LastIndexOf('\\') + 1), Environment.TickCount);
                    StreamWriter outfile = new StreamWriter(path, true, Encoding.UTF8);

                    string[] subjectDirectories = Directory.GetDirectories(dlg.SelectedPath);
                    for (int i = 0; i < subjectDirectories.Length; i++)
                    {
                        string[] speedDirectories = Directory.GetDirectories(subjectDirectories[i]);
                        for (int j = 0; j < speedDirectories.Length; j++)
                        {
                            prgTesting.Visible  = true;
                            lblRecognizing.Text = String.Format("Processing \"{0}\" ({1}/{2}) in \"{3}\" ({4}/{5}).",
                                                                speedDirectories[j].Substring(speedDirectories[j].LastIndexOf('\\') + 1),
                                                                j + 1,
                                                                speedDirectories.Length,
                                                                subjectDirectories[i].Substring(subjectDirectories[i].LastIndexOf('\\') + 1),
                                                                i + 1,
                                                                subjectDirectories.Length
                                                                );
                            lblRecognizing.Visible = true;
                            Application.DoEvents();

                            // Each slot in the list contains a gesture Category, which contains a list of gesture prototypes.
                            string[]        filenames  = Directory.GetFiles(speedDirectories[j]);
                            List <Category> categories = _rec.AssembleBatch(filenames);
                            if (categories != null)
                            {
                                string[] rstr = _rec.TestBatch(
                                    subjectDirectories[i].Substring(subjectDirectories[i].LastIndexOf('\\') + 1),
                                    speedDirectories[j].Substring(speedDirectories[j].LastIndexOf('\\') + 1),
                                    categories,
                                    dlg.SelectedPath,
                                    _protractor
                                    );
                                if (rstr != null)
                                {
                                    SystemSounds.Question.Play(); // success
                                    // now append the file at rstr[0] to the outfile
                                    StreamReader r    = new StreamReader(rstr[0], Encoding.UTF8);
                                    string       line = "tmp";
                                    while ((line = r.ReadLine()) != String.Empty)
                                    {
                                    }
                                    ;
                                    while ((line = r.ReadLine()) == String.Empty)
                                    {
                                    }
                                    ;
                                    while (line != String.Empty)
                                    {
                                        outfile.WriteLine(line);
                                        line = r.ReadLine();
                                    }
                                    r.Close();
                                    File.Delete(rstr[0]);
                                    File.Delete(rstr[1]);
                                }
                                else // failure
                                {
                                    string msg = String.Format("There was an error in the current log.");
                                    MessageBox.Show(this, msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                            else // error assembling batch
                            {
                                MessageBox.Show(this, "Unreadable files, or unbalanced number of gestures in categories.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            lblRecognizing.Visible = false;
                            prgTesting.Visible     = false;
                        }
                    }

                    outfile.Close();
                    lblRecognizing.Text = "Recognizing...";
                }
            }
        }
        /// <summary>
        /// This menu command allows the user to select a top-level directory in which subfolders are assumed to 
        /// represent gestures from one subject, and subsubfolders represent gestures of a given speed. A single
        /// output file for the entire directory structure is made.
        /// </summary>
        /// <param name="sender">The sender of this event.</param>
        /// <param name="e">The arguments for this event.</param>
        /// <remarks>
        /// The top-level directory can have any name, e.g., "logs", and its subdirectories are assumed to represent
        /// individual subjects, e.g., "s01", "s02", "s03", etc. Within each subject, the directories are assumed to
        /// represent speeds, e.g., "fast", "medium", and "slow". In reality, these two tokens (the subject name and 
        /// speed name) are just used to form the first two columns of the output, so they can be any string whatsoever.
        /// 
        /// The gesture files themselves loaded must conform to a naming convention where each example of a particular gesture 
        /// is named with the same string, followed by a numerical identifier for that example. As in:
        ///
        ///     circle01.xml        // circle gestures
        ///     circle02.xml
        ///     circle03.xml
        ///     square01.xml        // square gestures
        ///     square02.xml
        ///     square03.xml
        ///     triangle01.xml      // triangle gestures
        ///     triangle02.xml
        ///     triangle03.xml
        ///
        /// The same number of examples should be read in for each gesture category. The testing procedure will load a 
        /// random subset of each gesture and test on the remaining gestures.
        /// 
        /// <b>Warning.</b> This process will throw an exception if the number of gesture examples for each gesture is 
        /// unbalanced.
        /// </remarks>
        private void TestDirectories_Click(object sender, EventArgs e)
        {
            FoldersForm frm = new FoldersForm();
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                dlg.Description = "Select top-level folder.";
                dlg.SelectedPath = Directory.GetCurrentDirectory();

                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    string path = String.Format("{0}\\{1}__{2}.txt", dlg.SelectedPath, dlg.SelectedPath.Substring(dlg.SelectedPath.LastIndexOf('\\') + 1), Environment.TickCount);
                    StreamWriter outfile = new StreamWriter(path, true, Encoding.UTF8);

                    string[] subjectDirectories = Directory.GetDirectories(dlg.SelectedPath);
                    for (int i = 0; i < subjectDirectories.Length; i++)
                    {
                        string[] speedDirectories = Directory.GetDirectories(subjectDirectories[i]);
                        for (int j = 0; j < speedDirectories.Length; j++)
                        {
                            prgTesting.Visible = true;
                            lblRecognizing.Text = String.Format("Processing \"{0}\" ({1}/{2}) in \"{3}\" ({4}/{5}).", 
                                speedDirectories[j].Substring(speedDirectories[j].LastIndexOf('\\') + 1), 
                                j + 1, 
                                speedDirectories.Length, 
                                subjectDirectories[i].Substring(subjectDirectories[i].LastIndexOf('\\') + 1), 
                                i + 1, 
                                subjectDirectories.Length
                                );
                            lblRecognizing.Visible = true;
                            Application.DoEvents();

                            // Each slot in the list contains a gesture Category, which contains a list of gesture prototypes.
                            string[] filenames = Directory.GetFiles(speedDirectories[j]);
                            List<Category> categories = _rec.AssembleBatch(filenames);
                            if (categories != null)
                            {
                                string[] rstr = _rec.TestBatch(
                                    subjectDirectories[i].Substring(subjectDirectories[i].LastIndexOf('\\') + 1), 
                                    speedDirectories[j].Substring(speedDirectories[j].LastIndexOf('\\') + 1), 
                                    categories, 
                                    dlg.SelectedPath,
                                    _protractor
                                    );
                                if (rstr != null)
                                {
                                    SystemSounds.Question.Play(); // success
                                    // now append the file at rstr[0] to the outfile
                                    StreamReader r = new StreamReader(rstr[0], Encoding.UTF8);
                                    string line = "tmp";
                                    while ((line = r.ReadLine()) != String.Empty) { };
                                    while ((line = r.ReadLine()) == String.Empty) { };
                                    while (line != String.Empty)
                                    {
                                        outfile.WriteLine(line);
                                        line = r.ReadLine();
                                    }
                                    r.Close();
                                    File.Delete(rstr[0]);
                                    File.Delete(rstr[1]);
                                }
                                else // failure
                                {
                                    string msg = String.Format("There was an error in the current log.");
                                    MessageBox.Show(this, msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                            else // error assembling batch
                            {
                                MessageBox.Show(this, "Unreadable files, or unbalanced number of gestures in categories.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            lblRecognizing.Visible = false;
                            prgTesting.Visible = false;
                        }
                    }

                    outfile.Close();
                    lblRecognizing.Text = "Recognizing...";
                }
            }
        }