Exemplo n.º 1
0
 def get_distances(self, vectors, image_dict):
     results = {}
     result = []
     for i in vectors:
         print("Working on hash" + str(i))
         dist = {}
         projection = []
         for image in image_dict:
             a = np.array(vectors[i], dtype=np.float)
             b = a[1]
             a = a[0]
             p = np.array(image_dict[image], dtype=np.float)
             ap = p - a
             ab = b - a
             # distance = np.array(np.around((ab * ap) / Distance.E2_distance(a, b), decimals=4))
             projection.append(
                 np.array(
                     np.around((a + np.dot(ab, ap) / np.dot(ab, ab) * ab),
                               decimals=3)))
             # projection.append(np.array(np.around()))
         min_proj = np.amin(projection, axis=0)
         image_keys = list(image_dict.keys())
         j = 0
         # print(min_proj)
         for x in projection:
             distance = Distance.E2_distance(min_proj, x)
             ind = image_keys[j]
             dist[ind] = distance
             j = j + 1
         result.append(dist)
         results['h' + str(i)] = result
         result = []
     # print(results)
     return results
Exemplo n.º 2
0
 def get_neighbors(self, labelled_set, labels, imageInstance, k):
     distances = []
     j = 0
     for x in range(len(labelled_set)):
         # dist = Similarity.cos_similarity(imageInstance, labelled_set[x])
         dist = Distance.E2_distance(imageInstance, labelled_set[x])
         distances.append((labelled_set[x], labels[j], dist))
         j += 1
     distances.sort(key=operator.itemgetter(2), reverse=False)
     neighbors = []
     for x in range(k):
         neighbors.append(distances[x][1])
     # print("neighbors:" + str(neighbors))
     return neighbors