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

1  
!752
2  
3  
static Map<BWImage, S> chars;
4  
5  
static int luaMaxSteps = 100*1000*1000;
6  
7  
sS code = [[
8  
  -- Input:
9  
  --   iw = image of word to recognize
10  
  --   chars = images of characters & description
11  
  -- Output:
12  
  --   what is recognized (a string)
13  
  
14  
  local threshold = 0.95
15  
  
16  
  -- helper function
17  
  function roughMatch(ic, iw, x1)
18  
    local w, h = iw.w, iw.h
19  
    if ic.h ~= h then return false end
20  
    local x2 = x1+ic.w
21  
    if x2 > w then return false end
22  
    local d = 0
23  
    for x = x1, x2-1 do
24  
      for y = 0, h-1 do
25  
        d = d + math.abs(ic.getBrightness(x-x1, y)-iw.getBrightness(x, y))
26  
      end
27  
    end
28  
    return 1-d/((x2-x1)*h)
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 roughMatch(ic, iw, x) > threshold 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 bool again() {
52  
  ret false;
53  
}
54  
55  
static S getParams() {
56  
  ret structure(litmap("code", code));
57  
}
58  
59  
static S recognizeWord(BWImage iw, O infos) {
60  
  chars = (Map) quickImport(get(infos, "chars"));
61  
  luaMaxSteps(luaMaxSteps);
62  
  ret evalLua(makeSandbox(iw), code).tojstring();
63  
}
64  
65  
static Sandbox makeSandbox(final BWImage img) {
66  
  new LuaTable lChars;
67  
  int i = 0;
68  
  for (BWImage ic : keys(chars))
69  
    lChars.set(bwImageToLua(ic), Lua.value(chars.get(ic)));
70  
71  
  ret luaSandbox(
72  
    "iw", bwImageToLua(img),
73  
    "chars", lChars);
74  
}

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