Results 1 to 21 of 21

Thread: OCR-Engine for OSRS

  1. #1
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default OCR-Engine for OSRS

    I know Simba does a good job already, and tesseract can be tweaked to work for most text, but there is room for yet another alternative, so here it goes:
    I've just written a small OCR-engine for OSRS. I don't have the possibility to test it properly my self, but I assume it works like it should (mostly). This engine is pretty much an upgrade for my old one (OCR-engine for OSR (Uptext)), and it's still a early version, might have to add some updates at some point (time will tell).

    Motivation:
    • Should read connected chars properly (biggest motivator, Simba can't do this (example))
    • Simpler to use then my old one (no script-side functions needed to have it working)
    • Should work for most fonts in OSRS, not just uptext (just load the correct font).
    • Tweak-able - You pass it a very simple "filter" (my old one had hard-coded tolerance and shit).
    • Can use thresholding instead of having to pass a specific color (so it can read textline that has many colors)


    It has a small "issue": It is extremely good at adding noise-chars (".-,_") to the result (only when threshold is used), mostly just when it's run on some random image (where the text is not recognized). I could probably write "hack" to remove most of it, but i don't see why I should bother (yet).

    Sidenote: The engine should work in other games. EG: RS3 as long as the font is NOT smooth, and you have a proper charset for it.


    Methods exported:
    Simba Code:
    procedure TFontSet.Load(Font:AnsiString; Space:Int32=4);
    procedure TFontSet.Free();

    procedure TSimpleOCR.Init(Font:TFontSet; Dbg:LongBool=False; AClient:TTarget_Exported=ExportImageTarget());
    procedure TSimpleOCR.Init(Font:AnsiString; SpaceWidth:Int32=4; Dbg:LongBool=False; AClient:TTarget_Exported=ExportImageTarget()); overload;
    procedure TSimpleOCR.SetFont(Font:TFontSet);
    procedure TSimpleOCR.SetFont(Font:AnsiString; SpaceWidth:Int32=4); overload;
    procedure TSimpleOCR.Free();
    function TSimpleOCR.Recognize(B:TBox; Filter:TCompareRules; MaxWalk:Int32=40): AnsiString;
    function TSimpleOCR.RecognizeEx(AClient:T2DIntArray; Filter:TCompareRules; MaxWalk:Int32=40): AnsiString; overload;
    As you see there is no IsText(..), there is only Recognize(..), it does all that is needed, the rest can easily be done scriptside.
    @`dbg` (debug) parameter is simple (in OCR.Init): if "True" the client matrix is not freed once the image has been read, so you can debug it (TSimpleOCR.Client) and write it to a bitmap, mostly just to check if threshold looks correct, or if the passed box (B) is correctly positioned.


    Example usage (Coordinates might be off, and filters might be iffy):
    Simple example (uptext):
    pascal Code:
    program SimpleExample;
    {$loadlib SimpleOCR.dll}
    var
      OCR: TSimpleOCR;
    begin
      OCR.Init('UpChars07_s');  //or preferable: OCR.Init('UpChars07_s',,,ExportImageTarget());
      WriteLn(OCR.Recognize(TBox([7,7,465,25]), TCompareRules([-1,72,True,55]))); //filter = [AnyColor,72 tol, UseShadows!, 55 shadow tol]
    end;

    Extended example:
    pascal Code:
    program FullExample;
    {$loadlib SimpleOCR.dll}

    const
      (*
      TCompareRules:
        If color = -1 and useshadows=False then tolerance is ignored, it will use thresholding instead.
        Both shadows, and threshold is never used at the same time:
        - If HasShadow = True then shadows are used (specify shadow tolerance)
        - Else threshold is used (specify threshold, and if you need to invert the threshold)

        Example of using with some color:
        WriteLn(OCR.Recognize([29,34,450,51], [myColor, myTolerance]));

        // fields in the type is something like:
        IN: (Color,Toleranse), (HasShadow,ShadowTolernace), (Threshold,InvertThreshold)
        EG: [-1,   0,           False,    0,                 50,       False]
      *)

      //Example "filters" (could need tweaking)
      OCR_RULES_DEFAULT:TCompareRules = [-1,,,,50,False];  //Threshold
      OCR_RULES_UPTEXT: TCompareRules = [-1,75,True,55];   //Shadow
      OCR_RULES_CHATBOX:TCompareRules = [-1,,,,-80,True];  //Threshold
      OCR_RULES_BLUE:   TCompareRules = [16711680,10];     //navy-blue text (chatbox msg)
      OCR_RULES_BLACK:  TCompareRules = [0,10];            //black text (chatbox nick)



    // Uses
    var
      SmallFont,UpFont:TFontSet;
      OCR: TSimpleOCR;
      i:Int32;
    begin
      UpFont.Load('UpChars07_s');            // fontloading (can be forced freed with UpFont.Free() for example)
      SmallFont.Load('SmallChars07');        // tho memory should be automanaged.

      OCR.Init(UpFont);    // OCR initialization (font)

      // Uptext
      WriteLn('Example 1: '+OCR.Recognize(TBox([7,7,465,25]), OCR_RULES_UPTEXT));

      // Duel Arena challenge (The header "Dueling with: ...")
      OCR.SetFont(SmallFont);
      WriteLn('Example 2: '+OCR.Recognize(TBox([29,34,450,51]), OCR_RULES_DEFAULT));

      // Chatbox
      for i:=0 to 6 do
        WriteLn('Example 3: '+OCR.Recognize(TBox([8,359+(14*i),480,374+(14*i)+1]), OCR_RULES_CHATBOX));
    end;



    Plugin (just extract the dll to the plugin folder):
    - https://github.com/slackydev/SimpleOCR/releases

    Source:
    - https://github.com/slackydev/SimpleOCR/


    Note:
    It only works in Lape. I do not plan on supporting PascalScript.


    Changelog:
    log Code:
    06/12/14: Fix: Issue with grabbing the client image when using SMART. Updated examples, and the Init-function to reflect this change.
    06/12/14: Fix: Text could start with space.
    19/12/14: Addition: function TSimpleOCR.Recognize(AClient:T2DIntArray; Filter:TCompareRules; MaxWalk:Int32=40): AnsiString; overload;
    19/12/14: Update: Should compile on linux now.
    19/12/14: Fix: Issue caused `?`-font (63.bmp) to not load
    04/02/15: Fix: Removed 1 (maybe 2) bug(s), and added some more error-handling.
    ... For more changes check it out at github.

    PS: For proper implementation, so that it works with SMART you will need to actively updte the client, so that it's in tune with simba, this can be done by overrideing exported methods. See:
    > https://github.com/SRL/SRL/blob/master/osr/text.simba
    Last edited by slacky; 08-01-2017 at 12:34 PM.
    !No priv. messages please

  2. #2
    Join Date
    Sep 2012
    Location
    Netherlands
    Posts
    2,752
    Mentioned
    193 Post(s)
    Quoted
    1468 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    *snip*
    very nice slacky!

  3. #3
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    Source code looks pretty damn good - nice work, @slacky!

    Also mate, I am really glad to see that you are back in action.

  4. #4
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by hoodz View Post
    very nice slacky!
    Thanks!

    Quote Originally Posted by Janilabo View Post
    Source code looks pretty damn good - nice work, @slacky!

    Also mate, I am really glad to see that you are back in action.
    Thanks. Been a long time since I've heard from you buddy.

    I hope that this engine will get some uses, and even replace my old ("hacky") one
    Last edited by slacky; 12-06-2014 at 08:05 PM.
    !No priv. messages please

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Added a fix: There was an issue when using SMART with this, the way I grabbed the target-image would have failed with SMART as it's not like other targets.
    Thanks @Olly for noticing it.
    Last edited by slacky; 12-06-2014 at 08:46 PM.
    !No priv. messages please

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

    Default

    This looks pretty bad a, always had trouble reading 'CharsNPC07'

  7. #7
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Robert View Post
    This looks pretty bad a, always had trouble reading 'CharsNPC07'
    CharsNPC07 should work fine, I did a couple of tests on it which came back flawless.
    And what looks bad?
    !No priv. messages please

  8. #8
    Join Date
    Mar 2006
    Location
    Belgium
    Posts
    3,564
    Mentioned
    111 Post(s)
    Quoted
    1475 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    CharsNPC07 should work fine, I did a couple of tests on it which came back flawless.
    And what looks bad?
    he meant badass

    Creds to DannyRS for this wonderful sig!

  9. #9
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Sjoe View Post
    he meant badass
    Aha, thank you for explaining!
    !No priv. messages please

  10. #10
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    OCR has been pretty accurate except 1 minor detection error:
    http://puu.sh/doVxz/8e2b72d6b1.png
    http://puu.sh/doVvA/79c9b3631b.png
    Simba Code:
    var
      OCR: TSimpleOCR;
      i: integer;
    begin
      OCR.LoadFont('SmallChars07');
      for i:=0 to 10 do
        writeln(OCR.Recognize(IntToBox(109, 60, 182, 70 + i),  [2070753, 0]));
    end.
    It's detecting the underscore as dots: 'Darkace.......25'
    Using Replace() as a temp fix now

  11. #11
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    OCR has been pretty accurate except 1 minor detection error:
    http://puu.sh/doVxz/8e2b72d6b1.png
    http://puu.sh/doVvA/79c9b3631b.png
    Simba Code:
    var
      OCR: TSimpleOCR;
      i: integer;
    begin
      OCR.LoadFont('SmallChars07');
      for i:=0 to 10 do
        writeln(OCR.Recognize(IntToBox(109, 60, 182, 70 + i),  [2070753, 0]));
    end.
    It's detecting the underscore as dots: 'Darkace.......25'
    Using Replace() as a temp fix now
    It's not a bug/error. It's the fontset (SmallChars07) that is lacking the underscore (95.bmp) font.
    The 95.bmp from SmallChars fits the SmallChars07, just copy that one.

    PS: Seems like you are still using the prototype engine, since that one is pure lape its much slower, it also lack some small fixes ;P
    Last edited by slacky; 12-10-2014 at 06:24 PM.
    !No priv. messages please

  12. #12
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    It's not a bug/error. It's the fontset (SmallChars07) that is lacking the underscore (95.bmp) font.
    The 95.bmp from SmallChars fits the SmallChars07, just copy that one.

    PS: Seems like you are still using the prototype engine, since that one is pure lape its much slower, it also lack some small fixes ;P
    Ahh I see didn't thought of that.

    The script still has to wait for opponent's offer after reading the text so speed isnt an issue, easier for me to do minor adjustments using the pure lape version.
    Also the plugin version is throwing 'Error: Don't know which overloaded method to call with params (AnsiString)', probably because of https://villavu.com/forum/showthread.php?t=110843 of which only ur simba has the fix

    Simba Code:
    procedure TSimpleOCR.Init(Font:TFontSet;                       Dbg:LongBool=False; AClient:TTarget_Exported=ExportImageTarget());
    procedure TSimpleOCR.Init(Font:AnsiString; SpaceWidth:Int32=4; Dbg:LongBool=False; AClient:TTarget_Exported=ExportImageTarget()); overload;

  13. #13
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    ...
    Also the plugin version is throwing 'Error: Don't know which overloaded method to call with params (AnsiString)'
    ...
    Hmm. There shoudn't be any issue with overloads, just tried with older Simba versions, and it works fine for me. The "bug" you linked to is also unrelated to the issue you have.
    Can anyone else verify the above issue? Where it fails to call overload method to init from string.
    Last edited by slacky; 12-16-2014 at 10:57 AM.
    !No priv. messages please

  14. #14
    Join Date
    Aug 2007
    Location
    Colorado
    Posts
    7,421
    Mentioned
    268 Post(s)
    Quoted
    1442 Post(s)

    Default

    This looks interesting and if you say it's an upgrade from your previous OCR engine, I'm all for it. Would you say it's worth replacing your old OCR engine in AeroLib with this new version, Slacky?

    Also, whoever has tested this, how does it do speed-wise? Maybe this question is also best intended for you, Slacky. Would you say it's faster to recognize text as opposed to the older version?

    Current projects:
    [ AeroGuardians (GotR minigame), Motherlode Miner, Blast furnace ]

    "I won't fall in your gravity. Open your eyes,
    you're the Earth and I'm the sky..."


  15. #15
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Flight View Post
    This looks interesting and if you say it's an upgrade from your previous OCR engine, I'm all for it. Would you say it's worth replacing your old OCR engine in AeroLib with this new version, Slacky?

    Also, whoever has tested this, how does it do speed-wise? Maybe this question is also best intended for you, Slacky. Would you say it's faster to recognize text as opposed to the older version?
    Idk. I guess it would be worth the update. Performance-wise the new one is a tad slower, but on the other hand it reads connected characters https://villavu.com/forum/showthread.php?t=111126.
    It's slower because it doesn't have any shortcut "tricks", my old one could be passed a string like "more options" to make it quit once that text was read causing a significant boost in speed (in some cases), the new one doesn't have this feature, I'll consider adding it, and some other shit.
    The new one can however work with most font-sets, which means you can read this: http://i.imgur.com/Rn49FlM.png :P
    !No priv. messages please

  16. #16
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    I just got news from Olly that the OCR engine is added to Simbas updater (SRL-plugins), so you should now be able to use it without having to download it manually
    Last edited by slacky; 12-30-2014 at 04:23 AM.
    !No priv. messages please

  17. #17
    Join Date
    Sep 2012
    Location
    Netherlands
    Posts
    2,752
    Mentioned
    193 Post(s)
    Quoted
    1468 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    I just got news from Olly that the OCR engine is added to Simbas updater, so you should now be able to use it without having to download it manually
    that's some great news!

  18. #18
    Join Date
    Dec 2014
    Posts
    12
    Mentioned
    0 Post(s)
    Quoted
    5 Post(s)

    Default

    Thank you to share this with us!

  19. #19
    Join Date
    Aug 2007
    Location
    Colorado
    Posts
    7,421
    Mentioned
    268 Post(s)
    Quoted
    1442 Post(s)

    Default

    Perhaps I should replace the older OCR in AeroLib with this. Would you object to this, Slacky?

    Current projects:
    [ AeroGuardians (GotR minigame), Motherlode Miner, Blast furnace ]

    "I won't fall in your gravity. Open your eyes,
    you're the Earth and I'm the sky..."


  20. #20
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by Flight View Post
    Perhaps I should replace the older OCR in AeroLib with this. Would you object to this, Slacky?
    no issue with that, what so ever. This one comes for with SRL-plugins, so it's a wise move.
    Last edited by slacky; 02-22-2018 at 12:35 AM.
    !No priv. messages please

  21. #21
    Join Date
    Aug 2007
    Location
    Colorado
    Posts
    7,421
    Mentioned
    268 Post(s)
    Quoted
    1442 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    no issue with that, what so ever. This one comes for with SRL-plugins, so it's a wise move.
    Very good, however Simba fails to update the plugins so it looks like I'll be going about that part of the procedure manually.

    Current projects:
    [ AeroGuardians (GotR minigame), Motherlode Miner, Blast furnace ]

    "I won't fall in your gravity. Open your eyes,
    you're the Earth and I'm the sky..."


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
  •