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

88
LINES

< > BotCompany Repo | #1005684 // class ImageFinder

JavaX fragment (include)

1  
// Note: requires e.g. #1005530 - PIF Classes (Clip, Found, FullySearched, Screenshot)
2  
3  
sclass ImageFinder {
4  
  L<Screenshot> screenshots = synchroList();
5  
  L<Clip> clips = synchroList();
6  
7  
  float similarity = 0.999f;
8  
  int maxCopiesPerClip = 100;
9  
  Int maxTotal; // max total clips found per screenshot
10  
  //new LinkedBlockingDeque queue; 
11  
  bool quickSearch_clipBufferedImage = true;
12  
  bool fullSearch = true;
13  
  
14  
  *() {}
15  
  *(L<Screenshot> *screenshots, L<Clip> *clips) {}
16  
  *(Screenshot screenshot, L<Clip> clips) { this(ll(screenshot), clips); }
17  
18  
  void run() {
19  
    for (Screenshot s : cloneList(screenshots))
20  
      searchScreenshot(s);
21  
  }
22  
23  
  void searchScreenshot(Screenshot screenshot) {
24  
    print("Searching screenshot " + screenshot.id);
25  
    new Var<BWImage> screen;
26  
    
27  
    // Order clips by last found
28  
    /*new LinkedHashSet<Clip> clips;
29  
    for (Found found : reversed(list(Found)))
30  
      addIfNotNull(clips, found.clip!);
31  
    addAll(clips, reversed(list(Clip)));*/
32  
    
33  
    quickSearch(screenshot, screen);
34  
    if (fullSearch && !foundEnough(screenshot))
35  
      it: for (Clip clip : clips) {
36  
        if (findConcept(FullySearched, +screenshot, +clip) != null) continue;
37  
        print("  Searching " + clip.description);
38  
        if (!screenshot.hasImage()) continue;
39  
        if (!screen.has())
40  
          screen.set(new BWImage(screenshot.loadImage()));
41  
        bwRawImageSearch_verbose = true;
42  
        bwRawImageSearch_maxEntries = maxCopiesPerClip;
43  
        for (FoundImg fi : bwRawImageSearch(screen.v, clip.img, similarity)) {
44  
          uniq(Found, +screenshot, +clip, +fi);
45  
          if (foundEnough(screenshot)) break it;
46  
        }
47  
        uniq(FullySearched, +screenshot, +clip);
48  
      }
49  
    print("Done " + (fullSearch ? "" : "quick-") + "searching screenshot");
50  
  }
51  
  
52  
  void quickSearch(Screenshot screenshot, Var<BWImage> screen) {
53  
    new HashMap<Clip, Rect> lastPosition;
54  
    for (Found found : reversed(list(Found)))
55  
      if (!lastPosition.containsKey(found.clip!))
56  
        lastPosition.put(found.clip!, found.fi.r);
57  
    for (Clip clip : keys(lastPosition)) pcall {
58  
      if (!clips.contains(clip)) continue;
59  
      if (findConcept(FullySearched, +screenshot, +clip) != null) continue;
60  
      Rect pos = lastPosition.get(clip);
61  
      print("  Quick-Searching " + clip.description + " at " + struct(pos));
62  
      BWImage pat = clip.img;
63  
      int wp = pat.getWidth(), hp = pat.getHeight();
64  
      float maxError = (1f-similarity)*wp*hp;
65  
      float diff;
66  
      if (quickSearch_clipBufferedImage) {
67  
        // TODO: cache image
68  
        BWImage big = new BWImage(clipBufferedImage(screenshot.loadImage(), new Rectangle(pos.x, pos.y, wp, hp)));
69  
        diff = bwImageSectionsSimilarity(big, pat, 0, 0, maxError);
70  
      } else {
71  
        if (!screen.has())
72  
          screen.set(new BWImage(screenshot.loadImage()));
73  
        diff = bwImageSectionsSimilarity(screen.v, pat, pos.x, pos.y, maxError);
74  
      }
75  
      if (diff <= maxError) {
76  
        float sim = 1-diff/(wp*hp);
77  
        FoundImg fi = new FoundImg(pos, sim);
78  
        print("  Found!");
79  
        uniq(Found, +screenshot, +clip, +fi);
80  
        if (foundEnough(screenshot)) break;
81  
      }
82  
    }
83  
  }
84  
  
85  
  bool foundEnough(Screenshot screenshot) {
86  
    ret maxTotal != null && l(findBackRefs(screenshot, Found)) >= maxTotal;
87  
  }
88  
}

Author comment

Began life as a copy of #1005522

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005684
Snippet name: class ImageFinder
Eternal ID of this version: #1005684/1
Text MD5: 837230dd414da812dae9cbd0bf5e2aa4
Author: stefan
Category: javax / ocr
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-05 22:44:11
Source code size: 3383 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 514 / 1138
Referenced in: [show references]