Spaces:
Running
Running
Download source/studio/get_real.py from Ethscriptions/eths: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/Ethscriptions/eths/resolve/main/source/studio/get_real.py
- Command line
-
hf download hf://spaces/Ethscriptions/eths/source/studio/get_real.py
-
curl -L -o get_real.py https://huggingface.co/spaces/Ethscriptions/eths/resolve/main/source/studio/get_real.py
2.8 kB
| """Download the chosen real photos, pick the best 3:4 crop window with CLIP, save credits.""" | |
| import io, json, re, sys, urllib.parse, urllib.request | |
| import numpy as np | |
| from PIL import Image, ImageOps | |
| sys.path.insert(0, '/workspaces/studio') | |
| import clip_score as C | |
| ITEMS = {i['id']: i for i in json.load(open('/workspaces/studio/items2.json'))} | |
| PICKS = { # id -> title of the chosen file (from ranked_<id>.json) | |
| '10': 'UK Green Traffic Light Signal - geograph.org.uk - 52642', | |
| '11': 'SHANGHAI MITSUBISHI ELEVATOR ESCALATOR', | |
| '12': 'JVC RM-C1508 TV remote control unit', | |
| '13': 'Thermos flask with hot water.jpg', | |
| '14': 'People using nail cutters 02.jpg', | |
| '15': 'Baozi, Au Petit Grain de Sel, Paris 001.jpg', | |
| '16': 'Barber cutting hair with comb', | |
| '17': 'Yawning woman.jpg', | |
| '18': 'Trash can, Oak & Dante Streets', | |
| } | |
| UA = {'User-Agent': 'LiveWordsStudio/1.0 (https://huggingface.co/spaces/Ethscriptions/eths; educational)'} | |
| credits = [] | |
| for id_, title in PICKS.items(): | |
| it = ITEMS[id_] | |
| ranked = json.load(open(f'/tmp/work/real/ranked_{id_}.json')) | |
| c = next(r for r in ranked if r['title'].startswith(title)) | |
| name = c['landing'].split('File:')[-1] | |
| url = 'https://commons.wikimedia.org/wiki/Special:FilePath/' + name + '?width=2000' | |
| img = Image.open(io.BytesIO(urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=120).read())) | |
| img = ImageOps.exif_transpose(img).convert('RGB') | |
| w, h = img.size | |
| cw = min(w, int(h * 3 / 4)); chh = int(cw * 4 / 3) | |
| pos = C.text_emb([it['clip']]) | |
| best = None | |
| steps = 7 | |
| for k in range(steps): | |
| fx = k / (steps - 1) | |
| x0 = int((w - cw) * fx) if w > cw else 0 | |
| y0 = int((h - chh) * fx) if h > chh else 0 | |
| if w > cw and h > chh: | |
| y0 = (h - chh) // 2 | |
| crop = img.crop((x0, y0, x0 + cw, y0 + chh)) | |
| s = float(pos[0] @ C.image_emb(crop.resize((300, 400))) * 100) | |
| s -= 0.6 * abs(fx - 0.5) # mild preference for the centre | |
| if best is None or s > best[0]: | |
| best = (s, crop, fx) | |
| crop = best[1] | |
| if crop.width > 1600: | |
| crop = crop.resize((1500, 2000), Image.LANCZOS) | |
| crop.save(f'/tmp/work/real/{id_}_pick.jpg', quality=95) | |
| cr = dict(id=id_, zh=it['zh'], en=it['en'], file=name.replace('_', ' '), page=c['landing'], author=re.sub(r'\s+', ' ', c['creator'] or 'Unknown'), | |
| license=c['license'], license_url=c.get('license_url'), size=f'{w}x{h}', crop_pos=round(best[2], 2), clip=round(best[0], 1)) | |
| credits.append(cr) | |
| print(id_, it['en'], cr['license'], cr['author'][:40], img.size, '-> crop', crop.size, 'pos', cr['crop_pos'], 'clip', cr['clip'], flush=True) | |
| json.dump(credits, open('/workspaces/studio/credits2.json', 'w'), ensure_ascii=False, indent=1) | |