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).

1  
!752
2  
3  
static Map<BWImage, S> chars;
4  
sbool debug;
5  
6  
static int luaMaxSteps = 10*1000*1000;
7  
8  
sS code = [[
9  
  -- Input:
10  
  --   iw = image of word to recognize
11  
  --   chars = images of characters & description
12  
  -- Output:
13  
  --   what is recognized (a string)
14  
  
15  
  -- helper function
16  
  function preciseMatch(ic, iw, x1)
17  
    local w, h = iw.w, iw.h
18  
    if ic.h ~= h then return false end
19  
    local x2 = x1+ic.w
20  
    if x2 > w then return false end
21  
    for x = x1, x2-1 do
22  
      for y = 0, h-1 do
23  
        if ic.getBrightness(x-x1, y) ~= iw.getBrightness(x, y) then
24  
          return false
25  
        end
26  
      end
27  
    end
28  
    return true
29  
  end
30  
  
31  
  -- main algorithm
32  
  
33  
  local buf = {}
34  
  
35  
  for x=0, iw.w-1 do
36  
    for ic, c in pairs(chars) do
37  
      if preciseMatch(ic, iw, x) then
38  
        table.insert(buf, c)
39  
        x = x + ic.w-1
40  
        goto xloop
41  
      end
42  
    end
43  
    ::xloop::
44  
  end
45  
  
46  
  return table.concat(buf)
47  
  
48  
  -- done
49  
]];
50  
51  
static S recognizeWord(BWImage iw, O infos) {
52  
  chars = (Map) quickImport(get(infos, "chars"));
53  
  debug = isTrue(getOpt(infos, "debug"));
54  
  luaMaxSteps(luaMaxSteps);
55  
  ret evalLua(makeSandbox(iw), code).tojstring();
56  
}
57  
58  
static Sandbox makeSandbox(final BWImage img) {
59  
  new LuaTable lChars;
60  
  int i = 0;
61  
  for (BWImage ic : keys(chars)) {
62  
    //lChars.set(++i, bwImageToLua(ic));
63  
    //lChars.set(++i, Lua.value(chars.get(ic)));
64  
    lChars.set(bwImageToLua(ic), Lua.value(chars.get(ic)));
65  
  }
66  
67  
  ret luaSandbox(
68  
    "iw", bwImageToLua(img),
69  
    "chars", lChars);
70  
}

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: 508 / 528
Referenced in: [show references]