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

70
LINES

< > BotCompany Repo | #1004669 // LUA For Text Recognition

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

Uses 9620K of libraries. Click here for Pure Java version (1402L/9K/31K).

!752

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

static int luaMaxSteps = 10*1000*1000;

sS code = [[
  -- Input:
  --   iw = image of word to recognize
  --   chars = images of characters & description
  -- Output:
  --   what is recognized (a string)
  
  -- helper function
  function preciseMatch(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
    for x = x1, x2-1 do
      for y = 0, h-1 do
        if ic.getBrightness(x-x1, y) ~= iw.getBrightness(x, y) then
          return false
        end
      end
    end
    return true
  end
  
  -- main algorithm
  
  local buf = {}
  
  for x=0, iw.w-1 do
    for ic, c in pairs(chars) do
      if preciseMatch(ic, iw, x) then
        table.insert(buf, c)
        x = x + ic.w-1
        goto xloop
      end
    end
    ::xloop::
  end
  
  return table.concat(buf)
  
  -- done
]];

static S recognizeWord(BWImage iw, O infos) {
  chars = (Map) quickImport(get(infos, "chars"));
  debug = isTrue(getOpt(infos, "debug"));
  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(++i, bwImageToLua(ic));
    //lChars.set(++i, Lua.value(chars.get(ic)));
    lChars.set(bwImageToLua(ic), Lua.value(chars.get(ic)));
  }

  ret luaSandbox(
    "iw", bwImageToLua(img),
    "chars", lChars);
}

Author comment

Began life as a copy of #1004663

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004669
Snippet name: LUA For Text Recognition
Eternal ID of this version: #1004669/1
Text MD5: 37d67ec7f36cefa2f394cb487763a09b
Transpilation MD5: d3ffc989bc947cc576480b44f1e1b67c
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:03:04
Source code size: 1558 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 631 / 661
Referenced in: [show references]