def write_sort_feature(self,img_features, label_center, centers): for j, center in zip(range(len(centers)), centers): center_dict = {} for i, img_feature in zip(label_center, img_features): if j == i: feature = str(img_feature).split(",") feature_img = feature[2:] feature_img = list(map(float,feature_img)) dis = pdis(center, feature_img) center_dict[img_feature] = dis a = sorted(center_dict.items(), key=lambda x: x[1], reverse=True) save_file = os.path.join(self.save_sort_feature_path,str(j)+".txt") with open(save_file, 'w') as f: for key in a: f.write(key[0] + "," + str(float(key[1]))) f.write("\n")
def write_sort_feature(self, all_features, label_centers, centers,goods_upcs,img_file_names): for j, center in zip(range(len(centers)), centers): center_dict = {} for i, img_feature,good_upc,img_file_name in zip(label_centers, all_features,goods_upcs,img_file_names): if j == i: dis = pdis(center, img_feature) file_feature = str(good_upc)+","+str(img_file_name)+"," for feat in img_feature: file_feature = file_feature+","+str(float(feat)) center_dict[file_feature] = dis a = sorted(center_dict.items(), key=lambda x: x[1], reverse=True) save_file = os.path.join(self.save_sort_feature_path, str(j) + ".txt") with open(save_file, 'w') as f: for key in a: f.write(key[0] + "," + str(float(key[1]))) f.write("\n")
def add_good_img(self, request): try: img_local_file = request.POST.get('img_local_file') goods_shelfgoods_id = request.POST.get('goods_shelfgoods_id') good_upc = request.POST.get('good_upc') trace_id = request.POST.get('trace_id') log.info( "trace_id = {%s},img_local_file={%s},goods_shelfgoods_id={%s}" % (str(trace_id), str(img_local_file), str(goods_shelfgoods_id))) if os.path.isfile(img_local_file) == False: log.error( "trace_id = {%s},img_local_file is not exsit,img_local_file={%s},goods_shelfgoods_id={%s}" % (str(trace_id), str(img_local_file), str(goods_shelfgoods_id))) return HttpResponse(str(result_failed())) file_features = feature.get_feature_by_net(img_local_file) featArr = file_features[0][0] (w, h) = featArr.shape featArr.resize(1, w * h) featArr.resize(h, w) print(featArr.shape) f1s = [] for f1 in featArr: f1s.append(float(np.sum(f1))) cluter_label = clf.predict([f1s])[0] to_cluter_dis = pdis(f1s, clf.cluster_centers_[cluter_label])[0] # filename = os.path.basename(os.path.realpath(img_local_file)) # online.save_new_goods_feature(cluter_label, to_cluter_dis, good_upc, f1s, filename) online.save_new_goods_feature(cluter_label, to_cluter_dis, good_upc, f1s, goods_shelfgoods_id) log.info( "trace_id={%s},img_local_file={%s},add_good_img sucess,cluter_label={%s}" % (str(trace_id), img_local_file, str(cluter_label))) data = '' return HttpResponse(str(result_success(data))) except: log.trace()
def euc_dist(X, Y=None, Y_norm_squared=None, squared=False): return pdis(X, Y)