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

88
LINES

< > BotCompany Repo | #1033298 // Gazelle V (old)

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Transpiled version (13709L) is out of date.

!7

cm RecognizeScreen > DynPrintLogAndEnabled {
  switchable int x1;
  switchable int y1;
  switchable int w = screenWidth();
  switchable int h = screenHeight();
  switchable double maxFPS = 10;
  switchable int previewWidth = 200;
  switchable double statsEvery = 5;

  PersistableThrowable lastProcessingError;
  transient WithTimestamp<OKOrError<BufferedImage>> lastScreen;
  transient new Average screenShotTime;
  transient new Average iiTime;
  transient Timestamp loopStarted;
  transient new RestartableCountdown screenShotCountdown;
  transient BWIntegralImage ii;
  transient ImageSurface imageSurface;
  transient Throwable currentProcessingError;
  transient BufferedImage previewImage;

  Rect area() { ret rect(x1, y1, w, h); }
  
  start-thread {
    dm_registerAs_direct gazelleV();
    dm_watchField enabled(r { if (enabled) dm_reload(); });
    
    print("Screen bounds: " + allScreenBounds());

    dm_doEvery(min(statsEvery, 10.0), statsEvery, r { if (enabled) printStats(); });

    loopStarted = tsNow();
    shoot();
  }

  void shoot enter {
    if (!enabled) ret;
    
    long targetTime = sysNow()+iround(1000/maxFPS);
    long time = nanoTime();

    try {
      lastScreen = withTimestamp(okOrError(-> screenshot(area())));
      screenShotTime.add(nanosToSeconds(nanoTime()-time));
      if (lastScreen->isOK()) {
        time = nanoTime();
        ii = BWIntegralImage(lastScreen!!);
        //ii = BWIntegralImage_luminosity(lastScreen!!);
        iiTime.add(nanosToSeconds(nanoTime()-time));
  
        previewImage = scaledBufferedImageFromBWIntegralImage(ii, previewWidth);
        imageSurface?.setImage(previewImage);
      }

      currentProcessingError = null;
    } catch print e {
      setField(lastProcessingError := currentProcessingError = e);
      sleepSeconds(60); // errors in processing are no good
    }
    
    screenShotCountdown.setTargetTime(targetTime, r shoot);
  }

  void printStats() { print(stats()); }

  S stats() {
    var screen = lastScreen?!;
    ret formatColonProperties_noNulls(
      "Monitored area", area(),
      "Error", screen?.getError(),
      "Last screenshot taken", lastScreen?.timeStamp(),
      "Average time to take a screenshot", screenShotTime,
      "Average time to make integral image", iiTime,
      "Capacity left in first core", percentRatioStrOneDigit(freeInCore()),
      "Total CPU used", percentRatioStrOneDigit((1-freeInCore())/numberOfCores()),
    );
  }

  double freeInCore() {
    ret ratio(toSeconds(screenShotCountdown.totalSleepTime),
      elapsedSeconds(loopStarted));
  }

  visual hsplit(super, jscroll_center(imageSurface = doubleBufferedImageSurface_noAlpha()));
  
  BufferedImage lastScreenImage() { ret getVar(getVar(lastScreen)); }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033298
Snippet name: Gazelle V (old)
Eternal ID of this version: #1033298/32
Text MD5: e26972e97846a45a0e504c89ff7ad75e
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-27 16:10:34
Source code size: 2842 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 201 / 730
Version history: 31 change(s)
Referenced in: [show references]