Uses 5262K of libraries. Click here for Pure Java version (3022L/21K/72K).
1 | !752 |
2 | |
3 | static S imageID = "#1004061"; |
4 | |
5 | !include #1004250 // Eye |
6 | |
7 | !include #1004067 // XRecognizableImage |
8 | |
9 | static File infoFile; |
10 | static InputImage inputImage; |
11 | static ImageInfo ii; |
12 | static XRecognizableImage ri; |
13 | static ImageReader recognizer; |
14 | static volatile L<DebugItem> debugItems; |
15 | static JRecognizedText textArea; |
16 | sbool triggerRecog; |
17 | volatile sbool recognizing; |
18 | |
19 | p { |
20 | RGBImage img = new RGBImage(loadImg(imageID)); |
21 | |
22 | // load imageInfo |
23 | ii = new ImageInfo; |
24 | infoFile = getProgramFile("imageInfo.tree"); |
25 | S iitext = loadTextFile(infoFile); |
26 | if (nempty(iitext)) |
27 | ii.getTree().set(Tree.parse(iitext)); |
28 | |
29 | ri = new XRecognizableImage; |
30 | ri.imageInfo = ii; |
31 | ri.setImage(img); |
32 | ri.setDrawMarkLines(true); |
33 | ri.setCorrections(ii.getCorrections()); |
34 | |
35 | inputImage = new InputImage(img.toBW()); |
36 | |
37 | textArea = new JRecognizedText; |
38 | loadRecognizer(); |
39 | if (recognizer == null) |
40 | recognizer = new SaR(new LineFinder, new FlexibleSegmenter, new CompareImages); |
41 | |
42 | recog(recognizer); |
43 | |
44 | learnCorrections(); |
45 | |
46 | recog(recognizer); |
47 | |
48 | JFrame frame = showFrame(centerAndSouth(vgrid(ri, textArea), |
49 | null /*jbutton("Recognize again", r { recog(); })*/)); |
50 | |
51 | swingTimer(frame, 100, r{ |
52 | if (triggerRecog && !recognizing) { triggerRecog = false; recogInBackground(); } |
53 | }); |
54 | } |
55 | |
56 | svoid saveImageInfo() { |
57 | saveTextFile(infoFile, ii.getTree().toString()); |
58 | ri.setCorrections(ii.getCorrections()); |
59 | ri.repaintImageSurface(); |
60 | } |
61 | |
62 | svoid recognizerModified() { |
63 | S s = recognizer.getDescription(); |
64 | saveTextFile(getProgramFile("recognizer.tree"), s); |
65 | print("Saved recognizer (" + l(s) + " chars)"); |
66 | triggerRecog = true; |
67 | } |
68 | |
69 | svoid loadRecognizer() { |
70 | S s = loadTextFile(getProgramFile("recognizer.tree")); |
71 | if (nempty(s)) |
72 | recognizer = OCRUtil.makeImageReader(s); |
73 | } |
74 | |
75 | svoid learnCorrections() { |
76 | S desc = recognizer.getDescription(); |
77 | CharacterLearner characterLearner = recognizer.getCharacterLearner(); |
78 | if (characterLearner != null) |
79 | for (Correction c : ii.getCorrections()) { |
80 | Rectangle r = c.getRectangle(); |
81 | Point p = new Point(r.x+r.width/2, r.y+r.height/2); |
82 | Subrecognition subrecognition = findSubrecognition(p); |
83 | continue if subrecognition == null; |
84 | ImageWithMarkLines imageWithMarkLines = |
85 | new ImageWithMarkLines(subrecognition.image, |
86 | subrecognition.topLine, subrecognition.baseLine); |
87 | characterLearner.learnCharacter(imageWithMarkLines, c.getText()); |
88 | } |
89 | if (neq(recognizer.getDescription(), desc)) |
90 | recognizerModified(); |
91 | } |
92 | |
93 | static Subrecognition findSubrecognition(Point p) { |
94 | for (DebugItem item : debugItems) |
95 | if (item.data instanceof Subrecognition) { |
96 | Subrecognition s = (Subrecognition) item.data; |
97 | Rectangle r = new Rectangle(s.clip); |
98 | r.grow(1, 1); |
99 | if (r.contains(p)) |
100 | return s; |
101 | } |
102 | null; |
103 | } |
104 | |
105 | svoid recog(ImageReader rec) { |
106 | long t = now(); |
107 | // do it! |
108 | rec.setCollectDebugInfo(true); |
109 | RecognizedText rt; |
110 | time { |
111 | rt = rec.extendedReadImage(inputImage); |
112 | } |
113 | |
114 | debugItems = rec.getDebugInfo(); |
115 | done(t, "recog"); |
116 | recogDone(rt); |
117 | } |
118 | |
119 | static ImageReader cloneRecognizer(ImageReader recognizer) { |
120 | ret OCRUtil.makeImageReader(recognizer.getDescription()); |
121 | } |
122 | |
123 | svoid recogDone(final RecognizedText rt) { |
124 | awtIfNecessary { |
125 | print("Debug items: " + l(debugItems)); |
126 | ri.setDebugInfo(debugItems); |
127 | |
128 | S text = rt != null ? rt.text : ""; |
129 | textArea.setText(text, debugItems); |
130 | |
131 | ri.repaintImageSurface(); |
132 | } |
133 | } |
134 | |
135 | svoid recogInBackground() { |
136 | recognizing = true; |
137 | final ImageReader rec = cloneRecognizer(recognizer); |
138 | thread "Recognizing" { |
139 | try { |
140 | recog(rec); |
141 | } finally { |
142 | recognizing = false; |
143 | } |
144 | } |
145 | } |
Began life as a copy of #1004066
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: | #1004068 |
Snippet name: | New Eye |
Eternal ID of this version: | #1004068/1 |
Text MD5: | 56291e2c345bd66d53ed467046ab9a83 |
Transpilation MD5: | 24037a5b19dd57fb3c002865e4fa7ade |
Author: | stefan |
Category: | javax / ocr |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-08-23 15:47:35 |
Source code size: | 3817 bytes / 145 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 761 / 1483 |
Referenced in: | [show references] |