runic-reader-2 / code /doc_filter.py
kesha-humonen's picture
Add files using upload-large-folder tool
ed46c5d verified
Raw History Blame Contribute Delete
1.1 kB
"""Flag archival cards and document scans among photographs.
Archive cards of the runic archives are mostly blank cream-coloured paper with small pasted
photographs. The feature is the share of warm cream pixels (bright, red and green clearly above
blue) in a downscaled image; neutral white or grey backgrounds of object photographs do not count.
"""
import json
import sys
import numpy as np
from PIL import Image
def cream_share(path):
im = Image.open(path).convert("RGB")
im.draft("RGB", (256, 256))
im.thumbnail((256, 256))
a = np.asarray(im).astype(np.float32)
r, g, b = a[..., 0], a[..., 1], a[..., 2]
cream = (r > 195) & (g > 185) & (r - b > 14) & (r - b < 60) & (np.abs(r - g) < 22)
return float(cream.mean())
if __name__ == "__main__":
out = {}
for split in sys.argv[2:]:
for l in open(split):
rec = json.loads(l)
if rec["image"] not in out:
out[rec["image"]] = cream_share(f"{sys.argv[1]}/{rec['image']}")
json.dump(out, open(f"{sys.argv[1]}/data/cream_share.json", "w"))
print(len(out))