Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

75
LINES

< > BotCompany Repo | #1006093 // Learn to read - recognize [v2, dev.]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (8747L/60K/207K).

1  
!752
2  
3  
concepts.
4  
5  
concept Example {
6  
  BWImage image;
7  
  S imageMD5;
8  
  S text;
9  
  
10  
  // new fields in this program:
11  
  L<Rect> rects;
12  
  BWImage visualizedSplit;
13  
  S guessedText;
14  
  //bool countMatch;
15  
}
16  
17  
static MultiMap<S, S> learnedChars;
18  
19  
p {
20  
  loadConceptsFrom("#1006005");
21  
  Concepts in = mainConcepts;
22  
  
23  
  for (Example e : in.list(Example))
24  
    e.rects = horizontalAutoSplit2ThenAutoCrop(e.image);
25  
26  
  showConceptsTable_dropFields.set(ll("imageMD5"));
27  
  final JTable table = showConceptsTable(Example);
28  
  setFrameTitle(table, "Recognizing texts");
29  
  tablePopupMenu(table, voidfunc(JPopupMenu menu, int row) {
30  
    final Example e = (Example) getConcept((long) getTableCell(table, row, 0));
31  
    addMenuItem(menu, "Copy single character to clipboard...", r {
32  
      final JTextField tf = jTextField("1");
33  
      showForm("Character index (1 to " + l(e.rects) + ")",  tf, r {
34  
        copyBWImageToClipboard(e.image.clip(e.rects.get(parseInt(trim(tf.getText()))-1)));
35  
      });
36  
    });
37  
  });
38  
  
39  
  learnedChars = getLearnedChars();
40  
  
41  
  for (Example e : in.list(Example))
42  
    guess(e);
43  
44  
  for (Example e : in.list(Example)) {
45  
    // first, auto-split
46  
    cset(e, visualizedSplit := new BWImage(mergeImagePartsHorizontally(e.image.toRGB(), e.rects)));
47  
    
48  
    new StringBuilder buf;
49  
    for (Rect r : e.rects) {
50  
      BWImage cImg = e.image.clip(r);
51  
      S md5 = md5OfBWImage(cImg);
52  
      L<S> l = learnedChars.get(md5);
53  
      S c = or(first(l), "?");
54  
      buf.append(c);
55  
    }
56  
    cset(e, guessedText := str(buf));
57  
  }
58  
}
59  
60  
svoid guess(Example e) {
61  
  if (nempty(e.text))
62  
    learnedChars.setPut(e.imageMD5, e.text);
63  
    
64  
  L<Rect> rects = horizontalAutoSplit2ThenAutoCrop(e.image);
65  
  S text = e.text.replace(" ", "");
66  
  bool countMatch = l(rects) == l(text);
67  
  if (!countMatch) ret;
68  
  print("Guessing " + text);
69  
  for i over rects: {
70  
    Rect r = rects.get(i);
71  
    BWImage cImg = e.image.clip(r);
72  
    S md5 = md5OfBWImage(cImg);
73  
    learnedChars.setPut(md5, substring(text, i, 1));
74  
  }
75  
}

Author comment

Began life as a copy of #1006062

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006093
Snippet name: Learn to read - recognize [v2, dev.]
Eternal ID of this version: #1006093/1
Text MD5: a82ad654c6f5076d9669a0e9a5a090e4
Transpilation MD5: 7a08c4a4a9131380d9893a2b549aaf48
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-18 02:53:42
Source code size: 2052 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 417 / 507
Referenced in: [show references]