-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter_gate.py
68 lines (48 loc) · 1.81 KB
/
filter_gate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: UTF-8 -*-
'''
Image encoder. Get visual embeddings of images.
'''
import os
import imagehash
from PIL import Image
import pickle
class FilterGate():
def __init__(self,base_path,hash_size):
self.base_path=base_path
self.hash_size=hash_size
self.best_imgs={}
def phash_sim(self,img1,img2,hash_size=None):
if not hash_size:
hash_size=self.hash_size
img1_hash = imagehash.phash(Image.open(img1), hash_size=hash_size)
img2_hash = imagehash.phash(Image.open(img2), hash_size=hash_size)
return 1 - (img1_hash - img2_hash) / len(img1_hash) ** 2
def filter(self):
self.best_imgs={}
ents = os.listdir(self.base_path)
while len(ents)>0:
ent=ents.pop()
imgs=os.listdir(self.base_path + ent + '/')
n_img=len(imgs)
sim_matrix=[[0]*n_img for i in range(n_img)]
for i in range(n_img):
for j in range(i+1,n_img):
sim=self.phash_sim(self.base_path + ent + '/'+imgs[i], self.base_path + ent + '/'+imgs[j])
sim_matrix[i][j]=sim
sim_matrix[j][i] =sim
# sim_matrix=[sum(i) for i in sim_matrix]
max_index=0
max_sim=sum(sim_matrix[0])
for i in range(1,n_img):
if sum(sim_matrix[i])>max_sim:
max_index=i
max_sim=sum(sim_matrix[i])
self.best_imgs[ent]=self.base_path + ent + '/'+imgs[max_index]
return self.best_imgs
def save_best_imgs(self,output_file,n=1):
with open(output_file, 'wb') as out:
pickle.dump(self.best_imgs, out)
if __name__ == '__main__':
f=FilterGate('./data/wn8/',hash_size=16)
f.filter()
f.save_best_imgs('wn18_best_img.pickle')