Libraryless. Click here for Pure Java version (2205L/14K/49K).
1 | !752 |
2 | |
3 | static Map<BWImage, S> chars; |
4 | static bool debug; |
5 | |
6 | !include #1000522 |
7 | |
8 | p { |
9 | chars = loadGlyphSheet("#1004645"); |
10 | Map<BWImage, S> words = loadGlyphSheet("#1004649"); |
11 | chars = mapValues("unChar", chars); |
12 | |
13 | printStructure(values(words)); |
14 | |
15 | double score = 0, total = 0; |
16 | for (BWImage img : keys(words)) { |
17 | debug = false; |
18 | S w; |
19 | try { |
20 | w = recognizeWord(img); |
21 | } catch e { |
22 | w = exceptionToStringShort(e); |
23 | } |
24 | S real = words.get(img); |
25 | print(real + " => " + w); |
26 | ++total; |
27 | if (eq(real, w)) { |
28 | ++score; |
29 | print(" ok!"); |
30 | } else { |
31 | print(" oops..."); |
32 | debug = true; |
33 | try { |
34 | recognizeWord(img); |
35 | } catch {} |
36 | } |
37 | } |
38 | |
39 | print(); |
40 | print("SCORE: " + formatDouble(score, 1) + "/" + formatDouble(total, 1)); |
41 | } |
42 | |
43 | static S recognizeWord(BWImage iw) { |
44 | int w = iw.getWidth(); |
45 | new StringBuilder buf; |
46 | xloop: for (int x = 0; x < w; x++) { |
47 | if (debug) |
48 | print("Best match at " + x + ": " + structure(bestMatch(iw, x))); |
49 | for (BWImage ic : keys(chars)) { |
50 | if (preciseMatch(ic, iw, x)) { |
51 | buf.append(chars.get(ic)); |
52 | x += ic.getWidth()-1; |
53 | continue xloop; |
54 | } |
55 | } |
56 | } |
57 | ret str(buf); |
58 | } |
59 | |
60 | sclass Lowest<A> { |
61 | A a; |
62 | double score; |
63 | |
64 | void update(A x, double s) { |
65 | if (a == null || s < score) { |
66 | a = x; |
67 | score = s; |
68 | } |
69 | } |
70 | |
71 | Pair<A, Double> getPair() { |
72 | ret a == null ? null : pair(a, score); |
73 | } |
74 | } |
75 | |
76 | static Pair<S, Double> bestMatch(BWImage iw, int x) { |
77 | new Lowest<S> best; |
78 | for (BWImage ic : keys(chars)) { |
79 | double d = diff(ic, iw.clip(x, 0, ic.getWidth(), ic.getHeight())); |
80 | best.update(chars.get(ic), d); |
81 | } |
82 | ret best.getPair(); |
83 | } |
84 | |
85 | static bool preciseMatch(BWImage ic, BWImage iw, int x1) { |
86 | int w = iw.getWidth(), h = iw.getHeight(); |
87 | if (ic.getHeight() != h) false; |
88 | int x2 = x1+ic.getWidth(); |
89 | if (x2 > w) false; |
90 | for (int x = x1; x < x2; x++) |
91 | for (int y = 0; y < h; y++) |
92 | if (ic.getPixel(x-x1, y) != iw.getPixel(x, y)) false; |
93 | true; |
94 | } |
95 | |
96 | static S unChar(S s) { |
97 | try { |
98 | int i = s.indexOf('-'); |
99 | if (i > 0) s = substring(s, 0, i); |
100 | ret str((char) hexToInt(s)); |
101 | } catch { |
102 | ret "[" + s + "]"; |
103 | } |
104 | } |
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: | 715 / 748 |
Referenced in: | [show references] |