Initialize() private method

private Initialize ( IntPtr tesseractHandle, string datapath, string language ) : int
tesseractHandle IntPtr
datapath string
language string
return int
Beispiel #1
0
        /// <summary>
        /// Used to initialize the routine asynchronously
        /// </summary>
        /// <param name="languageId">Language ID</param>
        /// <returns>IEnumerator</returns>
        private IEnumerator InitializeRoutine(string languageId)
        {
            // Copies all tess data to persistent directory
            yield return CoroutineRunner.RunCoroutine(InitializeTessdataRoutine());

            string[] languageSplit = languageId.Split('+');

            // Check whether we have stored the correct tesseract language data
            for(int i = 0; i < languageSplit.Length; i++)
            {
                if(!m_AvailableDataSet.Contains(languageSplit[i]))
                {
                    Debug.LogWarning(string.Format("Tesseract language id is not available. Please check your settings for the correct data import. [{0}]", languageSplit[i]));
                }
            }

            if(m_Handle != IntPtr.Zero)
            {
                // Perform initialization from the native library
                if(TesseractOCRBridge.Initialize(m_Handle, GetTessdataPersistentPath(), languageId) == 0)
                {
                    // Copies the configuration file to persistent directory and sets the config file to use
                    yield return CoroutineRunner.RunCoroutine(InitializeConfigFileRoutine(true));

                    Debug.Log("Initialize TesseractOCR Success!");
                }
                else
                {
                    Debug.Log("Initialize TesseractOCR Failed!");
                }
            }
        }
        /// <summary>
        /// Initializes tesseract's configuration list
        /// </summary>
        private void InitializeTesseractConfiguration()
        {
            IntPtr handle = TesseractOCRBridge.CreateTesseractHandle();

            if(TesseractOCRBridge.Initialize(handle, Application.persistentDataPath + "/tessdata", "eng") == 0)
            {
                TesseractOCRBridge.PrintVariablesToFile(handle, "config");
            }

            TesseractOCRBridge.EndTesseractHandle(handle);
            TesseractOCRBridge.DeleteMonitorHandle(handle);

            string defaultConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "config");

            if(File.Exists(defaultConfigPath))
            {
                string[] lines = File.ReadAllLines(defaultConfigPath);

                m_TesseractDefaultConfigInfo = new TesseractConfigInfo[lines.Length];

                for(int i = 0; i < lines.Length; i++)
                {
                    string[] split = lines[i].Split('\t');
                    m_TesseractDefaultConfigInfo[i] = new TesseractConfigInfo()
                    {
                        name = split[0],
                        value = split[1],
                        description = split[2]
                    };
                }

                IComparer<TesseractConfigInfo> comparer = new AscendingCompare();
                Array.Sort<TesseractConfigInfo>(m_TesseractDefaultConfigInfo, comparer);

                int defaultConfigLen = m_TesseractDefaultConfigInfo.Length;
                m_TesseractConfigInfo = new TesseractConfigInfo[defaultConfigLen];
                Array.Copy(m_TesseractDefaultConfigInfo, m_TesseractConfigInfo, defaultConfigLen);

                File.Delete(defaultConfigPath);
            }

            string userDefinedConfigPath = "Assets/StreamingAssets/tessdata/configs";

            if(Directory.Exists(userDefinedConfigPath))
            {
                string[] configs = Directory.GetFiles(userDefinedConfigPath, "*.", SearchOption.TopDirectoryOnly);

                if(configs.Length == 1)
                {
                    string[] lines = File.ReadAllLines(configs[0]);

                    for(int i = 0; i < lines.Length; i++)
                    {
                        string[] split = lines[i].Split('\t');
                        int index = Array.FindIndex<TesseractConfigInfo>(m_TesseractConfigInfo, a => a.name == split[0]);

                        if(index >= 0)
                        {
                            m_TesseractConfigInfo[index].value = split[1];
                        }
                    }

                    IComparer<TesseractConfigInfo> comparer = new AscendingCompare();
                    Array.Sort<TesseractConfigInfo>(m_TesseractDefaultConfigInfo, comparer);
                }
            }
        }