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

1  
!752
2  
3  
static Map<BWImage, S> chars;
4  
5  
!include #1000522 // helper functions
6  
7  
static int luaMaxSteps = 100*1000*1000;
8  
9  
sS code = [[
10  
  -- Input:
11  
  --   iw = image of word to recognize
12  
  --   chars = images of characters & description
13  
  -- Output:
14  
  --   what is recognized (a string)
15  
  
16  
  local threshold = 0.95
17  
  
18  
  -- main algorithm
19  
  
20  
  local buf = {}
21  
  
22  
  for x=0, iw.w-1 do
23  
    local best, bestChar, bestCharImg = 0, nil, nil
24  
    for ic, c in pairs(chars) do
25  
      local score = roughMatch(ic, iw, x)
26  
      if score > best then
27  
        bestChar = c
28  
        bestCharImg = ic
29  
        best = score
30  
      end
31  
    end
32  
    if bestChar ~= nil and best > threshold then
33  
      table.insert(buf, bestChar)
34  
      x = x + bestCharImg.w-2
35  
    end
36  
  end
37  
  
38  
  return table.concat(buf)
39  
  
40  
  -- done
41  
]];
42  
43  
static bool prepareNext() {
44  
  L<S> lines = toLines(code);
45  
  S txt = "local threshold = ";
46  
  int i = indexOfElementThatContains(lines, txt);
47  
  if (i < 0) false;
48  
  lines.set(i, print("    " + txt + formatDouble(random(0.96, 1.0), 8)));
49  
  code = fromLines(lines);
50  
  //printCode();
51  
  ret true;
52  
}
53  
54  
static void printCode() {
55  
  print(code); // to do: with line numbers
56  
}
57  
58  
static S getParams() {
59  
  ret structure(litmap("code", code));
60  
}
61  
62  
static S recognizeWord(BWImage iw, O infos) {
63  
  chars = (Map) quickImport(get(infos, "chars"));
64  
  luaMaxSteps(luaMaxSteps);
65  
  ret evalLua(makeSandbox(iw), code).tojstring();
66  
}
67  
68  
static Sandbox makeSandbox(final BWImage img) {
69  
  new LuaTable lChars;
70  
  int i = 0;
71  
  for (BWImage ic : keys(chars))
72  
    lChars.set(bwImageToLua(ic), Lua.value(chars.get(ic)));
73  
74  
  ret luaSandbox(
75  
    "iw", bwImageToLua(img),
76  
    "chars", lChars,
77  
    "roughMatch", new ThreeArgFunction {
78  
      public LuaValue call(LuaValue ic, LuaValue iw, LuaValue x1) {
79  
        BWImage _ic = bwImageFromLua(ic);
80  
        BWImage _iw = bwImageFromLua(iw);
81  
        // TODO: optimize
82  
        ret LuaValue.valueOf(
83  
          1-diff(_ic, _iw.clip(x1.toint(), 0, _ic.getWidth(), _ic.getHeight())));
84  
      }
85  
    });
86  
}

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