-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClustering.py
78 lines (61 loc) · 2.34 KB
/
Clustering.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Clustering:
def __init__(self,images,RGBmode=False,threshold=4):
self.imgCount=len(images.index)
self.images=images['path'].to_list()
if RGBmode:
self.hashes=images['rgbHash'].to_list()
self.hashes=list(map(int, self.hashes))
else:
self.hashes=images['gsHash'].to_list()
self.hashes=list(map(int, self.hashes))
self.unionFind=[i for i in range(self.imgCount)]
self.ranks=[0 for _ in range(self.imgCount)]
self.threshold=threshold
def getClusters(self):
self.__cluster()
clusters=dict()
for i in range(self.imgCount):
parent=self.__find(i)
if clusters.get(parent) is None:
clusters[parent]=[self.images[i]]
else:
clusters[parent].append(self.images[i])
return clusters
def __find(self,x):
if self.unionFind[x]==x:
return x
else:
self.unionFind[x]=self.__find(self.unionFind[x])
return self.unionFind[x]
def __union(self,x,y):
if self.__find(x)==self.__find(y):
return
if self.ranks[x]==self.ranks[y]:
self.unionFind[y]=x
self.ranks[x]+=1
elif self.ranks[x]>self.ranks[y]:
self.unionFind[y]=x
else:
self.unionFind[x]=y
return
def __hammingDistance(self,hash1:int,hash2:int):
#assert len(bin(hash1))==len(bin(hash2)),"Hash lengths not equal"
return (hash1^hash2).bit_count()
def __cluster(self):
for i in range(self.imgCount):
self.__nearestNeighbour(i)
def __nearestNeighbour(self,idx):
dists=[(self.__hammingDistance(self.hashes[i],self.hashes[idx]),i) for i in range(self.imgCount)]
for dist in dists:
if dist[0]<self.threshold:
self.__union(dist[1],idx)
if __name__=="__main__":
import json
import pandas as pd
from PIL import Image
from ImageCollector import ImageCollector
images=ImageCollector.getImages(r"D:\Downloads",True)
clusterGenerator=Clustering(images)
clusters=clusterGenerator.getClusters()
clusters={k: v for k,v in clusters.items() if len(v)>1}
#json.dump(clusters,open("mega_dump.json",'w'))