加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.py 2.70 KB
一键复制 编辑 原始数据 按行查看 历史
GhostGuest 提交于 2020-12-18 11:26 . flask服务器优化
import json
import os
import time
from flask import Flask, request, redirect, url_for, render_template
from FaceRecognition.SetFaces import Set_faces
from FaceRecognition.predict import Recognition
app = Flask(__name__)
# 待录入底库图片路径
SetFacePath = 'FaceRecognition/data/train'
# 特征向量存储路径
npypath = 'FaceRecognition/data/faceembs.npy'
# 底库人脸名字存储路径
jsonpath = 'FaceRecognition/data/facename.json'
@app.route('/')
def hello_world():
return render_template("index.html")
@app.route("/setFaces")
def setFaces():
sf = Set_faces(SetFacePath, npypath, jsonpath)
sf.startSet()
data = "{data:底库数据录入成功}"
data = json.loads(data)
return data
@app.route("/predict", methods=['POST', 'GET'])
def predict():
if request.method == 'POST':
image_file = request.files['uploadFile']
# 当前文件所在路径
base_path = os.path.dirname(__file__)
# 预测图片上传目录
upload_path = os.path.join(base_path, 'FaceRecognition/data/predict')
print(upload_path)
# 判断目录是否存在
if not os.path.exists(upload_path):
os.mkdir(upload_path)
# 预测图片保存路径
file_path = upload_path + "/" + image_file.filename
print(file_path)
# 照片压缩并保存
image_file.save(file_path)
t1 = time.time()
R = Recognition(npypath, jsonpath)
result = R.readface(file_path)
result = json.dumps(result)
print("predict_id:", result)
t2 = time.time()
print('识别耗时', t2 - t1)
return result
else:
data = "{data:不支持的请求类型}"
data = json.loads(data)
return data
@app.route('/upload', methods=['POST', 'GET'])
def upload():
if request.method == 'POST':
student_id = request.values.get("id")
print(student_id)
image_file = request.files['uploadFile']
base_path = os.path.dirname(__file__) # 当前文件所在路径
upload_path = os.path.join(base_path, 'FaceRecognition/data/train/', student_id)
print(upload_path)
if not os.path.exists(upload_path):
os.mkdir(upload_path)
file_path = upload_path + "/" + student_id + ".jpg"
print(file_path)
# 照片压缩并保存
image_file.save(file_path)
# return redirect(url_for('upload'))
data = "{data:图片上传成功}"
data = json.loads(data)
return data
# return render_template('upload.html')
else:
data = "{data:不支持的请求类型}"
data = json.loads(data)
return data
if __name__ == '__main__':
app.run(debug=True)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化