1 | sclass FindURLBox1 { |
2 | Color color = Color.white; |
3 | int maxHeight = 50, minWidth = 100; |
4 | int g = 2; // grid size |
5 | double fullScanEvery = 10.0; // if we get stuck on a wrong box |
6 | |
7 | new L<Rect> encouragedBoxes; |
8 | new L<Rect> discouragedBoxes; |
9 | |
10 | RGBImage img; |
11 | L<Rect> clips; |
12 | Rect bestClip; |
13 | RGBImage bestImage; |
14 | bool verbose; |
15 | long recogTime; // includes shooting the screen |
16 | long lastFullScan; |
17 | |
18 | // returns true if smth was found |
19 | bool go() { |
20 | long time = sysNow(); |
21 | try { |
22 | if (lastFullScan >= now()-toMS(fullScanEvery) |
23 | && stillThere()) |
24 | ret true; |
25 | lastFullScan = now(); |
26 | img = shootScreen(); |
27 | ret calc(); |
28 | } finally { |
29 | recogTime = sysNow()-time; |
30 | } |
31 | } |
32 | |
33 | bool calc() { |
34 | clips = [Rect r : areasColored(img, color, g) | r.h <= maxHeight && r.w >= minWidth]; |
35 | if (verbose) |
36 | print(n(clips, "areas") + " found for color " + new RGB(color) + ": " + struct(clips)); |
37 | |
38 | bestClip = bestAccordingTo(clips, func(Rect r) { scoreClip(r) }); |
39 | bestImage = img.clip(bestClip); |
40 | ret bestClip != null; |
41 | } |
42 | |
43 | double scoreClip(Rect r) { |
44 | int i = encouragedBoxes.indexOf(r); |
45 | if (i >= 0) ret 1000-i; |
46 | i = discouragedBoxes.indexOf(r); |
47 | if (i >= 0) ret -1000+i; |
48 | |
49 | double width = min(1, r.w/(double) img.w()*2); |
50 | double top = r.y/(double) img.h(); |
51 | double height = r.h/(double) img.h(); |
52 | ret width-(top+height)*2; // thin, wide, high up |
53 | } |
54 | |
55 | RGBImage bestImage() { |
56 | ret bestImage; |
57 | } |
58 | |
59 | void reset() { |
60 | bestClip = null; |
61 | } |
62 | |
63 | bool stillThere() { |
64 | if (bestClip == null) false; |
65 | Rect r = intersectWithScreen(growRect(bestClip, g)); |
66 | RGBImage img = shootScreen(r); |
67 | L<Rect> clips = areasColored(img, color, g); |
68 | Rect r2 = translateRect(bestClip, -r.x, -r.y); |
69 | //print("r2=" + r2 + ", clips=" + struct(clips)); |
70 | if (clips.contains(r2)) { |
71 | bestImage = img.clip(r2); |
72 | true; |
73 | } else { |
74 | bestClip = null; |
75 | bestImage = null; |
76 | false; |
77 | } |
78 | } |
79 | } |
Began life as a copy of #1006217
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: | #1006231 |
Snippet name: | FindURLBox1 |
Eternal ID of this version: | #1006231/1 |
Text MD5: | 4259779c9f6eb8489fbe6bffadb74a23 |
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:24:07 |
Source code size: | 2076 bytes / 79 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 584 / 1217 |
Referenced in: | [show references] |