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).

1  
!7
2  
3  
static float bw_threshold = 0.8f;
4  
static double sleepAfterCalc = 0.2;
5  
static double gcFrequency = 10; // Do a GC every 10 seconds (if calculating)
6  
7  
!include #1006246 // FindURLBoxApp
8  
9  
static volatile O sar, simpleRecognizer;
10  
static FindURLBoxApp app;
11  
static JLabel lblRecognition2;
12  
//static ImageSurface isRecognition;
13  
static SingleComponentPanel scpRecog, scpBottom;
14  
static JButton btnPause, btnWdist;
15  
sbool firstRecognition = true;
16  
static RegTimer gcTimer = new RegTimer(toMS(gcFrequency));
17  
18  
concept Recognition {
19  
  new Ref<BoxShot> shot;
20  
  S text;
21  
}
22  
23  
p {
24  
  conceptsAndBot(60000);
25  
  substance();
26  
  
27  
  awt {
28  
    // Load URL box finder
29  
  
30  
    app = new FindURLBoxApp {
31  
      void calc() {
32  
        super.calc();
33  
        if (!recognizing) ret;
34  
        final BWImage img = getBWImage();
35  
        if (img != null && simpleRecognizer != null) {
36  
          S text = "?";
37  
          final long time = sysNow();
38  
          pcall {
39  
            text = (S) call(simpleRecognizer, "recognize", quickExport(img, simpleRecognizer));
40  
          }
41  
          final S _text = text;
42  
          cset(uniq(Recognition, +shot), +text);
43  
          awt {
44  
            //lblRecognition.setText("[Raw: " + _text + "]");
45  
            S sText = _text;
46  
            pcall {
47  
              sText = trim(ocr_withoutSymbols(_text));
48  
            }
49  
            final S _sText = sText;
50  
            lblRecognition2.setText(sText);
51  
            initOutput();
52  
            setSectionTitle(lblRecognition2, "Recognized URL (" + (sysNow()-time) + " ms)");
53  
          }
54  
        }
55  
        if (gcTimer.hit())
56  
          timedGC();
57  
        sleepSeconds(sleepAfterCalc);
58  
      }
59  
    };
60  
    
61  
    app.title = "Watch Me Surf";
62  
    app.init();
63  
  
64  
    frameInnerHeight(app.frame, iround(app.frameHeight*2.2));
65  
    moveToBottomRightCorner(app.frame);
66  
    hideConsole();
67  
    
68  
    //lblRecognition = jlabel();
69  
    lblRecognition2 = jlabel();
70  
    scpRecog = new SingleComponentPanel(jImageWithTextToTheRight(#1006293, "Loading recognizer"));
71  
    addToWindow(app.frame,
72  
      centerAndSouth(jSection("Recognized URL", scpRecog),
73  
      centerAndEast(scpBottom = new SingleComponentPanel,
74  
        jRightAlignedLine(
75  
          jbutton("Teach...", "teach"),
76  
          btnWdist = jbutton("Where Did I Surf Today?", "wdist")))));
77  
    btnPause = jbutton("Pause watching", f pause);
78  
    scpBottom.setComponent(jline(jMemoryView(), btnPause));
79  
    
80  
    installTimer(scpBottom, 60000, r {
81  
      if (!app.recognizing)
82  
        timedGCInNewThread();
83  
    });
84  
  }
85  
  
86  
  // Load Show & Recognize
87  
  
88  
  sar = hotwireDependent("#1006187");
89  
  //set(sar, fullSearch := true);
90  
  set(sar, closeAfterTeach := true);
91  
  call(sar, "prepare");
92  
  simpleRecognizer = get(sar, "sr");
93  
  print("SimpleRecognizer loaded");
94  
  awt { initOutput(); }
95  
}
96  
97  
static BWImage getBWImage() {
98  
  RGBImage rgb = app.finder.bestImage();
99  
  if (rgb == null) null;
100  
  ret preprocess(new BWImage(rgb));
101  
}
102  
103  
svoid teach {
104  
  print("teach");
105  
  BWImage img = getBWImage();
106  
  if (img == null) ret;
107  
  deleteAll(GrabbedImage);
108  
  cnew(GrabbedImage, image := img, md5 := md5OfBWImage(img));
109  
  O showAndRecognize = nuObject(_getClass(sar, "main$ShowAndRecognize"), quickExport(mainConcepts, sar));
110  
  callOpt(showAndRecognize, "teachRow", 0);
111  
}
112  
113  
static BWImage preprocess(BWImage img) {
114  
  ret bwImageTo2Bit(img, bw_threshold);
115  
}
116  
117  
svoid initOutput {
118  
  if (firstRecognition && scpRecog != null) {
119  
    firstRecognition = false;
120  
    scpRecog.setComponent(
121  
      centerAndEast(lblRecognition2,
122  
        jbutton("Copy URL", r {
123  
          copyTextToClipboard(lblRecognition2.getText());
124  
          infoMessage("URL copied to clipboard.");
125  
        })));
126  
    frameInnerHeight(app.frame, iround(app.frameHeight*1.5));
127  
    moveToBottomRightCorner(app.frame);
128  
  }
129  
  /*if (isRecognition == null)
130  
    scpRecog.setComponent(jscroll(isRecognition = new ImageSurface));
131  
  isRecognition.setImage(img.getBufferedImage());*/
132  
}
133  
134  
svoid pause {
135  
  if (app.recognizing) {
136  
    app.setRecognizing(false);
137  
    btnPause.setText("Resume watching");
138  
  } else {
139  
    app.setRecognizing(true);
140  
    btnPause.setText("Pause watching");
141  
  }
142  
}
143  
144  
svoid wdist {
145  
  btnWdist.setEnabled(false);
146  
  btnWdist.setText(btnWdist.getText() + " ...");
147  
  runInNewThreadWithNotify(#1006299, r {
148  
    awt {
149  
      btnWdist.setEnabled(true);
150  
      btnWdist.setText(dropSuffix(" ...", btnWdist.getText()));
151  
    }
152  
  });
153  
}

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