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

89
LINES

< > BotCompany Repo | #1032475 // FaceDataSet

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (7340L/44K).

sclass FaceDataSet {
  S csvSnippetID = #1032472;
  
  // by imageURL
  new Map<S, Entry> entryMap;
  new L<Entry> entries;
  
  class Entry {
    S imageURL;
    File imageFile;
    new Set<DoubleRect> facePositions;
    int w, h;
    
    *(S *imageURL) {
      imageFile = downloadImage(imageURL);
    }
    
    BufferedImage image() {
      var img = loadImage2(imageFile);
      if (img == null) print(imageFile + " not found");
      else {
        w = img.getWidth();
        h = img.getHeight();
      }
      ret img;
    }
    
    BufferedImage markedImage(int width default w()) {
      var img = scaleToWidth(image(), width);
      int height = img.getHeight();
      for (var face : facePositions) {
        Rect r = scaleRect_floor(face, width, height);
        drawBox(img, r, Color.red, 0.5);
      }
      ret img;
    }
    
    int w() { if (w == 0) image(); ret w; }
    int h() { if (h == 0) image(); ret h; }
  }

  File imagesDir() { ret javaxCachesDir("Google Face Data Set"); }
  
  File downloadImage(S url) {
    File file = newFile(imagesDir(), md5FileName(url) + fileExtension(url));
    if (file.length() == 0) null;
    if (!file.exists()) try {
      loadBinaryPageToFile(url, file);
    } catch e {
      if (containsExceptionOfType FileNotFoundException(e))
        // as a warning for future generations!
        saveBinaryFile(file, new byte[0]);
    }
    ret file.exists() ? file : null;
  }
  
  void load {
    for (LS row : parseExtendedCSV_iterator(loadSnippet(csvSnippetID))) {
      for n to 3: pcall {
        int i = n*5;
        S imageURL = convertHttpToHttps(row.get(i++));
        Entry e = entryMap.get(imageURL);
        if (e == null) {
          e = new Entry(imageURL);
          if (e.imageFile == null) continue with print("Skipping " + e.imageURL);
          entryMap.put(imageURL, e);
          entries.add(e);
        }
        
        e.facePositions.add(doubleRectFromPoints(
          parseDouble(row.get(i)),   // left column  (x1)
          parseDouble(row.get(i+2)), // top row      (y1)
          parseDouble(row.get(i+1)), // right column (x2)
          parseDouble(row.get(i+3)), // bottom row   (y2)
        ));
      }
    }
    
    print("Have " + nEntries(entries));
  }
}
 
/* Row begins: 
URL of image1 (string)
Top-left column of the face bounding box in image1 normalized by width (float)
Bottom-right column of the face bounding box in image1 normalized by width (float)
Top-left row of the face bounding box in image1 normalized by height (float)
Bottom-right row of the face bounding box in image1 normalized by height (float)
*/

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032475
Snippet name: FaceDataSet
Eternal ID of this version: #1032475/21
Text MD5: cc3c073aeafe9e8c034dfe36e83b5208
Transpilation MD5: 5e8e5428e9d0325fb5c581abfaac9acc
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-08 08:18:43
Source code size: 2701 bytes / 89 lines
Pitched / IR pitched: No / No
Views / Downloads: 207 / 384
Version history: 20 change(s)
Referenced in: [show references]