import imagehash
from PIL import Image
from tqdm import tqdm
import numpy as np
from collections import Counter
import os
query_images = np.load(os.path.join('src','model_queries.npy'))
fp = open(os.path.join('src', 'user_query_indices.txt'), 'r')
user_query_indices = {}
for ii, l in enumerate(fp.readlines()):
items = l.split(',')
user_query_indices[ii] = np.asarray([int(i) for i in items])
all_hashes = []
for im in tqdm(query_images):
img = Image.fromarray(im.astype(np.uint8))
h = imagehash.phash(img)
all_hashes.append(str(h))
all_hashes = np.asarray(all_hashes)
suspicious = {}
for imh, cnt in Counter(all_hashes).most_common():
if cnt == 1:
break
image_indices = np.where(all_hashes == imh)[0]
for uid in user_query_indices:
if len(np.intersect1d(image_indices, user_query_indices[uid])):
if uid not in suspicious:
suspicious[uid] = [imh]
else:
suspicious[uid].append(imh)
attackers = sorted(list(suspicious.keys()), key=lambda k: len(suspicious[k]), reverse=True)
attackers = attackers[:20]
bad_users_string = f','.join([str(i) for i in np.sort(attackers)])
flag = f'ictf{{{bad_users_string}}}'
fp = open('flag_sol.txt', 'w')
fp.write(flag)
fp.close()