Neelarghya / tesseract-unity

Standalone OCR plugin for Unity using Tesseract
Apache License 2.0
69 stars 17 forks source link

IndexOutOfRange - TesseractWrapper #5

Closed J9B1 closed 3 years ago

J9B1 commented 3 years ago

I've imported the project into Unity 2019.4.17f1

After importing I had to fix an issue

Assets\Tesseract\Scripts\TesseractWrapper.cs(195,58): error CS0103: The name 'str_ptr' does not exist in the current context

As it couldn't find a reference called str_ptr, so I left the line that set recognizedText as stringPtr, though running the Demo then gives an index error that I've followed back to recognizedText, the index that is mentioned seems to be str[] words that has a length of 1 whilst the two others have lengths of 11, this seems to be due to the recognizedText, using the demo image supplied the value of recognizedText is 晉夠畯挠湡爠慥⁤桴獩⸮ਮ瑉洠慥獮吠獥敳慲瑣圠牯獫ਡ and I'm not 100% sure why, I think it has something to do with Line 189 IntPtr stringPtr = TessBaseAPIGetUTF8Text(_tessHandle); but I haven't learned enough about the Script yet to figure it out.

IndexOutOfRangeException: Index was outside the bounds of the array.
TesseractWrapper.Recognize (UnityEngine.Texture2D texture) (at Assets/Scripts/TesseractWrapper.cs:204)
TesseractDriver.Recognize (UnityEngine.Texture2D imageToRecognize) (at Assets/Scripts/TesseractDriver.cs:105)
TesseractDemoScript.OnSetupCompleteRecognize () (at Assets/Scripts/TesseractDemoScript.cs:33)
TesseractDriver.OcrSetup (UnityEngine.Events.UnityAction onSetupComplete) (at Assets/Scripts/TesseractDriver.cs:57)
TesseractDriver.Setup (UnityEngine.Events.UnityAction onSetupComplete) (at Assets/Scripts/TesseractDriver.cs:34)
TesseractDemoScript.Recoginze (UnityEngine.Texture2D outputTexture) (at Assets/Scripts/TesseractDemoScript.cs:28)
TesseractDemoScript.Start () (at Assets/Scripts/TesseractDemoScript.cs:20)

FIXED The initial error I received was the culprit, replace the #if, #endif statement with the following:

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
        string recognizedText = Marshal.PtrToStringAnsi(stringPtr);
#else
        string recognizedText = Marshal.PtrToStringAuto(stringPtr);
#endif
Neelarghya commented 3 years ago

Thank you for bringing this up, have updated the repo with the changes. Commit: 867cff4 I hope that fixed the issue, feel free to reopen or report any other issues you might face