"""Find real CC-licensed photos (Openverse: Wikimedia Commons / Flickr) and rank them with CLIP.""" import io, json, os, sys, time, urllib.parse, urllib.request import numpy as np from PIL import Image sys.path.insert(0, '/workspaces/studio') import clip_score as C ITEMS = json.load(open('/workspaces/studio/items2.json')) OUT = '/tmp/work/real' os.makedirs(OUT, exist_ok=True) UA = {'User-Agent': 'LiveWordsStudio/1.0 (educational live photo maker)'} NEG = ['a drawing or illustration', 'a diagram or logo', 'text and a document', 'a product catalogue cutout on white background', 'a black and white historical photo', 'a museum exhibit'] def get(url, timeout=60): return urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=timeout).read() def search(q): res = [] for page in (1, 2): p = urllib.parse.urlencode({'q': q, 'license_type': 'commercial', 'category': 'photograph', 'page_size': 20, 'page': page}) try: d = json.loads(get('https://api.openverse.org/v1/images/?' + p)) except Exception as e: print('search err', q, e); break res += d.get('results', []) time.sleep(1.5) return res report = {} for it in ITEMS: if len(sys.argv) > 1 and it['id'] not in sys.argv[1].split(','): continue seen, cands = set(), [] for q in it['q']: for r in search(q): if r['id'] in seen or not r.get('width') or not r.get('height'): continue seen.add(r['id']) w, h = r['width'], r['height'] cw = min(w, h * 3 / 4) # width of the 3:4 crop if cw < 900: continue cands.append(r) pos = C.text_emb([it['clip'], 'a candid everyday photo of ' + it['clip'][11:]]) neg = C.text_emb(NEG) others = C.text_emb(['a photo of ' + o['en'] for o in ITEMS if o['id'] != it['id']]) scored = [] for r in cands[:45]: try: th = Image.open(io.BytesIO(get(f"https://api.openverse.org/v1/images/{r['id']}/thumb/", 40))) except Exception as e: continue # score the centre 3:4 crop, which is what ends up in the frame tw, tht = th.size cw = min(tw, tht * 3 / 4); chh = cw * 4 / 3 th = th.crop(((tw - cw) / 2, (tht - chh) / 2, (tw + cw) / 2, (tht + chh) / 2)) e = C.image_emb(th) sp = float((pos @ e).max() * 100) sn = float((neg @ e).max() * 100) so = float((others @ e).max() * 100) portrait = r['height'] / r['width'] score = sp - 0.5 * max(0, sn - 20) - 0.5 * max(0, so - sp + 2) + (1.0 if portrait >= 1.2 else 0) scored.append(dict(id=r['id'], title=r['title'], creator=r.get('creator'), license=r['license'], license_version=r.get('license_version'), license_url=r.get('license_url'), landing=r.get('foreign_landing_url'), url=r['url'], w=r['width'], h=r['height'], source=r.get('source'), sp=round(sp, 1), sn=round(sn, 1), so=round(so, 1), score=round(score, 2))) scored.sort(key=lambda x: -x['score']) report[it['id']] = scored[:6] print(it['id'], it['en'], len(cands), 'cands') for s in scored[:4]: print(' ', s['score'], s['sp'], s['sn'], s['so'], s['license'], s['w'], s['h'], s['source'], (s['title'] or '')[:60]) json.dump(report, open(OUT + '/candidates_%s.json' % it['id'], 'w'), ensure_ascii=False, indent=1)