!752 static Map chars; 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 -- helper function function roughMatch(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 local d = 0 for x = x1, x2-1 do for y = 0, h-1 do d = d + math.abs(ic.getBrightness(x-x1, y)-iw.getBrightness(x, y)) end end return 1-d/((x2-x1)*h) end -- main algorithm local buf = {} for x=0, iw.w-1 do for ic, c in pairs(chars) do if roughMatch(ic, iw, x) > threshold then table.insert(buf, c) x = x + ic.w-1 goto xloop end end ::xloop:: end return table.concat(buf) -- done ]]; static bool again() { ret false; } 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); }