import face_recognition
from flask import Flask, jsonify, request, redirect
import numpy as np
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
ade3_image = face_recognition.load_image_file("./photo/ade3.png")
ade3_face_encoding = face_recognition.face_encodings(ade3_image)[0]
aoyama3_image = face_recognition.load_image_file("./photo/aoyama3.png")
aoyama3_face_encoding = face_recognition.face_encodings(aoyama3_image)[0]
・
・
・
concha3_image = face_recognition.load_image_file("./photo/concha3.png")
concha3_face_encoding = face_recognition.face_encodings(concha3_image)[0]
known_face_encodings = [
ade3_face_encoding,
aoyama3_face_encoding,
・
・
・
concha3_face_encoding
]
known_face_names = [
"アデミウソン",
"青山 直晃",
・
・
・
"ダビド コンチャ"
]
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
# The image file seems valid! Detect faces and return the result.
return detect_faces_in_image(file)
return '''
<!doctype html>
<title>Which player is this photo within GAMBA?</title>
<h1>写真をアップして選手の名前を教えてもらおう!</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
<br><br>
登録一覧
<br>
<img src="/static/images/players.png">
</form>
'''
def detect_faces_in_image(file_stream):
img = face_recognition.load_image_file(file_stream)
unknown_face_encodings = face_recognition.face_encodings(img)[0]
matches = face_recognition.compare_faces(known_face_encodings, unknown_face_encodings)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, unknown_face_encodings)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
if(name == "Unknown"):
face_found = False
else:
face_found = True
result = {
"face_found_in_image": face_found,
"is_name_of_picture": name
}
return jsonify(result)
if __name__ == "__main__":
app.run(host='192.168.0.32', port=5001, debug=True)
Leave a Reply