Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

47
LINES

< > BotCompany Repo | #1004663 // Test Algorithm For Text Recognition

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2676L/17K/57K).

!752

static Map<BWImage, S> chars;
sbool debug;

!include #1000522 // helper functions
!include #1004653 // Lowest

static S recognizeWord(BWImage iw, O infos) {
  chars = (Map) quickImport(get(infos, "chars"));
  debug = isTrue(getOpt(infos, "debug"));
  
  int w = iw.getWidth();
  new StringBuilder buf;
  xloop: for (int x = 0; x < w; x++) {
    if (debug)
      print("Best match at " + x + ": " + structure(bestMatch(iw, x)));
    for (BWImage ic : keys(chars)) {
      if (preciseMatch(ic, iw, x)) {
        buf.append(chars.get(ic));
        x += ic.getWidth()-1;
        continue xloop;
      }
    }
  }
  ret str(buf);
}

static Pair<S, Double> bestMatch(BWImage iw, int x) {
  new Lowest<S> best;
  for (BWImage ic : keys(chars)) {
    double d = diff(ic, iw.clip(x, 0, ic.getWidth(), ic.getHeight()));
    best.update(chars.get(ic), d);
  }
  ret best.getPair();
}

static bool preciseMatch(BWImage ic, BWImage iw, int x1) {
  int w = iw.getWidth(), h = iw.getHeight();
  if (ic.getHeight() != h) false;
  int x2 = x1+ic.getWidth();
  if (x2 > w) false;
  for (int x = x1; x < x2; x++)
    for (int y = 0; y < h; y++)
      if (ic.getPixel(x-x1, y) != iw.getPixel(x, y)) false;
  true;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004663
Snippet name: Test Algorithm For Text Recognition
Eternal ID of this version: #1004663/1
Text MD5: be227ed902736512aa93d3999d0cd2ab
Transpilation MD5: 74cdc2f4c79136e614a51f21af87cd54
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-05 11:09:15
Source code size: 1249 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 502 / 786
Referenced in: [show references]