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

144
LINES

< > BotCompany Repo | #1006246 // class FindURLBoxApp - Record browser's URL box with feedback [works]

JavaX fragment (include)

1  
// For now, leave out of inner class.
2  
3  
concept BoxShot {
4  
  Rect r;
5  
  S imageMD5;
6  
  //byte[] png;
7  
  new RefL<TimeRange> timeRanges;
8  
}
9  
10  
concept Evaluation {
11  
  new Ref<BoxShot> shot;
12  
  S comment;
13  
}
14  
15  
concept Boxes {
16  
  new L<Rect> encouragedBoxes;
17  
  new L<Rect> discouragedBoxes;
18  
}
19  
20  
sclass FindURLBoxApp {
21  
  int sleepTimeWhenNotFound = 5000;
22  
  static int frameHeight = 100;
23  
24  
  volatile bool recognizing = true;
25  
  JFrame frame;
26  
  SingleComponentPanel scp;
27  
  ImageSurface imageSurface;
28  
  FindURLBox1 finder;
29  
  S title = "Find URL Box";
30  
31  
  BoxShot shot;
32  
  TimeRange timeRange;
33  
34  
  Evaluation > WrongResult {} // This seems to work as inner-inner class, just syntax enhancements get confused
35  
  Evaluation > GoodResult {}
36  
37  
  void init() {
38  
    // clean up old field
39  
    for (BoxShot shot) {
40  
      if (shot.fieldValues.containsKey("png")) {
41  
        cset(shot, png := null);
42  
        print("Cleared PNG " + shot.id);
43  
      }
44  
    }
45  
    
46  
    swingAndWait(r {
47  
      finder = new FindURLBox1;
48  
      JComponent feedback = jRightAlignedLine(
49  
        jlabel("Is this the right box?"),
50  
          jbutton("Yes", r { yes() }),
51  
          jbutton("No", r { no() }),
52  
          jbutton("Start over", r { startOver() }));
53  
      scp = new SingleComponentPanel(jcenteredLabel("-"));
54  
      frame = showFrame(title, centerAndSouth(scp, feedback));
55  
      setRecognizing(true);
56  
      frameInnerSize(frame, 500, frameHeight);
57  
      moveToBottomRightCorner(frame);
58  
      frame.setAlwaysOnTop(true);
59  
      
60  
      awtCalcRegularly(frame, 200, 0, r { calc() });
61  
    });
62  
  }
63  
  
64  
  void calc {
65  
    if (!recognizing) ret; // paused
66  
    finder.encouragedBoxes = uniq(Boxes).encouragedBoxes;
67  
    finder.discouragedBoxes = uniq(Boxes).discouragedBoxes;
68  
    final bool b = finder.go();
69  
    awt {
70  
      if (b) {
71  
        RGBImage img = finder.bestImage();
72  
        S since = "";
73  
        if (shot != null && timeRange != null)
74  
          since = ", since " + (now()-timeRange.from)/1000 + "s, " + nth(l(shot.timeRanges)) + " appearance";
75  
        S text = "URL box found in " + finder.recogTime + " ms (" + img.w() + "*" + img.h() + " px" + since + ")";
76  
        if (imageSurface == null) {
77  
          imageSurface = new ImageSurface(img);
78  
          scp.setComponent(jSection(text, new JScrollPane(imageSurface)));
79  
        } else {
80  
          imageSurface.setImage(img);
81  
          setSectionTitle(imageSurface, text);
82  
        }
83  
        
84  
        S imageMD5 = md5OfRGBImage(img);
85  
        shot = findConcept(BoxShot, +imageMD5);
86  
        if (shot == null) {
87  
          shot = uniq(BoxShot, +imageMD5);
88  
          logStructure(getProgramFile("md5-png.log"),
89  
            litorderedmap(+imageMD5, png := rgbImageToPNG(img)));
90  
        }
91  
        cset(shot, r := finder.bestClip);
92  
        if (shot.timeRanges.contains(timeRange))
93  
          timeRange.addNow();
94  
        else
95  
          shot.timeRanges.add(timeRange = new TimeRange(now()));
96  
        shot.change();
97  
      } else {
98  
        shot = null;
99  
        timeRange = null;
100  
        if (imageSurface != null) {
101  
          imageSurface = null;
102  
          scp.setComponent(jcenteredLabel("URL box not found (" + finder.recogTime + " ms)"));
103  
        }
104  
      }
105  
    }
106  
    if (!b) sleep(sleepTimeWhenNotFound);
107  
  }
108  
109  
  void yes {
110  
    print("yes");
111  
    if (addOrMoveToStart(uniq(Boxes).encouragedBoxes, shot.r))
112  
      uniq(Boxes).change();
113  
    feedback(new GoodResult);
114  
  }
115  
  
116  
  void no {
117  
    if (addOrMoveToStart(uniq(Boxes).discouragedBoxes, shot.r))
118  
      uniq(Boxes).change();
119  
    feedback(new WrongResult);
120  
  }
121  
  
122  
  void startOver {
123  
    uniq(Boxes).encouragedBoxes.clear();
124  
    uniq(Boxes).discouragedBoxes.clear();
125  
    uniq(Boxes).change();
126  
    finder.reset();
127  
  }
128  
  
129  
  void feedback(final Evaluation ev) {
130  
    finder.reset();
131  
    thread "Feedback" {
132  
      cset(ev, +shot);
133  
      //infoMessage("Thank you, " + (ev instanceof WrongResult ? "negative" : "positive") + " feedback recorded. (id=" + ev.id + ")");
134  
      mainConcepts.save();
135  
    }
136  
  }
137  
  
138  
  void setRecognizing(bool b) {
139  
    recognizing = b;
140  
    frameIcon(frame, b ? #1006287 : #1006286);
141  
    setFrameTitle(frame, (b ? "[Watching] " : " [Paused] ")
142  
      + title);
143  
  }
144  
}

Author comment

Began life as a copy of #1006242

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: #1006246
Snippet name: class FindURLBoxApp - Record browser's URL box with feedback [works]
Eternal ID of this version: #1006246/1
Text MD5: 8f753d2b3d7c7d44668e6f0bcd24f48d
Author: stefan
Category: javax / ocr
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-28 14:32:28
Source code size: 4247 bytes / 144 lines
Pitched / IR pitched: No / No
Views / Downloads: 518 / 930
Referenced in: [show references]