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

74
LINES

< > BotCompany Repo | #1004671 // LUA For Text Recognition with Variation

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

Uses 9620K of libraries. Click here for Pure Java version (1801L/11K/40K).

!752

static Map<BWImage, S> chars;

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
  
  -- helper function
  function roughMatch(ic, iw, x1)
    local w, h = iw.w, iw.h
    if ic.h ~= h then return false end
    local x2 = x1+ic.w
    if x2 > w then return false end
    local d = 0
    for x = x1, x2-1 do
      for y = 0, h-1 do
        d = d + math.abs(ic.getBrightness(x-x1, y)-iw.getBrightness(x, y))
      end
    end
    return 1-d/((x2-x1)*h)
  end
  
  -- main algorithm
  
  local buf = {}
  
  for x=0, iw.w-1 do
    for ic, c in pairs(chars) do
      if roughMatch(ic, iw, x) > threshold then
        table.insert(buf, c)
        x = x + ic.w-1
        goto xloop
      end
    end
    ::xloop::
  end
  
  return table.concat(buf)
  
  -- done
]];

static bool again() {
  ret false;
}

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);
}

Author comment

Began life as a copy of #1004669

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: #1004671
Snippet name: LUA For Text Recognition with Variation
Eternal ID of this version: #1004671/1
Text MD5: e6c64551971e4bad4cb62672db2ef0c6
Transpilation MD5: 56effd5eb4603465f5a1c85e4ed77bb8
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 18:53:25
Source code size: 1550 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 484 / 543
Referenced in: #1004672 - LUA For Text Recognition with Variation 2 (adding Java callback) [WORKS]