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)

// For now, leave out of inner class.

concept BoxShot {
  Rect r;
  S imageMD5;
  //byte[] png;
  new RefL<TimeRange> timeRanges;
}

concept Evaluation {
  new Ref<BoxShot> shot;
  S comment;
}

concept Boxes {
  new L<Rect> encouragedBoxes;
  new L<Rect> discouragedBoxes;
}

sclass FindURLBoxApp {
  int sleepTimeWhenNotFound = 5000;
  static int frameHeight = 100;

  volatile bool recognizing = true;
  JFrame frame;
  SingleComponentPanel scp;
  ImageSurface imageSurface;
  FindURLBox1 finder;
  S title = "Find URL Box";

  BoxShot shot;
  TimeRange timeRange;

  Evaluation > WrongResult {} // This seems to work as inner-inner class, just syntax enhancements get confused
  Evaluation > GoodResult {}

  void init() {
    // clean up old field
    for (BoxShot shot) {
      if (shot.fieldValues.containsKey("png")) {
        cset(shot, png := null);
        print("Cleared PNG " + shot.id);
      }
    }
    
    swingAndWait(r {
      finder = new FindURLBox1;
      JComponent feedback = jRightAlignedLine(
        jlabel("Is this the right box?"),
          jbutton("Yes", r { yes() }),
          jbutton("No", r { no() }),
          jbutton("Start over", r { startOver() }));
      scp = new SingleComponentPanel(jcenteredLabel("-"));
      frame = showFrame(title, centerAndSouth(scp, feedback));
      setRecognizing(true);
      frameInnerSize(frame, 500, frameHeight);
      moveToBottomRightCorner(frame);
      frame.setAlwaysOnTop(true);
      
      awtCalcRegularly(frame, 200, 0, r { calc() });
    });
  }
  
  void calc {
    if (!recognizing) ret; // paused
    finder.encouragedBoxes = uniq(Boxes).encouragedBoxes;
    finder.discouragedBoxes = uniq(Boxes).discouragedBoxes;
    final bool b = finder.go();
    awt {
      if (b) {
        RGBImage img = finder.bestImage();
        S since = "";
        if (shot != null && timeRange != null)
          since = ", since " + (now()-timeRange.from)/1000 + "s, " + nth(l(shot.timeRanges)) + " appearance";
        S text = "URL box found in " + finder.recogTime + " ms (" + img.w() + "*" + img.h() + " px" + since + ")";
        if (imageSurface == null) {
          imageSurface = new ImageSurface(img);
          scp.setComponent(jSection(text, new JScrollPane(imageSurface)));
        } else {
          imageSurface.setImage(img);
          setSectionTitle(imageSurface, text);
        }
        
        S imageMD5 = md5OfRGBImage(img);
        shot = findConcept(BoxShot, +imageMD5);
        if (shot == null) {
          shot = uniq(BoxShot, +imageMD5);
          logStructure(getProgramFile("md5-png.log"),
            litorderedmap(+imageMD5, png := rgbImageToPNG(img)));
        }
        cset(shot, r := finder.bestClip);
        if (shot.timeRanges.contains(timeRange))
          timeRange.addNow();
        else
          shot.timeRanges.add(timeRange = new TimeRange(now()));
        shot.change();
      } else {
        shot = null;
        timeRange = null;
        if (imageSurface != null) {
          imageSurface = null;
          scp.setComponent(jcenteredLabel("URL box not found (" + finder.recogTime + " ms)"));
        }
      }
    }
    if (!b) sleep(sleepTimeWhenNotFound);
  }

  void yes {
    print("yes");
    if (addOrMoveToStart(uniq(Boxes).encouragedBoxes, shot.r))
      uniq(Boxes).change();
    feedback(new GoodResult);
  }
  
  void no {
    if (addOrMoveToStart(uniq(Boxes).discouragedBoxes, shot.r))
      uniq(Boxes).change();
    feedback(new WrongResult);
  }
  
  void startOver {
    uniq(Boxes).encouragedBoxes.clear();
    uniq(Boxes).discouragedBoxes.clear();
    uniq(Boxes).change();
    finder.reset();
  }
  
  void feedback(final Evaluation ev) {
    finder.reset();
    thread "Feedback" {
      cset(ev, +shot);
      //infoMessage("Thank you, " + (ev instanceof WrongResult ? "negative" : "positive") + " feedback recorded. (id=" + ev.id + ")");
      mainConcepts.save();
    }
  }
  
  void setRecognizing(bool b) {
    recognizing = b;
    frameIcon(frame, b ? #1006287 : #1006286);
    setFrameTitle(frame, (b ? "[Watching] " : " [Paused] ")
      + title);
  }
}

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: 583 / 1004
Referenced in: [show references]