!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 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())); } }); }