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

164
LINES

< > BotCompany Repo | #1006187 // Show & recognize v2 (as class)

JavaX module [tags: use-pretranspiled]

Download Jar. Libraryless. Click here for Pure Java version (14991L/111K/373K).

1  
!7
2  
3  
concept GrabbedImage {
4  
  long lastScreenshotTime;
5  
  Rect lastRect;
6  
  BWImage image;
7  
  S md5;
8  
  int count;
9  
  
10  
  BWImage visualizedSplit;
11  
  new L<Rect> rects;
12  
  new L<BWImage> characters;
13  
  new L<S> characterMD5s;
14  
  L<S> groupedRecognition;
15  
  S recognition;
16  
}
17  
18  
concept RecogInfo {
19  
  new L<S> lines;
20  
}
21  
22  
concept SeedLoaded {
23  
  S snippetID;
24  
}
25  
26  
static SimpleRecognizer sr;
27  
sbool fullSearch, closeAfterTeach;
28  
29  
p {
30  
  prepare();
31  
  new ShowAndRecognize(new Concepts("#1005951").load());
32  
}
33  
34  
svoid prepare() {
35  
  setDBProgramID("#1006104"); // use DB of v1
36  
  conceptsAndBot();
37  
  //deleteConcepts(GrabbedImage); // legacy?
38  
  
39  
  loadSeed(#3000425);
40  
  
41  
  sr = new SimpleRecognizer;
42  
  sr.load(joinLines(uniq(RecogInfo).lines));
43  
  if (fullSearch) {
44  
    print("Enabling full search...");
45  
    sr.fullSearchMap = reverseMap(getLearnedCharacters2());
46  
    print("...done.");
47  
  }
48  
}
49  
50  
svoid loadSeed(S snippetID) {
51  
  snippetID = fsI(snippetID);
52  
  if (!hasConcept(SeedLoaded, snippetID)) {
53  
    print("Seeding with " + snippetID + "...");
54  
    RecogInfo ri = uniq(RecogInfo);
55  
    int l = l(ri.lines);
56  
    setAddAllFirstQuick(ri.lines, toLinesFullTrim(loadSnippet(snippetID)));
57  
    l = l(ri.lines)-l;
58  
    print("Got " + n(l, "new lines"));
59  
    if (l != 0)
60  
      ri.change();
61  
    uniq(SeedLoaded, snippetID);
62  
  }
63  
}
64  
65  
sclass ShowAndRecognize {
66  
  Concepts in; // contains GrabbedImage's
67  
  JTable table;
68  
  
69  
  *(Concepts *in) {
70  
    update();
71  
  }
72  
73  
  void update {
74  
    try {
75  
      if (table != null) {
76  
        frameIcon(table, "#1005541");
77  
        setFrameTitle(table, "Calculating - Recognitions");
78  
      }
79  
      new L<L> data;
80  
      for (GrabbedImage gi : sortByFieldDesc(in.list(GrabbedImage), "lastScreenshotTime")) {
81  
        analyzeGrabbedImage(gi, sr);
82  
        gi.change();
83  
        data.add(ll(gi.md5, gi.image.getBufferedImage(), gi.recognition, gi.visualizedSplit.getBufferedImage()));
84  
      }
85  
      
86  
      // sort by length of recognized text (longest first)
87  
      data = sortedDescAccordingTo(data, func(L l) { l(_get(l, 2)) });
88  
      
89  
      bool first = table == null;
90  
      sexyTable_drag = false;
91  
      table = showTableWithImages("Recognitions", splitAtSlash("MD5/Image/Recognition/Split"), data, table);
92  
93  
      if (!first) ret;
94  
      
95  
      awt {
96  
        maximizeFrame(table);
97  
        table.setRowHeight(40);
98  
        tablePopupMenuFirst(table, voidfunc(JPopupMenu menu, final Int row) {
99  
          addMenuItem(menu, "Teach...", r { teachRow(row) });
100  
          addMenuItem(menu, "Mark on screen", r-thread {
101  
            Rect r;
102  
            pointArrowsToArea(r = parseRect(getTableLine(table, row).get(3)));
103  
            print("Marking: " + r);
104  
          });
105  
        });
106  
        
107  
        onEnterAndDoubleClick(table, voidfunc(Int row) { teachRow(row); });
108  
      
109  
        gcAndPrintMemoryInfo();
110  
        
111  
        awtCalcOnConceptChanges(table, 1000, 1000, r { update() }, false);
112  
      }
113  
    } finally {
114  
      frameIcon(table, null);
115  
      setFrameTitle(table, "Recognitions");
116  
    }
117  
  }
118  
  
119  
  void teachRow(int row) {
120  
    S md5 = cast getTableCell(table, row, 0);
121  
    final GrabbedImage gi = findConcept(in, GrabbedImage, +md5);
122  
    print("characterMD5s: " + joinWithSpace(gi.characterMD5s));
123  
    final JTextField tf = jTypeWriterTextField(ocr_joinAsGrouped(gi.groupedRecognition));
124  
    final JCheckBox grouped = jCheckBox("grouped (" + ocrCharGroupingHelpText() + ")", true);
125  
    final JButton btnSubmit = jbutton("Submit", r {
126  
      S s = trim(tf.getText());
127  
      bool group = isChecked(grouped);
128  
      L<Rect> rects = gi.rects;
129  
      if (group) {
130  
        rects = ocr_groupRects(rects, s);
131  
        s = ocr_unmerge(s);
132  
      }
133  
      saveScreenClip(gi.image);
134  
      L<S> characterMD5s = md5OfBWImageParts(gi.image, rects);
135  
      
136  
      if (sr.fullSearchMap != null)
137  
        for i over rects:
138  
          sr.fullSearchMap.put(gi.image.clip(rects.get(i)), characterMD5s.get(i));
139  
        
140  
      S line = "The images " + quote(join(" ", characterMD5s)) + " are the " + (group ? "grouped " : "") + "characters " + quote(s) + ".";
141  
      print(">> " + line);
142  
      logQuoted(glyphTeachLog(), "[" + localDateWithSeconds() + "] Image " + ocrMD5OfBWImage(gi.image) + ": " + line);
143  
      RecogInfo ri = uniq(RecogInfo);
144  
      if (setAddOrMoveToEnd(ri.lines, line)) {
145  
        print("Feeding to recognizer.");
146  
        sr.load(line);
147  
        ri.change();
148  
      }
149  
      disposeFrame(tf);
150  
      if (closeAfterTeach) disposeFrame(table);
151  
    });
152  
    onUpdateAndNow(ll(tf, grouped), r {
153  
      S s = trim(tf.getText());
154  
      btnSubmit.setEnabled((isChecked(grouped) ? ocr_glyphCount(s) : l(dropSpaces(s))) == l(gi.characters));
155  
    });
156  
    growFrameSouth(50, showFormTitled("Teach",
157  
      "Image:" , jscroll(zoomedImageSurface(2, gi.image.getBufferedImage())),
158  
      "Split:" , jscroll(zoomedImageSurface(2, gi.visualizedSplit.getBufferedImage())),
159  
      "Meaning:", tf,
160  
      "", grouped,
161  
      "", btnSubmit));
162  
    awtLater(tf, 100, r { print("select all"); tf.selectAll() });
163  
  }
164  
}

Author comment

Began life as a copy of #1006104

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: #1006187
Snippet name: Show & recognize v2 (as class)
Eternal ID of this version: #1006187/30
Text MD5: 4bcd81bccb6b5818583013872635a8d6
Transpilation MD5: ff5094524a6d00b7b9622f295225fec1
Author: stefan
Category: javax / gui / ocr
Type: JavaX module
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-13 16:52:52
Source code size: 5104 bytes / 164 lines
Pitched / IR pitched: No / No
Views / Downloads: 859 / 2097
Version history: 29 change(s)
Referenced in: [show references]