Download prep/extract_masks.py from myned-ai/facepoints: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/myned-ai/facepoints/resolve/main/prep/extract_masks.py
- Command line
-
hf download hf://myned-ai/facepoints/prep/extract_masks.py
-
curl -L -o extract_masks.py https://huggingface.co/myned-ai/facepoints/resolve/main/prep/extract_masks.py
4.34 kB
| """Person masks for extracted crops: Ava-256's segmentation, cut with each crop's own box.""" | |
| # Usage: python -m facepoints.prep.extract_masks <data root> [--workers 16] [--threads 8] | |
| # [--captures ids.txt] | |
| # For every row of <root>/<capture>/labels.npz: reads cam<c>/<frame>.png from Ava-256's public S3 | |
| # (16TB/<capture>/decoder/segmentation_parts/cam<c>.zip, by HTTP range; one zip open per camera), | |
| # crops it with the row's box (photo px, the same box as the jpg), nearest-resizes it to 512 px and | |
| # writes <root>/<capture>/<frame>_<cam>_m.png: 255 = person (every class but background and chair; | |
| # hair included), 0 = not. Resumable per capture (masks.done holds "<written> <missing>"). The masks | |
| # feed the background swap of data.wild.Wild only: offline augmentation, never a runtime input. | |
| import argparse | |
| import concurrent.futures as cf | |
| import io | |
| import multiprocessing | |
| import os | |
| import zipfile | |
| from collections import defaultdict | |
| import numpy as np | |
| from PIL import Image | |
| from facepoints.prep.http_zip import HttpFile | |
| BASE = "https://fb-baas-f32eacb9-8abb-11eb-b2b8-4857dd089e15.s3.amazonaws.com/ava-256/16TB" | |
| CROP = 512 | |
| BACKGROUND, CHAIR = 0, 3 # segmentation classes that are not the person | |
| def one_capture(root, cap, threads): | |
| """Write the masks of one capture; return (capture, summary).""" | |
| d = os.path.join(root, cap) | |
| done = os.path.join(d, "masks.done") | |
| if os.path.exists(done): | |
| return cap, "skip" | |
| z = np.load(os.path.join(d, "labels.npz")) | |
| by_cam = defaultdict(list) | |
| for f, c, b in zip(z["frame"], z["cam"], z["box"], strict=True): | |
| by_cam[int(c)].append((int(f), b)) | |
| def cam(c, rows): # one thread per camera: each read is a small range request, latency-bound | |
| n = miss = 0 | |
| try: | |
| sz = zipfile.ZipFile(HttpFile(f"{BASE}/{cap}/decoder/segmentation_parts/cam{c}.zip")) | |
| except Exception: # noqa: BLE001 - a camera without masks: its crops just never swap | |
| return 0, len(rows) | |
| for f, b in rows: | |
| out = os.path.join(d, f"{f:06d}_{c}_m.png") | |
| if os.path.exists(out): | |
| n += 1 | |
| continue | |
| try: | |
| seg = Image.open(io.BytesIO(sz.read(f"cam{c}/{f:06d}.png"))) | |
| except KeyError: | |
| miss += 1 | |
| continue | |
| a = np.array( | |
| seg.crop(tuple(int(round(v)) for v in b)).resize((CROP, CROP), Image.NEAREST) | |
| ) | |
| person = (a != BACKGROUND) & (a != CHAIR) | |
| Image.fromarray((person * 255).astype(np.uint8)).save(out, optimize=True) | |
| n += 1 | |
| return n, miss | |
| with cf.ThreadPoolExecutor(threads) as ex: | |
| r = list(ex.map(lambda kv: cam(*kv), by_cam.items())) | |
| n, miss = sum(a for a, _ in r), sum(b for _, b in r) | |
| with open(done, "w") as fh: | |
| fh.write(f"{n} {miss}\n") | |
| return cap, f"{n} masks, {miss} missing" | |
| def main(): | |
| """Write the masks of every extracted capture with a process pool.""" | |
| ap = argparse.ArgumentParser(description=__doc__) | |
| ap.add_argument("root", help="data root written by extract.py") | |
| ap.add_argument("--workers", type=int, default=16, help="capture processes") | |
| ap.add_argument("--threads", type=int, default=8, help="camera threads per capture process") | |
| ap.add_argument("--captures", help="file of capture ids (default: every extracted capture)") | |
| a = ap.parse_args() | |
| caps = sorted( | |
| c for c in os.listdir(a.root) if os.path.exists(os.path.join(a.root, c, "labels.npz")) | |
| ) | |
| if a.captures: | |
| with open(a.captures) as fh: | |
| only = set(fh.read().split()) | |
| caps = [c for c in caps if c in only] | |
| ctx = multiprocessing.get_context("fork") if hasattr(os, "fork") else None | |
| with cf.ProcessPoolExecutor(a.workers, mp_context=ctx) as ex: | |
| futs = {ex.submit(one_capture, a.root, c, a.threads): c for c in caps} | |
| for i, fu in enumerate(cf.as_completed(futs)): | |
| try: | |
| r = fu.result()[1] | |
| except Exception as e: # noqa: BLE001 - a failed capture is redone on the next run | |
| r = f"ERROR {type(e).__name__}: {str(e)[:120]}" | |
| print(f"{i + 1}/{len(caps)} {futs[fu][-6:]} {r}", flush=True) | |
| print("MASKS DONE", flush=True) | |
| if __name__ == "__main__": | |
| main() | |