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).

!752

concepts.

concept Example {
  BWImage image;
  S imageMD5;
  S text;
  
  // new fields in this program:
  L<Rect> rects;
  BWImage visualizedSplit;
  S guessedText;
  //bool countMatch;
}

static MultiMap<S, S> learnedChars;

p {
  loadConceptsFrom("#1006005");
  Concepts in = mainConcepts;
  
  for (Example e : in.list(Example))
    e.rects = horizontalAutoSplit2ThenAutoCrop(e.image);

  showConceptsTable_dropFields.set(ll("imageMD5"));
  final JTable table = showConceptsTable(Example);
  setFrameTitle(table, "Recognizing texts");
  tablePopupMenu(table, voidfunc(JPopupMenu menu, int row) {
    final Example e = (Example) getConcept((long) getTableCell(table, row, 0));
    addMenuItem(menu, "Copy single character to clipboard...", r {
      final JTextField tf = jTextField("1");
      showForm("Character index (1 to " + l(e.rects) + ")",  tf, r {
        copyBWImageToClipboard(e.image.clip(e.rects.get(parseInt(trim(tf.getText()))-1)));
      });
    });
  });
  
  learnedChars = getLearnedChars();
  
  for (Example e : in.list(Example))
    guess(e);

  for (Example e : in.list(Example)) {
    // first, auto-split
    cset(e, visualizedSplit := new BWImage(mergeImagePartsHorizontally(e.image.toRGB(), e.rects)));
    
    new StringBuilder buf;
    for (Rect r : e.rects) {
      BWImage cImg = e.image.clip(r);
      S md5 = md5OfBWImage(cImg);
      L<S> l = learnedChars.get(md5);
      S c = or(first(l), "?");
      buf.append(c);
    }
    cset(e, guessedText := str(buf));
  }
}

svoid guess(Example e) {
  if (nempty(e.text))
    learnedChars.setPut(e.imageMD5, e.text);
    
  L<Rect> rects = horizontalAutoSplit2ThenAutoCrop(e.image);
  S text = e.text.replace(" ", "");
  bool countMatch = l(rects) == l(text);
  if (!countMatch) ret;
  print("Guessing " + text);
  for i over rects: {
    Rect r = rects.get(i);
    BWImage cImg = e.image.clip(r);
    S md5 = md5OfBWImage(cImg);
    learnedChars.setPut(md5, substring(text, i, 1));
  }
}

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: 422 / 514
Referenced in: