sunsexsurf

image_hash_mean

May 16th, 2020
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. import imagehash as img
  2. MAX_DIST = 256
  3.  
  4. def center(hashes):
  5.     d = MAX_DIST
  6.     cnt = None
  7.     for i in range(len(hashes)-1):
  8.         maxd = 0
  9.         for j in range(i+1,len(hashes)):
  10.             temp = img.hex_to_hash(hashes[i]) - img.hex_to_hash(hashes[j])
  11.             if temp > maxd:
  12.                 maxd = temp
  13.         if maxd < d:
  14.             d = maxd
  15.             cnt = hashes[i]
  16.     return cnt, d
  17.  
  18. f = open(r"E:\coins\coin_hash.txt", "r")
  19. buf = f.read()
  20. f.close()
  21. allhash = eval(buf)
  22. tres = allhash[3]
  23. cluster = {}
  24. for trihash in tres:
  25.     if not cluster.keys():
  26.         cluster[trihash] = {'neib':[trihash], 'dist': MAX_DIST}
  27.     else:
  28.         mind = MAX_DIST
  29.         minkey = None
  30.         for clus in cluster.keys():
  31.             d = img.hex_to_hash(clus)-img.hex_to_hash(trihash)
  32.             if d < mind:
  33.                 minkey = clus
  34.                 mind = d
  35.         if mind > 25:
  36.             cluster[trihash] = {'neib': [trihash], 'dist': MAX_DIST}
  37.         else:
  38.             cluster[minkey]['neib'].append(trihash)
  39.             c, dist = center(cluster[minkey]['neib'])
  40.             if c != minkey:
  41.                 cluster[c]=cluster[minkey]
  42.                 del cluster[minkey]
  43.                 cluster[c]['dist'] = dist
  44. print(cluster)
Advertisement
Add Comment
Please, Sign In to add comment