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

153
LINES

< > BotCompany Repo | #1006248 // Computer, watch me surf [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (10909L/76K/257K).

!7

static float bw_threshold = 0.8f;
static double sleepAfterCalc = 0.2;
static double gcFrequency = 10; // Do a GC every 10 seconds (if calculating)

!include #1006246 // FindURLBoxApp

static volatile O sar, simpleRecognizer;
static FindURLBoxApp app;
static JLabel lblRecognition2;
//static ImageSurface isRecognition;
static SingleComponentPanel scpRecog, scpBottom;
static JButton btnPause, btnWdist;
sbool firstRecognition = true;
static RegTimer gcTimer = new RegTimer(toMS(gcFrequency));

concept Recognition {
  new Ref<BoxShot> shot;
  S text;
}

p {
  conceptsAndBot(60000);
  substance();
  
  awt {
    // Load URL box finder
  
    app = new FindURLBoxApp {
      void calc() {
        super.calc();
        if (!recognizing) ret;
        final BWImage img = getBWImage();
        if (img != null && simpleRecognizer != null) {
          S text = "?";
          final long time = sysNow();
          pcall {
            text = (S) call(simpleRecognizer, "recognize", quickExport(img, simpleRecognizer));
          }
          final S _text = text;
          cset(uniq(Recognition, +shot), +text);
          awt {
            //lblRecognition.setText("[Raw: " + _text + "]");
            S sText = _text;
            pcall {
              sText = trim(ocr_withoutSymbols(_text));
            }
            final S _sText = sText;
            lblRecognition2.setText(sText);
            initOutput();
            setSectionTitle(lblRecognition2, "Recognized URL (" + (sysNow()-time) + " ms)");
          }
        }
        if (gcTimer.hit())
          timedGC();
        sleepSeconds(sleepAfterCalc);
      }
    };
    
    app.title = "Watch Me Surf";
    app.init();
  
    frameInnerHeight(app.frame, iround(app.frameHeight*2.2));
    moveToBottomRightCorner(app.frame);
    hideConsole();
    
    //lblRecognition = jlabel();
    lblRecognition2 = jlabel();
    scpRecog = new SingleComponentPanel(jImageWithTextToTheRight(#1006293, "Loading recognizer"));
    addToWindow(app.frame,
      centerAndSouth(jSection("Recognized URL", scpRecog),
      centerAndEast(scpBottom = new SingleComponentPanel,
        jRightAlignedLine(
          jbutton("Teach...", "teach"),
          btnWdist = jbutton("Where Did I Surf Today?", "wdist")))));
    btnPause = jbutton("Pause watching", f pause);
    scpBottom.setComponent(jline(jMemoryView(), btnPause));
    
    installTimer(scpBottom, 60000, r {
      if (!app.recognizing)
        timedGCInNewThread();
    });
  }
  
  // Load Show & Recognize
  
  sar = hotwireDependent("#1006187");
  //set(sar, fullSearch := true);
  set(sar, closeAfterTeach := true);
  call(sar, "prepare");
  simpleRecognizer = get(sar, "sr");
  print("SimpleRecognizer loaded");
  awt { initOutput(); }
}

static BWImage getBWImage() {
  RGBImage rgb = app.finder.bestImage();
  if (rgb == null) null;
  ret preprocess(new BWImage(rgb));
}

svoid teach {
  print("teach");
  BWImage img = getBWImage();
  if (img == null) ret;
  deleteAll(GrabbedImage);
  cnew(GrabbedImage, image := img, md5 := md5OfBWImage(img));
  O showAndRecognize = nuObject(_getClass(sar, "main$ShowAndRecognize"), quickExport(mainConcepts, sar));
  callOpt(showAndRecognize, "teachRow", 0);
}

static BWImage preprocess(BWImage img) {
  ret bwImageTo2Bit(img, bw_threshold);
}

svoid initOutput {
  if (firstRecognition && scpRecog != null) {
    firstRecognition = false;
    scpRecog.setComponent(
      centerAndEast(lblRecognition2,
        jbutton("Copy URL", r {
          copyTextToClipboard(lblRecognition2.getText());
          infoMessage("URL copied to clipboard.");
        })));
    frameInnerHeight(app.frame, iround(app.frameHeight*1.5));
    moveToBottomRightCorner(app.frame);
  }
  /*if (isRecognition == null)
    scpRecog.setComponent(jscroll(isRecognition = new ImageSurface));
  isRecognition.setImage(img.getBufferedImage());*/
}

svoid pause {
  if (app.recognizing) {
    app.setRecognizing(false);
    btnPause.setText("Resume watching");
  } else {
    app.setRecognizing(true);
    btnPause.setText("Pause watching");
  }
}

svoid wdist {
  btnWdist.setEnabled(false);
  btnWdist.setText(btnWdist.getText() + " ...");
  runInNewThreadWithNotify(#1006299, r {
    awt {
      btnWdist.setEnabled(true);
      btnWdist.setText(dropSuffix(" ...", btnWdist.getText()));
    }
  });
}

Author comment

Began life as a copy of #1006247

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: #1006248
Snippet name: Computer, watch me surf [WORKS]
Eternal ID of this version: #1006248/1
Text MD5: 492703447ff4430afe56bdfba23a1c8d
Transpilation MD5: 3f6a127d1f29afe571a297b0157bd884
Author: stefan
Category: javax / ocr
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-28 17:21:45
Source code size: 4477 bytes / 153 lines
Pitched / IR pitched: No / No
Views / Downloads: 833 / 1376
Referenced in: #1006299 - Where Did I Surf Today? [works, part of that other program]