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

88
LINES

< > BotCompany Repo | #1005684 // class ImageFinder

JavaX fragment (include)

// Note: requires e.g. #1005530 - PIF Classes (Clip, Found, FullySearched, Screenshot)

sclass ImageFinder {
  L<Screenshot> screenshots = synchroList();
  L<Clip> clips = synchroList();

  float similarity = 0.999f;
  int maxCopiesPerClip = 100;
  Int maxTotal; // max total clips found per screenshot
  //new LinkedBlockingDeque queue; 
  bool quickSearch_clipBufferedImage = true;
  bool fullSearch = true;
  
  *() {}
  *(L<Screenshot> *screenshots, L<Clip> *clips) {}
  *(Screenshot screenshot, L<Clip> clips) { this(ll(screenshot), clips); }

  void run() {
    for (Screenshot s : cloneList(screenshots))
      searchScreenshot(s);
  }

  void searchScreenshot(Screenshot screenshot) {
    print("Searching screenshot " + screenshot.id);
    new Var<BWImage> screen;
    
    // Order clips by last found
    /*new LinkedHashSet<Clip> clips;
    for (Found found : reversed(list(Found)))
      addIfNotNull(clips, found.clip!);
    addAll(clips, reversed(list(Clip)));*/
    
    quickSearch(screenshot, screen);
    if (fullSearch && !foundEnough(screenshot))
      it: for (Clip clip : clips) {
        if (findConcept(FullySearched, +screenshot, +clip) != null) continue;
        print("  Searching " + clip.description);
        if (!screenshot.hasImage()) continue;
        if (!screen.has())
          screen.set(new BWImage(screenshot.loadImage()));
        bwRawImageSearch_verbose = true;
        bwRawImageSearch_maxEntries = maxCopiesPerClip;
        for (FoundImg fi : bwRawImageSearch(screen.v, clip.img, similarity)) {
          uniq(Found, +screenshot, +clip, +fi);
          if (foundEnough(screenshot)) break it;
        }
        uniq(FullySearched, +screenshot, +clip);
      }
    print("Done " + (fullSearch ? "" : "quick-") + "searching screenshot");
  }
  
  void quickSearch(Screenshot screenshot, Var<BWImage> screen) {
    new HashMap<Clip, Rect> lastPosition;
    for (Found found : reversed(list(Found)))
      if (!lastPosition.containsKey(found.clip!))
        lastPosition.put(found.clip!, found.fi.r);
    for (Clip clip : keys(lastPosition)) pcall {
      if (!clips.contains(clip)) continue;
      if (findConcept(FullySearched, +screenshot, +clip) != null) continue;
      Rect pos = lastPosition.get(clip);
      print("  Quick-Searching " + clip.description + " at " + struct(pos));
      BWImage pat = clip.img;
      int wp = pat.getWidth(), hp = pat.getHeight();
      float maxError = (1f-similarity)*wp*hp;
      float diff;
      if (quickSearch_clipBufferedImage) {
        // TODO: cache image
        BWImage big = new BWImage(clipBufferedImage(screenshot.loadImage(), new Rectangle(pos.x, pos.y, wp, hp)));
        diff = bwImageSectionsSimilarity(big, pat, 0, 0, maxError);
      } else {
        if (!screen.has())
          screen.set(new BWImage(screenshot.loadImage()));
        diff = bwImageSectionsSimilarity(screen.v, pat, pos.x, pos.y, maxError);
      }
      if (diff <= maxError) {
        float sim = 1-diff/(wp*hp);
        FoundImg fi = new FoundImg(pos, sim);
        print("  Found!");
        uniq(Found, +screenshot, +clip, +fi);
        if (foundEnough(screenshot)) break;
      }
    }
  }
  
  bool foundEnough(Screenshot screenshot) {
    ret maxTotal != null && l(findBackRefs(screenshot, Found)) >= maxTotal;
  }
}

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: 575 / 1208
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)