Libraryless. Click here for Pure Java version (2205L/14K/49K).
!752 static Map<BWImage, S> chars; static bool debug; !include #1000522 p { chars = loadGlyphSheet("#1004645"); Map<BWImage, S> words = loadGlyphSheet("#1004649"); chars = mapValues("unChar", chars); printStructure(values(words)); double score = 0, total = 0; for (BWImage img : keys(words)) { debug = false; S w; try { w = recognizeWord(img); } catch e { w = exceptionToStringShort(e); } S real = words.get(img); print(real + " => " + w); ++total; if (eq(real, w)) { ++score; print(" ok!"); } else { print(" oops..."); debug = true; try { recognizeWord(img); } catch {} } } print(); print("SCORE: " + formatDouble(score, 1) + "/" + formatDouble(total, 1)); } static S recognizeWord(BWImage iw) { int w = iw.getWidth(); new StringBuilder buf; xloop: for (int x = 0; x < w; x++) { if (debug) print("Best match at " + x + ": " + structure(bestMatch(iw, x))); for (BWImage ic : keys(chars)) { if (preciseMatch(ic, iw, x)) { buf.append(chars.get(ic)); x += ic.getWidth()-1; continue xloop; } } } ret str(buf); } sclass Lowest<A> { A a; double score; void update(A x, double s) { if (a == null || s < score) { a = x; score = s; } } Pair<A, Double> getPair() { ret a == null ? null : pair(a, score); } } static Pair<S, Double> bestMatch(BWImage iw, int x) { new Lowest<S> best; for (BWImage ic : keys(chars)) { double d = diff(ic, iw.clip(x, 0, ic.getWidth(), ic.getHeight())); best.update(chars.get(ic), d); } ret best.getPair(); } static bool preciseMatch(BWImage ic, BWImage iw, int x1) { int w = iw.getWidth(), h = iw.getHeight(); if (ic.getHeight() != h) false; int x2 = x1+ic.getWidth(); if (x2 > w) false; for (int x = x1; x < x2; x++) for (int y = 0; y < h; y++) if (ic.getPixel(x-x1, y) != iw.getPixel(x, y)) false; true; } static S unChar(S s) { try { int i = s.indexOf('-'); if (i > 0) s = substring(s, 0, i); ret str((char) hexToInt(s)); } catch { ret "[" + s + "]"; } }
Began life as a copy of #1004643
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: | #1004646 |
Snippet name: | Try To Recognize Font From Glyph Table |
Eternal ID of this version: | #1004646/1 |
Text MD5: | f01946320e96cf02f356bc13b14ae33d |
Transpilation MD5: | 6ea852c49ddea4a3f61a539cd83776ae |
Author: | stefan |
Category: | javax / images |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-08-24 21:39:48 |
Source code size: | 2282 bytes / 104 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 704 / 734 |
Referenced in: | #1004654 - Algorithm Sucker - Recognizing "Ticketing" (works) |