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

86
LINES

< > BotCompany Repo | #1004672 // LUA For Text Recognition with Variation 2 (adding Java callback) [WORKS]

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

Uses 9620K of libraries. Click here for Pure Java version (2173L/14K/48K).

!752

static Map<BWImage, S> chars;

!include #1000522 // helper functions

static int luaMaxSteps = 100*1000*1000;

sS code = [[
  -- Input:
  --   iw = image of word to recognize
  --   chars = images of characters & description
  -- Output:
  --   what is recognized (a string)
  
  local threshold = 0.95
  
  -- main algorithm
  
  local buf = {}
  
  for x=0, iw.w-1 do
    local best, bestChar, bestCharImg = 0, nil, nil
    for ic, c in pairs(chars) do
      local score = roughMatch(ic, iw, x)
      if score > best then
        bestChar = c
        bestCharImg = ic
        best = score
      end
    end
    if bestChar ~= nil and best > threshold then
      table.insert(buf, bestChar)
      x = x + bestCharImg.w-2
    end
  end
  
  return table.concat(buf)
  
  -- done
]];

static bool prepareNext() {
  L<S> lines = toLines(code);
  S txt = "local threshold = ";
  int i = indexOfElementThatContains(lines, txt);
  if (i < 0) false;
  lines.set(i, print("    " + txt + formatDouble(random(0.96, 1.0), 8)));
  code = fromLines(lines);
  //printCode();
  ret true;
}

static void printCode() {
  print(code); // to do: with line numbers
}

static S getParams() {
  ret structure(litmap("code", code));
}

static S recognizeWord(BWImage iw, O infos) {
  chars = (Map) quickImport(get(infos, "chars"));
  luaMaxSteps(luaMaxSteps);
  ret evalLua(makeSandbox(iw), code).tojstring();
}

static Sandbox makeSandbox(final BWImage img) {
  new LuaTable lChars;
  int i = 0;
  for (BWImage ic : keys(chars))
    lChars.set(bwImageToLua(ic), Lua.value(chars.get(ic)));

  ret luaSandbox(
    "iw", bwImageToLua(img),
    "chars", lChars,
    "roughMatch", new ThreeArgFunction {
      public LuaValue call(LuaValue ic, LuaValue iw, LuaValue x1) {
        BWImage _ic = bwImageFromLua(ic);
        BWImage _iw = bwImageFromLua(iw);
        // TODO: optimize
        ret LuaValue.valueOf(
          1-diff(_ic, _iw.clip(x1.toint(), 0, _ic.getWidth(), _ic.getHeight())));
      }
    });
}

Author comment

Began life as a copy of #1004671

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: #1004672
Snippet name: LUA For Text Recognition with Variation 2 (adding Java callback) [WORKS]
Eternal ID of this version: #1004672/1
Text MD5: 20674a6be4b00fb283e301b43be0ffee
Transpilation MD5: 0f48295cec00366a8b87bb0e78762c6c
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-25 19:37:19
Source code size: 2078 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 446 / 493
Referenced in: [show references]