Click here to Skip to main content
15,891,930 members
Articles / Artificial Intelligence / Neural Networks

Multiple convolution neural networks approach for online handwriting recognition

Rate me:
Please Sign up or sign in to vote.
4.95/5 (37 votes)
9 Apr 2013CPOL8 min read 76.6K   25.1K   74  
The research focuses on the presentation of word recognition technique for an online handwriting recognition system which uses multiple component neural networks (MCNN) as the exchangeable parts of the classifier.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace UPImage.FileFormat
{

    /// <summary>
    /// KEYWORDs in file
    /// </summary>
    public class UPKEYWORD
    {
        public List<String> content_list;
        public String keyName;
        public UPKEYWORD()
        {
            keyName = "";
            content_list = new List<String>();
        }
    };

    /// <summary>
    /// ////---------------------------------------------------------------------------
    /// </summary>
    public class UPRecognizerDocumentation
    {
    }
    public class UPRecognizerDeclarations
    {
    }
    public class UPRecognitionResults
    {
    }

    /// <summary>
    /// --------------------------------------------------------------------------------
    /// </summary>
    public class UPAlphabet
    {
        UPKEYWORD alphabet;
        UPKEYWORD alphabet_freq;
        public UPAlphabet()
        {
            alphabet = new UPKEYWORD();
            alphabet_freq = new UPKEYWORD();

        }
        public void SetAlphabet(UPKEYWORD kw)
        {
            switch (kw.keyName)
            {
                case ".ALPHABET":
                    alphabet = kw;
                    break;
                case ".ALPHABET_FREQ":
                    alphabet_freq = kw;
                    break;
            }
        }
    }
    /// <summary>
    /// ---------  Lexicon  ---------------------------------------------------
    ///
    /// .KEYWORD .LEXICON_SOURCE [S]	       	Name of institution or person where the lexicon came from.
    /// .KEYWORD .LEXICON_ID [S]		Name of the lexicon.
    /// .KEYWORD .LEXICON_CONTACT [F]	        Where to reach the person responsible to answer questions about the lexicon.
    /// .KEYWORD .LEXICON_INFO [F]		Informations about the lexicon.
    /// .KEYWORD .LEXICON [L] [.]		Representative set of class labels found in the database, generally at the word or character segmentation level.
    /// .KEYWORD .LEXICON_FREQ [N] [.]		Frequencies of lexical entries defined by .LEXICON. Lexical frequencies 
    /// characterize the distribution from which data samples were drawn at random. Therefore, the number of
    /// times a lexical entry appears in the database should be approximately proportional to the lexical 
    /// frequencies. Normalizing such that all numbers add-up to one is not necessary.
    /// </summary>
    public class UPLexicon
    {
        public UPKEYWORD lexicon_source;
        public UPKEYWORD lexicon_id;
        public UPKEYWORD lexicon_contact;
        public UPKEYWORD lexicon_info;
        public UPKEYWORD lexicon;
        public UPKEYWORD lexicon_freq;
        public UPLexicon()
        {
        }
        public void SetLexicon(UPKEYWORD kw)
        {
            switch (kw.keyName)
            {
                case ".LEXICON_SOURCE":
                    break;
                case ".LEXICON_ID":
                    break;
                case ".LEXICON_CONTACT":
                    break;
                case ".LEXICON_INFO":
                    break;
                case ".LEXICON":
                    break;
                case ".LEXICON_FREQ":
                    break;
            }
        }
    }
    public class UPUnitSystem
    {
        /*
         * .COMMENT ---------  Unit system   ---------------------------------------------
        .KEYWORD .X_POINTS_PER_INCH [N]	  	x resolution of the data collection device (1 inch ~ 2.5 cm).
        .KEYWORD .Y_POINTS_PER_INCH [N]		y resolution of the data collection device.
        .KEYWORD .Z_POINTS_PER_INCH [N]		z (altitude) resolution of the data collection device.
        .KEYWORD .X_POINTS_PER_MM [N]	  	x resolution of the data collection device (in SI units).
        .KEYWORD .Y_POINTS_PER_MM [N]		y resolution of the data collection device.
        .KEYWORD .Z_POINTS_PER_MM [N]		z (altitude) resolution of the data collection device.
        .KEYWORD .POINTS_PER_GRAM [N]		Pressure resolution of the data collection device.
        .KEYWORD .POINTS_PER_SECOND [N]		Sampling rate, MANDATORY if T not in .COORD.
         * */
        double dpiX;
        double dpiY;
        double dpiZ;

        //
        double dpMX;
        double dpMY;
        double dpMZ;
        //
        double dpGram;
        double dpSecond;
        //
        public double DpiX
        {
            get
            {
                return dpiX;
            }
            set
            {
                if (dpiX == value)
                    return;
                dpiX = value;
            }
        }
        public double DpiY
        {
            get
            {
                return dpiY;
            }
            set
            {
                if (dpiY == value)
                    return;
                dpiY = value;
            }
        }
        public double DpiZ
        {
            get
            {
                return dpiZ;
            }
            set
            {
                if (dpiZ == value)
                    return;
                dpiZ = value;
            }
        }
        public double DpSecond
        {
            get
            {
                return dpSecond;
            }
            set
            {
                if (dpSecond == value)
                    return;
                dpSecond = value;
            }
        }
        //
        public UPUnitSystem()
        {
            dpiX = 0;
            dpiY = 0;
            dpiZ = 0;
            dpMX = 0;
            dpMY = 0;
            dpMZ = 0;
            dpGram = 0;
            dpSecond = 0;
        }
        public void SetUPUnitSystem(UPKEYWORD kw)
        {
            switch (kw.keyName)
            {
                case ".X_POINTS_PER_INCH":

                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".X_POINTS_PER_INCH"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpiX = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
                case ".Y_POINTS_PER_INCH":
                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".Y_POINTS_PER_INCH"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpiY = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
                case ".Z_POINTS_PER_INCH":
                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".Z_POINTS_PER_INCH"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpiZ = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
                case ".X_POINTS_PER_MM":
                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".X_POINTS_PER_MM"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpMX = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
                case ".Y_POINTS_PER_MM":
                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".Y_POINTS_PER_MM"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpMY = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
                case ".Z_POINTS_PER_MM":
                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".Z_POINTS_PER_MM"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpMZ = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
                case ".POINTS_PER_GRAM":
                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".POINTS_PER_GRAM"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpGram = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
                case ".POINTS_PER_SECOND":
                    foreach (var s in kw.content_list)
                    {
                        //get Y_DIM number
                        if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, ".POINTS_PER_SECOND"))
                        {
                            String tempSt = s.Substring(s.IndexOf(" ")).Trim();
                            dpSecond = Convert.ToDouble(tempSt);
                            break;
                        }
                    }
                    break;
            }
        }
    }
    public class UPDataSet : UPDataAnnotations
    {
        String stName; //name of UPDataSet
        UPAlphabet alphabet;
        UPLexicon lexicon;
        UPUnitSystem unitsystem;
        //list of data layouts
        List<UPDataLayout> datalayouts;
        UPDataLayout curr_datalayout;
        //
        /* allocated filecontents, for the Unipenfile and each included file */
        String initialDirectory;
        String filename;
        String currentopenfile;
        List<String> upIncludeFiles;
        UPKEYWORD keyword;
        int CompID;
        private int _maxDegreeOfParallelism = Environment.ProcessorCount;
    

        //
        /// <summary>
        ///
        /// </summary>
        /// 
        public int MaxDegreeOfParallelism
        {
            get
            {
                return _maxDegreeOfParallelism;
            }

            set
            {
                if (value == _maxDegreeOfParallelism)
                    return;

                if ((value == 0) || (value > Environment.ProcessorCount))
                    _maxDegreeOfParallelism = -1;
                else
                    _maxDegreeOfParallelism = value;

                ParallelOption.MaxDegreeOfParallelism = _maxDegreeOfParallelism;
            }
        }
        public ParallelOptions ParallelOption { get; private set; }
        public String Filename
        {
            get
            {
                return filename;
            }
            set
            {
                if (filename == value)
                    return;
                filename = value;
            }
        }
        public String InitialDirectory
        {
            get
            {
                return initialDirectory;
            }
            set
            {
                initialDirectory = value;
            }
        }
        public List<UPDataLayout> Datalayouts
        {
            get
            {
                return datalayouts;
            }
        }
        public UPUnitSystem Unitsystem
        {
            get
            {
                return unitsystem;
            }
            set
            {
                if (unitsystem == value)
                    return;
                unitsystem = value;
            }
        }
        //----------------------------------------------------------------------------------------------------------------------//
        public UPDataSet()
        {
            stName = "";
            alphabet = new UPAlphabet();
            lexicon = new UPLexicon();
            unitsystem = new UPUnitSystem();
            datalayouts = new List<UPDataLayout>();
            curr_datalayout = null;
            initialDirectory = "";
            upIncludeFiles = new List<string>();
            keyword = null;
            CompID = -1;
            ParallelOption = new ParallelOptions();
            ParallelOption.TaskScheduler = null;
            _maxDegreeOfParallelism = Environment.ProcessorCount;
            ParallelOption.MaxDegreeOfParallelism = _maxDegreeOfParallelism;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fname"> file name included full path</param>
        /// <param name="directory">safe file name (without path)</param>
        public bool FillDataSetFromFile(string fname)
        {
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                stName = fname;
                filename = fname;
                currentopenfile = filename;
                
                    using (StreamReader sr = new StreamReader(filename))
                    {
                        string line;
                        // Read and display lines from the file until the end of 
                        // the file is reached.
                        while ((line = sr.ReadLine()) != null)
                        {
                            line = line.Trim();
                            if (UPImage.Common.UpCommonLib.SubStringSearch(null, line, "."))
                            {
                                line = UPImage.Common.UpCommonLib.ReplaceSubString(line, "\t", " ").Trim();
                                String str = "";
                                if (line.Trim().IndexOf(" ") > 0)
                                {
                                    str = line.Substring(0, line.IndexOf(" ")).Trim();
                                }
                                else
                                {
                                    str = line.Trim();
                                }
                                Parallel.ForEach(UPImage.Common.UpCommonLib.keyworkNames, ParallelOption, (st, loopState) =>
                                {
                                    if (str.CompareTo(st) == 0)
                                    {
                                        if ((keyword != null) && (keyword.keyName != ""))
                                        {
                                            SetParameterFromKeyword(keyword);
                                        }
                                        //Create new KEYWORD
                                        keyword = new UPKEYWORD();
                                        keyword.keyName = str;
                                        loopState.Stop();
                                    }
                                });
                            }
                            //add Content to content list
                            if ((keyword != null) && (keyword.keyName != ""))
                            {
                                if (!String.IsNullOrWhiteSpace(line))
                                {
                                    line = line.Trim();
                                    keyword.content_list.Add(line);
                                }
                            }
                        }
                    }
                  
                //read KEYWORDS in included files
                while (upIncludeFiles.Count>0)
                {
                    String[] ls = new String[upIncludeFiles.Count];
                    upIncludeFiles.CopyTo(ls);
                    upIncludeFiles.Clear();
                    foreach (var item in ls)
                    {

                        // Create an instance of StreamReader to read from a file.
                        // The using statement also closes the StreamReader.
                        currentopenfile = item;
                        FileInfo finfo = new FileInfo(currentopenfile);
                        if (finfo.Exists)
                        {
                            using (StreamReader sr = new StreamReader(item))
                            {
                                string line;
                                // Read and display lines from the file until the end of 
                                // the file is reached.
                                while ((line = sr.ReadLine()) != null)
                                {
                                    line = UPImage.Common.UpCommonLib.ReplaceSubString(line, "\t", " ").Trim();
                                    if (UPImage.Common.UpCommonLib.SubStringSearch(null, line, "."))
                                    {
                                        String str = "";
                                        if (line.IndexOf(" ") > 0)
                                        {
                                            str = line.Substring(0, line.IndexOf(" ")).Trim();
                                        }
                                        else
                                        {
                                            str = line.Trim();
                                        }
                                        Parallel.ForEach(UPImage.Common.UpCommonLib.keyworkNames, ParallelOption, (st, loopState) =>
                                        {
                                            if (str.CompareTo(st) == 0)
                                            {
                                                if ((keyword != null) && (keyword.keyName != ""))
                                                {
                                                    //get data from current keyword
                                                    SetParameterFromKeyword(keyword);
                                                }
                                                //Create new KEYWORD
                                                keyword = new UPKEYWORD();
                                                keyword.keyName = str;
                                                loopState.Stop();
                                            }
                                        });
                                    }
                                    if ((keyword != null) && (keyword.keyName != ""))
                                    {
                                        line = line.Trim();
                                        if (!String.IsNullOrWhiteSpace(line))
                                        {
                                            keyword.content_list.Add(line);
                                        }
                                    }
                                }
                               
                            }
                        }
                        else
                        {
                            MessageBox.Show(String.Format("the:{0} is not exits!",currentopenfile));
                            return false;
                        }
                       
                    }
                    
                }
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            
            
        }
        /// <summary>
        /// GetDataFromKeyword
        /// </summary>
        /// <param name="kw"></param>
        private void SetParameterFromKeyword(UPKEYWORD kw)
        {
            //Category data for last keyword
            //get data from current keyword
            switch (kw.keyName)
            {
                case ".INCLUDE":
                    GetIncludeFileNames(keyword);
                    break;
                case ".X_DIM":
                    //create new DataLayout
                    curr_datalayout = new UPDataLayout();
                    //add datalayout to list
                    datalayouts.Add(curr_datalayout);
                    curr_datalayout.SetDataLayoutParameter(kw);
                    break;
                case ".PEN_DOWN":
                    //check if pen trajectory is not empty
                    
                    if (kw.content_list.Count > 1)
                    {
                        if (kw.content_list.First().Trim().Length == Common.UpCommonLib.keyPenDown.Length)
                        {
                            CompID++;
                            if (curr_datalayout == null)
                            {
                                //create new DataLayout
                                curr_datalayout = new UPDataLayout();
                                //add datalayout to list
                                datalayouts.Add(curr_datalayout);
                            }
                            curr_datalayout.SetUPUnipen(kw, segments, CompID);
                        }
                    }
                    break;
                case ".PEN_UP":
                    //check if pen trajectory is not empty
                    if (kw.content_list.Count > 1)
                    {
                        if (kw.content_list.First().Trim().Length == Common.UpCommonLib.keyPenUp.Length)
                        {
                            CompID++;
                            if (curr_datalayout == null)
                            {
                                //create new DataLayout
                                curr_datalayout = new UPDataLayout();
                                //add datalayout to list
                                datalayouts.Add(curr_datalayout);
                            }
                            curr_datalayout.SetUPUnipen(kw, segments, CompID);
                        }
                    }
                    break;
                case ".DT":
                    //do some thing??
                    break;
                case ".START_SET":
                    //Set name of UPDataSet (future update)
                    break;
                default:
                    //set Mandatory parameters to UPDataSet
                    SetMandatoryDeclarations(kw);
                    //set Document parameters to UPDataSet
                    SetDataDocument(kw);
                    //set Annotation parameters to UPDataSet
                    SetDataAnnotations(kw);
                    //set alphabet parameters
                    alphabet.SetAlphabet(kw);
                    //Set lexicon parameters
                    lexicon.SetLexicon(kw);
                    //Set Unit System parameters
                    unitsystem.SetUPUnitSystem(kw);
                    //add other prameterts to current datalayout
                    if (curr_datalayout != null)
                    {
                        curr_datalayout.SetDataLayoutParameter(kw);
                    }
                    break;
            }
        }
        /// <summary>
        /// GetIncludeFileNames
        /// </summary>
        /// <param name="kw"></param>
        private void GetIncludeFileNames(UPKEYWORD kw)
        {
            if (kw.keyName == ".INCLUDE")
            {
                //get include paths
                foreach (var s in kw.content_list)
                {
                    String sTemp = "";

                    if (UPImage.Common.UpCommonLib.SubStringSearch(null, s, UPImage.Common.UpCommonLib.keyInclude))
                    {
                        sTemp = s.Substring(s.IndexOf(UPImage.Common.UpCommonLib.keyInclude) + UPImage.Common.UpCommonLib.keyInclude.Length).Trim();
                    }
                    else
                    {
                        sTemp = s.Trim();
                    }
                    //add included file name to list
                    FileInfo fInfo = new FileInfo(currentopenfile);
                    DirectoryInfo directory = fInfo.Directory;
                    String includePath="";
                    if (initialDirectory == "")
                    {
                        initialDirectory = "UnipenData";
                    }
                   // check if sTemp has "../".  Get included files inside a included file
                    try
                    {
                        String dataFolder = String.Format("{0}\\{1}",initialDirectory, "data");
                        //try to get default UNIPEN folder
                        if (currentopenfile.Contains(dataFolder))
                        {
                            includePath = String.Format("{0}\\{1}", currentopenfile.Substring(0,currentopenfile.IndexOf(dataFolder)), "UnipenData\\include");
                            //get included files from data file
                            sTemp = sTemp.Replace("/", "\\");
                            String Includedfilename = String.Format("{0}\\{1}", includePath, sTemp);
                            Includedfilename=Includedfilename.Replace("\\\\", "\\");
                            upIncludeFiles.Add(Includedfilename);
                        }
                        else 
                        {
                            includePath = directory.FullName;
                            if (sTemp.Contains("../"))
                            {
                                while (sTemp.Contains("../"))
                                {
                                    directory = directory.Parent;
                                    includePath = directory.FullName;
                                    sTemp = sTemp.Remove(sTemp.IndexOf("../"), 3);
                                }
                                sTemp = sTemp.Replace("/", "\\");
                                String Includedfilename = String.Format("{0}\\{1}", includePath, sTemp);
                                Includedfilename=Includedfilename.Replace("\\\\", "\\");
                                upIncludeFiles.Add(Includedfilename);
                            }
                            else
                            {
                               
                                if (sTemp.Substring(0) == "/")
                                {
                                    sTemp.Remove(0, 1);
                                }
                                String dic = "";
                                if (sTemp.Contains("/"))
                                {
                                    dic = sTemp.Substring(0, sTemp.IndexOf("/"));
                                }
                                if (dic != "")
                                {
                                    if (includePath.Contains(dic))
                                    {
                                        dic = includePath.Substring(0, includePath.IndexOf(dic));
                                        sTemp = sTemp.Replace("/", "\\");
                                        String Includedfilename = String.Format("{0}\\{1}", dic, sTemp);
                                        Includedfilename=Includedfilename.Replace("\\\\", "\\");
                                        upIncludeFiles.Add(Includedfilename);
                                    }
                                    else
                                    {
                                        sTemp = sTemp.Replace("/", "\\");
                                        sTemp=sTemp.Replace("\\\\", "\\");
                                        upIncludeFiles.Add(sTemp);
                                    }
                                }
                                else
                                {
                                    String Includedfilename = String.Format("{0}\\{1}", includePath, sTemp);
                                    Includedfilename=Includedfilename.Replace("\\\\", "\\");
                                    upIncludeFiles.Add(Includedfilename);
                                }
                               
                               
                            }
                            
                        }

                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                   
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Vietnam Maritime University
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions