face_recognition_api.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # -*- coding:utf-8 -*-
  2. from flask import Flask, jsonify, abort, make_response, request, url_for
  3. from flask_httpauth import HTTPBasicAuth
  4. import json
  5. import os
  6. import ntpath
  7. import argparse
  8. import face_mysql
  9. import tensorflow as tf
  10. import src.facenet
  11. import src.align.detect_face
  12. import numpy as np
  13. from scipy import misc
  14. import matrix_fun
  15. import urllib
  16. app = Flask(__name__)
  17. # 图片最大为16M
  18. app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
  19. auth = HTTPBasicAuth()
  20. #设置最大的相似距离,1.22是facenet基于lfw计算得到的
  21. MAX_DISTINCT=1.22
  22. # 设置上传的图片路径和格式
  23. from werkzeug import secure_filename
  24. #设置post请求中获取的图片保存的路径
  25. UPLOAD_FOLDER = './pic_tmp/'
  26. if not os.path.exists(UPLOAD_FOLDER):
  27. os.makedirs(UPLOAD_FOLDER)
  28. else:
  29. pass
  30. ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
  31. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  32. def allowed_file(filename):
  33. return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  34. with tf.Graph().as_default():
  35. gpu_memory_fraction = 1.0
  36. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
  37. sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
  38. with sess.as_default():
  39. pnet, rnet, onet = src.align.detect_face.create_mtcnn(sess, None)
  40. #训练模型的路径
  41. modelpath = "./models/facenet/20170512-110547"
  42. with tf.Graph().as_default():
  43. sess = tf.Session()
  44. # src.facenet.load_model(modelpath)
  45. # 加载模型
  46. meta_file, ckpt_file = src.facenet.get_model_filenames(modelpath)
  47. saver = tf.train.import_meta_graph(os.path.join(modelpath, meta_file))
  48. saver.restore(sess, os.path.join(modelpath, ckpt_file))
  49. # Get input and output tensors
  50. images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
  51. embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
  52. phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
  53. # 进行人脸识别,加载
  54. print('Creating networks and loading parameters')
  55. #获取post中的图片并执行插入到库 返回数据库中保存的id
  56. @app.route('/face/insert', methods=['POST'])
  57. def face_insert():
  58. #分别获取post请求中的uid 和ugroup作为图片信息
  59. uid = request.form['uid']
  60. ugroup = request.form['ugroup']
  61. upload_files = request.files['imagefile']
  62. #从post请求图片保存到本地路径中
  63. file = upload_files
  64. if file and allowed_file(file.filename):
  65. filename = secure_filename(file.filename)
  66. file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
  67. image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  68. print(image_path)
  69. #opencv读取图片,开始进行人脸识别
  70. img = misc.imread(os.path.expanduser(image_path), mode='RGB')
  71. # 设置默认插入时 detect_multiple_faces =Flase只检测图中的一张人脸,True则检测人脸中的多张
  72. #一般入库时只检测一张人脸,查询时检测多张人脸
  73. images = image_array_align_data(img, image_path, pnet, rnet, onet, detect_multiple_faces=False)
  74. feed_dict = {images_placeholder: images, phase_train_placeholder: False}
  75. #emb_array保存的是经过facenet转换的128维的向量
  76. emb_array = sess.run(embeddings, feed_dict=feed_dict)
  77. filename_base, file_extension = os.path.splitext(image_path)
  78. id_list = []
  79. #存入数据库
  80. for j in range(0, len(emb_array)):
  81. face_mysql_instant = face_mysql.face_mysql()
  82. last_id = face_mysql_instant.insert_facejson(filename_base + "_" + str(j),
  83. ",".join(str(li) for li in emb_array[j].tolist()), uid, ugroup)
  84. id_list.append(str(last_id))
  85. #设置返回类型
  86. request_result = {}
  87. request_result['id'] = ",".join(id_list)
  88. if len(id_list) > 0:
  89. request_result['state'] = 'sucess'
  90. else:
  91. request_result['state'] = 'error'
  92. print(request_result)
  93. return json.dumps(request_result)
  94. @app.route('/face/query', methods=['POST'])
  95. def face_query():
  96. #获取查询条件 在ugroup中查找相似的人脸
  97. ugroup = request.form['ugroup']
  98. upload_files = request.files['imagefile']
  99. #获取post请求的图片到本地
  100. file = upload_files
  101. if file and allowed_file(file.filename):
  102. filename = secure_filename(file.filename)
  103. file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
  104. image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  105. print(image_path)
  106. #读取本地的图片
  107. img = misc.imread(os.path.expanduser(image_path), mode='RGB')
  108. images = image_array_align_data(img, image_path, pnet, rnet, onet)
  109. #判断如果如图没有检测到人脸则直接返回
  110. if len(images.shape) < 4: return json.dumps({'error': "not found face"})
  111. feed_dict = {images_placeholder: images, phase_train_placeholder: False}
  112. emb_array = sess.run(embeddings, feed_dict=feed_dict)
  113. face_query = matrix_fun.matrix()
  114. #分别获取距离该图片中人脸最相近的人脸信息
  115. # pic_min_scores 是数据库中人脸距离(facenet计算人脸相似度根据人脸距离进行的)
  116. # pic_min_names 是当时入库时保存的文件名
  117. # pic_min_uid 是对应的用户id
  118. pic_min_scores, pic_min_names, pic_min_uid = face_query.get_socres(emb_array, ugroup)
  119. #如果提交的query没有group 则返回
  120. if len(pic_min_scores) == 0: return json.dumps({'error': "not found user group"})
  121. #设置返回结果
  122. result = []
  123. for i in range(0, len(pic_min_scores)):
  124. if pic_min_scores[i]<MAX_DISTINCT:
  125. rdict = {'uid': pic_min_uid[i],
  126. 'distance': pic_min_scores[i],
  127. 'pic_name': pic_min_names[i] }
  128. result.append(rdict)
  129. print(result)
  130. if len(result)==0 :
  131. return json.dumps({"state":"success, but not match face"})
  132. else:
  133. return json.dumps(result)
  134. #检测图片中的人脸 image_arr是opencv读取图片后的3维矩阵 返回图片中人脸的位置信息
  135. def image_array_align_data(image_arr, image_path, pnet, rnet, onet, image_size=160, margin=32, gpu_memory_fraction=1.0,
  136. detect_multiple_faces=True):
  137. minsize = 20 # minimum size of face
  138. threshold = [0.6, 0.7, 0.7] # three steps's threshold
  139. factor = 0.709 # scale factor
  140. img = image_arr
  141. bounding_boxes, _ = src.align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
  142. nrof_faces = bounding_boxes.shape[0]
  143. nrof_successfully_aligned = 0
  144. if nrof_faces > 0:
  145. det = bounding_boxes[:, 0:4]
  146. det_arr = []
  147. img_size = np.asarray(img.shape)[0:2]
  148. if nrof_faces > 1:
  149. if detect_multiple_faces:
  150. for i in range(nrof_faces):
  151. det_arr.append(np.squeeze(det[i]))
  152. else:
  153. bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1])
  154. img_center = img_size / 2
  155. offsets = np.vstack(
  156. [(det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0]])
  157. offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
  158. index = np.argmax(
  159. bounding_box_size - offset_dist_squared * 2.0) # some extra weight on the centering
  160. det_arr.append(det[index, :])
  161. else:
  162. det_arr.append(np.squeeze(det))
  163. images = np.zeros((len(det_arr), image_size, image_size, 3))
  164. for i, det in enumerate(det_arr):
  165. det = np.squeeze(det)
  166. bb = np.zeros(4, dtype=np.int32)
  167. bb[0] = np.maximum(det[0] - margin / 2, 0)
  168. bb[1] = np.maximum(det[1] - margin / 2, 0)
  169. bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
  170. bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
  171. cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
  172. # 进行图片缩放 cv2.resize(img,(w,h))
  173. scaled = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
  174. nrof_successfully_aligned += 1
  175. # 保存检测的头像
  176. filename_base = './pic_tmp'
  177. filename = os.path.basename(image_path)
  178. filename_name, file_extension = os.path.splitext(filename)
  179. #多个人脸时,在picname后加_0 _1 _2 依次累加。
  180. output_filename_n = "{}/{}_{}{}".format(filename_base, filename_name, i, file_extension)
  181. misc.imsave(output_filename_n, scaled)
  182. scaled = src.facenet.prewhiten(scaled)
  183. scaled = src.facenet.crop(scaled, False, 160)
  184. scaled = src.facenet.flip(scaled, False)
  185. images[i] = scaled
  186. if nrof_faces > 0:
  187. return images
  188. else:
  189. # 如果没有检测到人脸 直接返回一个1*3的0矩阵 多少维度都行 只要能和是不是一个图片辨别出来就行
  190. return np.zeros((1, 3))
  191. # 备用 通过urllib的方式从远程地址获取一个图片到本地
  192. # 利用该方法可以提交一个图片的url地址,则也是先保存到本地再进行后续处理
  193. def get_url_imgae(picurl):
  194. response = urllib.urlopen(picurl)
  195. pic = response.read()
  196. pic_name = "./pic_tmp/" + os.path.basename(picurl)
  197. with open(pic_name, 'wb') as f:
  198. f.write(pic)
  199. return pic_name
  200. @auth.get_password
  201. def get_password(username):
  202. if username == 'face':
  203. return 'face'
  204. return None
  205. @auth.error_handler
  206. def unauthorized():
  207. return make_response(jsonify({'error': 'Unauthorized access'}), 401)
  208. @app.errorhandler(400)
  209. def not_found(error):
  210. return make_response(jsonify({'error': 'Invalid data!'}), 400)
  211. if __name__ == '__main__':
  212. app.run(host='0.0.0.0', port=8088)