def check_distances(self): for i in range(self.node_idx): distance = dcos(self.G.node[i]['desc'], self.G.node[self.node_idx]['desc']) # distance = abs(self.G.node[i]['desc'] - self.G.node[self.node_idx]['desc']) if distance <= self.threshold: self.G.add_edge(i, self.node_idx, weight=distance)
def find_closest(img, database, min_detection=2.5): imarr1 = np.asarray(img) imarr1 = imarr1[None, ...] #Prediction fvec1 = featuremodel.predict(imarr1)[0, :] #Closest person in DB dmin = 0.0 umin = "" for key, value in database.items(): fvec2 = value dcos_1_2 = dcos(fvec1, fvec2) if umin == "": dmin = dcos_1_2 umin = key elif dcos_1_2 < dmin: dmin = dcos_1_2 umin = key if dmin > min_detection: umin = "" return umin, dmin
def calculate_distance(im1, im2): fvec1 = extract_feature(im1) fvec2 = extract_feature(im2) dcos_1_2 = dcos(fvec1, fvec2) print('----------: {}'.format(dcos_1_2)) return dcos_1_2
def cos_distance(feature_vector1, feature_vector2): return dcos(feature_vector1, feature_vector2)
def get_distance(self, node0, node1): return dcos(self.G.node[node0]['desc'], self.G.node[node1]['desc'])
# convert the image pixels to a numpy array imarr_i = img_to_array(imarr_i) # reshape data for the model imarr_i = imarr_i.reshape( (1, imarr_i.shape[0], imarr_i.shape[1], imarr_i.shape[2])) return imarr_i imarr1 = get_im(str1) imarr2 = get_im(str2) imarr3 = get_im(str3) #imarr3=get_im(str3) #imarr4=get_im(str4) imarr_t = crpim imarr_t = img_to_array(imarr_t) # reshape data for the model imarr_t = imarr_t.reshape( (1, imarr_t.shape[0], imarr_t.shape[1], imarr_t.shape[2])) #imarr2=Image.open(crpim) face_id = {str1: imarr1, str2: imarr2, str3: imarr3} featuremodel = Model(input=facemodel.layers[0].input, output=facemodel.layers[-2].output) from scipy.spatial.distance import cosine as dcos for j in face_id: fvec1 = featuremodel.predict(face_id[j])[0, :] fvec2 = featuremodel.predict(imarr_t)[0, :] dcos_1_2 = dcos(fvec1, fvec2) if (dcos_1_2 < 0.2): print('\n \n \n RECOGNISED {0}'.format(j))