Results 1 to 12 of 12

Thread: "invalid integer" From StringToInt when converting a tesseract read

  1. #1
    Join Date
    May 2008
    Location
    Canada
    Posts
    665
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default "invalid integer" From StringToInt when converting a tesseract read

    When running this I error out while trying to convert Str to int. I have no idea whats happening, the integers aren't special characters, and I checked there are no extra spaces.
    Would definitely like some insight as to what's going on!
    :
    Tesseract setup in 31 ms
    17170 is the value we got
    22855 is the value we got
    Error: "17170" is an invalid integer at line 109

    Simba Code:
    Lon := StrToInt(txtLon);
    Lat := StrToInt(txtLat);


    Exact program I was compiling.
    Simba Code:
    program ReadingNumbers;
    {$i srl-6/srl.simba}
    // Thanks Olly for the slick Tesseract Code :)
    Type
      PlayerPosition = record
        Lon : Integer;
        Lat : Integer;
    end;

    var
      TessPtr: Pointer = nil;
      TessIsSetup: Boolean = False;

    procedure Tess_Setup();
    var
      Time: UInt64 := GetTickCount64();
    begin
    {$IFNDEF NO_TESS}
      TessPtr := Tesseract_Create();

      if (TessPtr = nil) then
        Writeln('ERROR: Failed to create a tesseract instance')
      else begin
        if (Tesseract_Init(TessPtr, TESS_DATA_PATH, TESS_LANGUAGE) <> 0) then
          Writeln('ERROR: Unable to initizalse tesseract, check Simba console for more infomation')
        else begin
          TessIsSetup := True;
          Writeln(Format('Tesseract setup in %d ms', [GetTickCount64() - Time]));
        end;
      end;
    {$ENDIF}
    end;

    procedure Tess_Free();
    begin
    {$IFNDEF NO_TESS}
      if (not TessIsSetup) then
        Exit();

      Tesseract_End(TessPtr);
      Tesseract_Delete(TessPtr);
      TessPtr := nil;
      TessIsSetup := False;
    {$ENDIF}
    end;

    function Tess_GetText(BMP: TMufasaBitmap): String;
    var
      i: integer;
      textLen: UInt32;
      textPtr: PChar;
    begin
    {$IFNDEF NO_TESS}
      Result := '';

      try
        Tesseract_SetImage(TessPtr, BMP.getData(), BMP.getWidth(), bmp.getHeight(), 4, Integer(BMP.getWidth() * 4));
        textPtr := Tesseract_GetUTF8Text(TessPtr, textLen);

        SetLength(Result, textLen);
        if (textLen > 0) then
        begin
          for i := 1 to textLen do
          begin
            Result[i] := textPtr[i - 1]^;
          end;
        end;
      finally
        Tesseract_FreeUTF8Text(textPtr);
        Tesseract_Clear(TessPtr);
      end;
    {$ENDIF}
    end;

    function Resize_And_Filter(BMP: TMufasaBitmap; xStretchFactor, yStretchFactor:Integer): TMufasaBitmap;
    var
      tmp: TMufasaBitmap;
      NewW, NewH: Integer;
      Time: UInt64 := GetTickCount64();
    begin
      tmp := BMP;

      NewW := tmp.getWidth() * xStretchFactor;
      NewH := tmp.getHeight() * yStretchFactor;

      //writeln(Format('Resized Image (%d, %d) > (%d, %d) in %d ms', [BMP.getWidth(), BMP.getHeight(), NewW, NewH, GetTickCount64() - Time]));
      tmp.ResizeEx(RM_Bilinear, NewW, NewH);
      tmp.ThresholdAdaptive(0, 255, false, TM_Mean, 3);

      Result := tmp;
    end;

    Function CurrentPosition(Longitude,Latitude: TMufasaBitmap):PlayerPosition;
    var
      txtLon,txtLat:String;
    begin
      Longitude := Resize_And_Filter(Longitude,4,4);
      Latitude := Resize_And_Filter(Latitude,4,4);



      txtLon := Tess_GetText(Longitude);
      txtLat := Tess_GetText(Latitude);
      writeln(txtLon + ' is the value we got');
      writeln(txtLat + ' is the value we got');

      With Result do
      begin
        Lon := StrToInt(txtLon);
        Lat := StrToInt(txtLat);
      end;
    end;

    Function MoveTo(toLat, toLon: integer):PlayerPosition;
    begin
    end;


    var
      img1,img2: Integer;
      img1M,img2M: TMufasaBitmap;
      MyPos : PlayerPosition;

    begin
      Tess_Setup();
      img1 := BitmapFromString(29, 8, 'meJxtkstvElEUxhcycx8zPGYoL1t' +
            'ABNNYSeq6cdMYQS2JpLpp0UjAFEK0xrgrbUq6beOjprhTC6Wgi44u' +
            'VRgeTYCmLiApbsqGFakb/gXPdPpwYXIX95773d95fNc0Ypa+SUd/j' +
            'pZXlomBwpp/+bzVbrXa7V6vl/+cxzqSmE90u93O745p2AwC95in3m' +
            'yA4KBz0O/3EY/1ZsPa67VKrfr9549r173YQPkhreOKMxwJLyQXgKC' +
            'SGcIgitLv09P3pxGPRi7bXW6XLMuiSVQEAgWUBmkmbkxIX6ULrMYf' +
            'uL1dyBOe+O74NzObvFELMipwkblIcjF5hoXlGnXVdmsWqwXpMRztH' +
            'ke5UhZUrELmOCO/nn4XehRiOZRMLcYTcZayIKvWqqLFqGBFLhp/co' +
            '4VlIezj0Mb6Q2WIpXj8DgBK55jqe3SxeZe02qzYT1ZfbU6MzsDz61' +
            'OW6VasQxbT6qNR0+xBBuI1qQrfClM3pyEZv+PNdB7D4IfPn1kCQv7' +
            'pdTSXCyGdMTudlR3a6JRPMHGov8O4er4WKksC6IAlUAiFSurWEEpF' +
            'aaXy+du+X2QFwy6G5zKZrOcwPsC/q3tLcwT0WbM5DKH3UPwfUfawT' +
            'oMWZ6+eJZaScGs1Cyh8MP9X/uDwaDRaEwFAxBxjrrqzbo4JOLjycN' +
            'PeLP+tlgqFuWSd9wLLkMQ6bAGM2AomHuGgs8AKfBpywxl4VbRYEaN' +
            'QJ2MKj72AvpiMAO3DIxFT/8CQZKq9g==');
      img2 := BitmapFromString(29, 8, 'meJwlUltPE0EUfmB3Z2a3e6nbVjC' +
            'FRlpbEAhUUwzxxYAigYKIGCURHxQiiND4qIkkJiKBgK1GoyQgFy9A' +
            'hWJCtVRoCd2CGC5WUKCBH4D/wjN2cjKZc5vzfeec6rqa1R+rG5ubo' +
            'bmQLcdGFP5Jd9f6xvpm4qf3hU826YleaPO0rax+j68s93n7ZaOS5y' +
            'z4s7MT+Dzjn/JXXXITmW9qbYaUT4Gp0XdjtlwbVnh7gUMx6hnEeO5' +
            '7+p71IYkUuoqQgAVJCMwEyivKzdmZWlwzWzJ1sjgbnHWVFOc588cn' +
            'xgVRYAnLEg7J5Nad2x2eDhaDynI8B9hAsETgbmpp7untQTpELTIRD' +
            'fKHiY+lZaWGY8bZL0HXmeLsHGsoHLKesOYV5e8f7CeTycWlxSKXk+' +
            'a2Nh/8P4NvB03mdKIIqZ/t+Q5tWbPnOpCIU5b6hvrpmWlRkeDd0ta' +
            'SSCTW1te8Pi8vClBRUiUWsY03Gyf9k1jERzJUxCOdrHszMNB67y6g' +
            'gqwsm2U+Ml96oQxYpP48X1keng+bLWZOQFBxIbJgTDcSkR8ZHblYW' +
            'ZGKATnuyAYXEKeqTBFeabjqe+5DIkm3ZAS/BmvqahnEYpkGl5w7ux' +
            'CNWO02lqcNyS08GYlGVZMqGqTh0eEqt9tkPioZZWB67cb1qcA0g9l' +
            'MaxavF8D48vWrdk870uEHjx4e/j3UNC0W17q6nwJ+eOwl92JabCm2' +
            'VFVbLapS5+POSDQy9y08NDykqHr35erE1q/t39vx5bjz9Clg1NPfu' +
            '7W9tbO7O/Z+TDUZsEThQTnYBHpjlhMxTDaNYwA8qCzmaKNkQoeeCh' +
            'AQkIJx0xTEwCYQylEAVxpiQGiAwv8Dty/JNA==');

      img1M := GetMufasaBitmap(img1);
      img2M := GetMufasaBitmap(img2);

      MyPos := CurrentPosition(img1M,img2M);
      writeln(MyPos.Lon);
      writeln(MyPos.Lat);

      FreeBitmap(Img1);
      FreeBitmap(Img2);

    end.
    Last edited by mrpickle; 01-22-2017 at 11:50 PM.

  2. #2
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    It looks like very odd error. Only thing I have on mind are some invisible special characters.

    Put filter inside your function, before conversion and see what will happen:

    Simba Code:
    txtLon := ReplaceRegExpr('[^0-9]',txtLon,'',false);
    txtLat := ReplaceRegExpr('[^0-9]',txtLat,'',false);

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Whens you put something other than a integer when converting it. Where? IDK. But that's what it means

  4. #4
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    Use the Tesseract whitelist if you only want to read digits.

  5. #5
    Join Date
    May 2008
    Location
    Canada
    Posts
    665
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    Quote Originally Posted by bg5 View Post
    It looks like very odd error. Only thing I have on mind are some invisible special characters.

    Put filter inside your function, before conversion and see what will happen:

    Simba Code:
    txtLon := ReplaceRegExpr('[^0-9]',txtLon,'',false);
    txtLat := ReplaceRegExpr('[^0-9]',txtLat,'',false);
    This worked, odd though. But it worked

    Quote Originally Posted by Olly View Post
    Use the Tesseract whitelist if you only want to read digits.
    I tried it, but it didn't work... maybe I didn't implement properly? Same error as before. I used it like this:
    Simba Code:
    Tesseract_SetVariable(TessPtr, 'tessedit_char_whitelist', TESS_WHITELIST_NUMBERS);
        Tesseract_SetImage(TessPtr, BMP.getData(), BMP.getWidth(), bmp.getHeight(), 4, Integer(BMP.getWidth() * 4));
        textPtr := Tesseract_GetUTF8Text(TessPtr, textLen);

    With your function here:
    Simba Code:
    function Tess_GetNumbers(BMP: TMufasaBitmap): String;
    var
      i: integer;
      textLen: UInt32;
      textPtr: PChar;
    begin
    {$IFNDEF NO_TESS}
      Result := '';

      try
        Tesseract_SetVariable(TessPtr, 'tessedit_char_whitelist', TESS_WHITELIST_NUMBERS);
        Tesseract_SetImage(TessPtr, BMP.getData(), BMP.getWidth(), bmp.getHeight(), 4, Integer(BMP.getWidth() * 4));
        textPtr := Tesseract_GetUTF8Text(TessPtr, textLen);

        SetLength(Result, textLen);
        if (textLen > 0) then
        begin
          for i := 1 to textLen do
          begin
            Result[i] := textPtr[i - 1]^;
          end;
        end;
      finally
        Tesseract_FreeUTF8Text(textPtr);
        Tesseract_Clear(TessPtr);
      end;
    {$ENDIF}
    end;

  6. #6
    Join Date
    May 2008
    Location
    Canada
    Posts
    665
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    Did some more testing. The Whitelist in the code above works fine; I only saw numbers. But I still had the same issue when trying to convert them to Integer, Unless I use:
    ReplaceRegExpr('[^0-9]',txtLon,'',false);

  7. #7
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    Quote Originally Posted by mrpickle View Post
    Did some more testing. The Whitelist in the code above works fine; I only saw numbers. But I still had the same issue when trying to convert them to Integer, Unless I use:
    ReplaceRegExpr('[^0-9]',txtLon,'',false);

    that's pretty self-explanatory that there are invisible special characters in you txtLon , thxLat. If you want to check out which special characters are there use this function on them:

    Simba Code:
    procedure ShowStrOrd(s:string);
    var i,h :integer;
    r : string;
    begin
      h := length(s)-1;
      for i :=1 to h do
        r := r + s[i]+':#'+tostr( ord(s[i]) )+' ; ';
      writeln(r);
    end;

    then use it before regexpr filters

    Simba Code:
    ShowStrOrd(txtLon);
    ShowStrOrd(txtlat);

  8. #8
    Join Date
    May 2008
    Location
    Canada
    Posts
    665
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    Quote Originally Posted by bg5 View Post
    that's pretty self-explanatory that there are invisible special characters in you txtLon , thxLat. If you want to check out which special characters are there use this function on them:

    Simba Code:
    procedure ShowStrOrd(s:string);
    var i,h :integer;
    r : string;
    begin
      h := length(s)-1;
      for i :=1 to h do
        r := r + s[i]+':#'+tostr( ord(s[i]) )+' ; ';
      writeln(r);
    end;

    then use it before regexpr filters

    Simba Code:
    ShowStrOrd(txtLon);
    ShowStrOrd(txtlat);
    I definitely didn't think to do that. But yeah there's an invisible #10 (Line feed?) that was read at the end of each line. Now everything makes sense!

    Appreciate it!

  9. #9
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    Quote Originally Posted by mrpickle View Post
    I definitely didn't think to do that. But yeah there's an invisible #10 (Line feed?) that was read at the end of each line. Now everything makes sense!

    Appreciate it!
    @Olly , isn't it some kind of Tesseract bug ? Especially that proper 'new line' mark is #13 #10

  10. #10
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    Quote Originally Posted by bg5 View Post
    @Olly , isn't it some kind of Tesseract bug ? Especially that proper 'new line' mark is #13#10
    It's probably intentional, just trim the string before converting to a Integer.

    Down the line I would like to switch to Tesseract 4.0 & and train it for RS fonts and remove the c++ bridge if possible. But Tesseract isn't exactly easy to build & work with.

    @Brandon Does building Tesseract create libs that are friendly with other languages such as?

    Simba Code:
    LoadLibrary('libtesseract.dll')
    GetProcedureAddress(Lib, 'Tesseract_Create');
    Etc...

  11. #11
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Olly View Post
    It's probably intentional, just trim the string before converting to a Integer.

    Down the line I would like to switch to Tesseract 4.0 & and train it for RS fonts and remove the c++ bridge if possible. But Tesseract isn't exactly easy to build & work with.

    @Brandon Does building Tesseract create libs that are friendly with other languages such as?

    Simba Code:
    LoadLibrary('libtesseract.dll')
    GetProcedureAddress(Lib, 'Tesseract_Create');
    Etc...
    Sorry for the late reply, I rarely check forums (been busy).

    I was confused by the question. However, if I understand it correctly, you want to build tessaract with all exported symbols so that you can load them in Simba or some other language?

    Yes you can compile it directly to a dynamic library and load that from other languages (it exports C API's as well as C++ ones).
    Last edited by Brandon; 02-25-2017 at 08:24 PM.
    I am Ggzz..
    Hackintosher

  12. #12
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    ...
    That's exactly what I meant. I only posted that earlier today, suppose I just got lucky that you decided to check in today.
    Last edited by Olly; 02-26-2017 at 06:16 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •