def images_to_vectors(self, inpath, outjson_path, modelpath): results = dict() with tf.Graph().as_default(): with tf.Session() as sess: src.facenet.load_model(modelpath) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") image_paths = self.get_image_paths(inpath) for image_path in image_paths: # 获取图片中的人脸数 img = misc.imread(os.path.expanduser(image_path), mode='RGB') images = self.image_array_align_data(img,image_path) #判断是否检测出人脸 检测不出 就跳出此循环 if images.shape[0] == 1 : continue feed_dict = {images_placeholder: images, phase_train_placeholder: False} emb_array = sess.run(embeddings, feed_dict=feed_dict) filename_base, file_extension = os.path.splitext(image_path) for j in range(0, len(emb_array)): results[filename_base + "_" + str(j)] = emb_array[j].tolist() face_mysql_instant = face_mysql.face_mysql() face_mysql_instant.insert_facejson(filename_base + "_" + str(j), ",".join(str(li) for li in emb_array[j].tolist())) # All done, save for later! json.dump(results, open(outjson_path, "w"))
def face_insert(): # 分别获取post请求中的uid 和ugroup作为图片信息 uid = request.form['uid'] ugroup = request.form['ugroup'] upload_files = request.files['imagefile'] # 从post请求图片保存到本地路径中 file = upload_files if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) print(image_path) # opencv读取图片,开始进行人脸识别 img = misc.imread(os.path.expanduser(image_path), mode='RGB') # 设置默认插入时 detect_multiple_faces =Flase只检测图中的一张人脸,True则检测人脸中的多张 # 一般入库时只检测一张人脸,查询时检测多张人脸 images = image_array_align_data(img, image_path, pnet, rnet, onet, detect_multiple_faces=False) feed_dict = { images_placeholder: images, phase_train_placeholder: False } # emb_array保存的是经过facenet转换的128维的向量 # emb_array = sess.run(embeddings, feed_dict=feed_dict) try: # emb_array保存的是经过facenet转换的128维的向量 emb_array = sess.run(embeddings, feed_dict=feed_dict) except: return render_template('face_insert.html', request_one='error') filename_base, file_extension = os.path.splitext(image_path) id_list = [] # 存入数据库 for j in range(0, len(emb_array)): face_mysql_instant = face_mysql.face_mysql() last_id = face_mysql_instant.insert_facejson( filename_base + "_" + str(j), ",".join(str(li) for li in emb_array[j].tolist()), uid, ugroup) id_list.append(str(last_id)) # 设置返回类型 request_result = {} request_result['id'] = ",".join(id_list) if len(id_list) > 0: request_result['state'] = 'sucess' return render_template('face_insert.html', request_one=request_result['state'])
def get_socres(self, A, ugroup): # 设置每次处理的最大数据库记录数 #如果数据库中记录太多时可分批进行处理 maxlen = 128 fmysql = face_mysql.face_mysql() results = np.array(fmysql.findall_facejson(ugroup)) #如果没有数据库找到时 直接返回空的list if results.shape[0] == 0: return [], [], [] pic_scores_all = [] #获取数据库中的入库时的图片名称 pic_names在数据库中存的是数组列索引4这个位置 pic_names = results[:, 4] #获取入库时图片对象的uid pic_uid在数据库中存的是数组列索引2这个位置 pic_uid = results[:, 2] pic_floor = results[:, 7] for i in range(0, len(results), maxlen): pic_vectors = results[i:i + maxlen, 3] # 效率待优化,现在是每行处理 pic_vectors = [[float(j) for j in i.split(',')] for i in pic_vectors] pic_socores = self.EuclideanDistances( A, np.array(pic_vectors)) #比较两个128向量的欧氏距离 pic_socores_list = np.array(pic_socores).tolist() pic_scores_all.extend(pic_socores_list) pic_scores_all = np.array(pic_scores_all).transpose() # 获取距离最近的值 # np.argsort() 返回排序后的索引 pic_min_scores = np.amin(pic_scores_all, axis=1) pic_min_names = [] pic_min_uid = [] pic_min_floor = [] for i in range(0, len(pic_min_scores)): # 获取最小值的index index = np.where(pic_scores_all[i] == pic_min_scores[i]) # print(int(index[0])) # 有多个符合条件的只取第一个 pic_min_names.append(pic_names[index[0][0]]) pic_min_uid.append(pic_uid[index[0][0]]) pic_min_floor.append(pic_floor[index[0][0]]) # print(pic_min_names) return pic_min_scores.tolist( ), pic_min_names, pic_min_uid, pic_min_floor
def images_to_vectors(self, inpath, outjson_path, modelpath): results = dict() with tf.Graph().as_default(): with tf.Session() as sess: src.facenet.load_model(modelpath) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") image_paths = self.get_image_paths(inpath) for image_path in image_paths: # 获取图片中的人脸数 img = misc.imread(os.path.expanduser(image_path), mode='RGB') images = self.image_array_align_data(img, image_path) #判断是否检测出人脸 检测不出 就跳出此循环 if images.shape[0] == 1: continue feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array = sess.run(embeddings, feed_dict=feed_dict) filename_base, file_extension = os.path.splitext( image_path) for j in range(0, len(emb_array)): results[filename_base + "_" + str(j)] = emb_array[j].tolist() face_mysql_instant = face_mysql.face_mysql() face_mysql_instant.insert_facejson( filename_base + "_" + str(j), ",".join(str(li) for li in emb_array[j].tolist()), 555, 333) # All done, save for later! json.dump(results, open(outjson_path, "w"))
def get_socres(self, A, ugroup): # 设置每次处理的最大数据库记录数 #如果数据库中记录太多时可分批进行处理 maxlen = 128 fmysql = face_mysql.face_mysql() results = np.array(fmysql.findall_facejson(ugroup)) #如果没有数据库找到时 直接返回空的list if results.shape[0] == 0: return [],[],[] pic_scores_all = [] #获取数据库中的入库时的图片名称 pic_names在数据库中存的是数组列索引4这个位置 pic_names = results[:, 4] #获取入库时图片对象的uid pic_uid在数据库中存的是数组列索引2这个位置 pic_uid = results[:, 2] for i in range(0, len(results), maxlen): pic_vectors = results[i:i + maxlen, 3] # 效率待优化,现在是每行处理 pic_vectors = [[float(j) for j in i.split(',')] for i in pic_vectors] pic_socores = self.EuclideanDistances(A, np.array(pic_vectors)) pic_socores_list = np.array(pic_socores).tolist() pic_scores_all.extend(pic_socores_list) pic_scores_all = np.array(pic_scores_all).transpose() # 获取距离最近的值 # np.argsort() 返回排序后的索引 pic_min_scores = np.amin(pic_scores_all, axis=1) pic_min_names = [] pic_min_uid = [] for i in range(0, len(pic_min_scores)): # 获取最小值的index index = np.where(pic_scores_all[i] == pic_min_scores[i]) # print(int(index[0])) # 有多个符合条件的只取第一个 pic_min_names.append(pic_names[index[0][0]]) pic_min_uid.append(pic_uid[index[0][0]]) # print(pic_min_names) return pic_min_scores.tolist(), pic_min_names, pic_min_uid