Libraryless. Click here for Pure Java version (10176L/72K/244K).
1 | !7 |
2 | |
3 | static double spaceWidth = 8; |
4 | |
5 | // typical line dists: 6, 21 |
6 | static float lineDist1 = 6, lineDist2 = 22; |
7 | static float lineHeight = lineDist2-lineDist1; |
8 | |
9 | p-type { |
10 | RGBImage img = rgbShootScreenHidingConsole(; |
11 | Rect r = ocr_findSnippetSource(img; |
12 | |
13 | if (r == null) { |
14 | print("No snippet source found."; |
15 | ret; |
16 | } |
17 | print("Scanning: " + r; |
18 | |
19 | BWImage bwImage = new BWImage(img.clip(r; |
20 | L<Rect> clips = autoSegment(bwImage; |
21 | |
22 | L<Rect> r2 = ocr_mergeOverlappingY(clips; |
23 | //L<Rect> r2 = ocr_mergeOverlappingY_plusPercent(clips, 7; |
24 | r2 = sortByField(r2, "y"); |
25 | |
26 | /*for (Rect rx : r2) { |
27 | print("h=" + rx.h + ". " + struct(bwImageRowBrightnesses(bwImage.clip(rx; |
28 | }*/ |
29 | |
30 | // Split rows |
31 | for i over r2: { |
32 | Rect rx = r2.get(i; |
33 | BWImage cImg = bwImage.clip(rx); |
34 | int h = rx.h/2; |
35 | //float[] rows = bwImageRowBrightnesses(bwImage.clip(rx)); |
36 | //float b = rows[l(rows)/2]; |
37 | float b = (float) cImg.clip(0, h, rx.w, 1).averageBrightness(); |
38 | if (b > 0.93f) { |
39 | Rect upper = new Rect(rx.x, rx.y, rx.w, h); |
40 | Rect lower = new Rect(rx.x, rx.y+h, rx.w, rx.h-h); |
41 | r2.set(i, lower); |
42 | r2.add(i++, upper); |
43 | } |
44 | } |
45 | |
46 | // Merge rows |
47 | for (int i = 0; i < l(r2)-1; i++) { |
48 | Rect a = r2.get(i), b = r2.get(i+1); |
49 | if (b.h < a.h/2 && (b.y-a.y2()) < 2) { // fix the dangling underscore |
50 | r2.set(i, mergeRects(a, b)); |
51 | r2.remove(i+1); |
52 | } |
53 | } |
54 | |
55 | r2 = bwAutoCropClips(bwImage, r2); |
56 | |
57 | showImageWithSelections(bwImage.getBufferedImage(), r2; |
58 | focusConsole(); |
59 | growToTop(consoleFrame()); |
60 | enableFullSearchInSimpleRecognizer(filledSimpleRecognizer_cached()); |
61 | L<S> lines = ocr_recognizeTextClips(bwImage, r2; |
62 | |
63 | lines = map(f ocr_unescape, lines); |
64 | |
65 | addIndents(lines, r2); |
66 | addEmptyRows(lines, r2); |
67 | |
68 | printNumbered_flushed(lines; |
69 | } |
70 | |
71 | svoid addIndents(L<S> lines, L<Rect> rects) { |
72 | int n = l(lines); |
73 | assertEquals(n, l(rects)); |
74 | int leftBorder = toInt(collectMinInt(rects, "x")); |
75 | for i to n: { |
76 | int dist = rects.get(i).x-leftBorder; |
77 | print("dist=" + dist); |
78 | int spaces = iround(dist/spaceWidth; |
79 | if (spaces > 0) |
80 | lines.set(i, rep(spaces, ' ') + lines.get(i)); |
81 | } |
82 | } |
83 | |
84 | svoid addEmptyRows(L<S> lines, L<Rect> rects) { |
85 | int n = l(lines); |
86 | assertEquals(n, l(rects)); |
87 | for (int i = 0; i+1 < n; i++) { |
88 | Rect a = rects.get(i), b = rects.get(i+1); |
89 | if (a != null && b != null) { |
90 | int dist = b.y-a.y2(); |
91 | float frows = (dist-lineDist1)/lineHeight; |
92 | int rows = iround(frows); |
93 | print("dist=" + dist + ", rows=" + frows); |
94 | if (rows > 0) { |
95 | lines.addAll(i+1, rep("", rows)); |
96 | rects.addAll(i+1, rep((Rect) null, rows)); |
97 | i += rows-1; |
98 | } |
99 | } |
100 | } |
101 | } |
Began life as a copy of #1006334
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1006341 |
Snippet name: | Find snippet source on screen, recognize, step 2 (dev.) |
Eternal ID of this version: | #1006341/3 |
Text MD5: | 96f857cc4ae2d4b320302059f395b932 |
Transpilation MD5: | 2577054484bd49ae0de392a5aaded4bb |
Author: | stefan |
Category: | javax / ocr |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-05-11 00:59:02 |
Source code size: | 2775 bytes / 101 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 692 / 1115 |
Version history: | 2 change(s) |
Referenced in: | [show references] |