!752 static Map chars; !include #1000522 // helper functions 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 -- main algorithm local buf = {} for x=0, iw.w-1 do local best, bestChar, bestCharImg = 0, nil, nil for ic, c in pairs(chars) do local score = roughMatch(ic, iw, x) if score > best then bestChar = c bestCharImg = ic best = score end end if bestChar ~= nil and best > threshold then table.insert(buf, bestChar) x = x + bestCharImg.w-2 end end return table.concat(buf) -- done ]]; static bool prepareNext() { L lines = toLines(code); S txt = "local threshold = "; int i = indexOfElementThatContains(lines, txt); if (i < 0) false; lines.set(i, print(" " + txt + formatDouble(random(0.96, 1.0), 8))); code = fromLines(lines); //printCode(); ret true; } static void printCode() { print(code); // to do: with line numbers } 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, "roughMatch", new ThreeArgFunction { public LuaValue call(LuaValue ic, LuaValue iw, LuaValue x1) { BWImage _ic = bwImageFromLua(ic); BWImage _iw = bwImageFromLua(iw); // TODO: optimize ret LuaValue.valueOf( 1-diff(_ic, _iw.clip(x1.toint(), 0, _ic.getWidth(), _ic.getHeight()))); } }); }