1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- from PIL import ImageFile
- from flask import Flask, request, jsonify
- import face_recognition
- import base64
- from io import BytesIO
- import joblib
- import numpy as np
- ImageFile.SAFEBLOCK = 2048 * 2048
- app = Flask(__name__)
- model_file_name = "saved_model.pkl"
- clf = None
- classes = None
- dummy_data = [
- {
- "name": "Bayu",
- "address": "299 St Louis Road Oak Forest, IL 60452",
- "nik": "1000076456784631"
- },
- {
- "name": "Dio",
- "address": "22 Whitemarsh St. Mansfield, MA 02048",
- "nik": "1000024792887549"
- },
- {
- "name": "Hadi",
- "address": "643 Honey Creek Dr. Milledgeville, GA 31061",
- "nik": "1000038502830420"
- },
- {
- "name": "Kevin",
- "address": "881 Cooper Ave. Hummelstown, PA 17036",
- "nik": "1000045356476664"
- },
- {
- "name": "Matrix",
- "address": "580 Glenwood Dr. Garner, NC 27529",
- "nik": "1000023452134598"
- },
- {
- "name": "Surya",
- "address": "909 South St Paul Street Hopewell, VA 23860",
- "nik": "1000075656784734"
- },
- ]
- ssl = None
- @app.route('/predict', methods=['POST'])
- def predict():
- result = []
- if "image" in request.json:
- im_b64 = request.json["image"]
- elif "image" in request.files:
- im_b64 = request.files["image"]
- elif "image" in request.form:
- im_b64 = request.form["image"]
- else:
- return {"error": "Error reading image"}
- im_bytes = base64.b64decode(im_b64)
- im_file = BytesIO(im_bytes)
- test_image = face_recognition.load_image_file(im_file)
- face_locations = face_recognition.face_locations(test_image)
- no = len(face_locations)
- for i in range(no):
- test_image_enc = face_recognition.face_encodings(test_image)[i]
- proba_list = clf.predict_proba([test_image_enc])
- i = np.argmax(proba_list)
- proba = list(*proba_list)[i]
- name = dummy_data[i]["name"]
- address = dummy_data[i]["address"]
- nik = dummy_data[i]["nik"]
- js = {
- "id": str(i),
- "name": name,
- "address": address,
- "nik": nik,
- "proba": proba
- }
- result.append(js)
- return jsonify(result)
- if __name__ == '__main__':
- try:
- clf = joblib.load(model_file_name)
- classes = clf.classes_
- print('model loaded')
- except FileNotFoundError as e:
- print('No model here')
- exit(1)
- app.run(host='0.0.0.0', port=8349, debug=True, ssl_context=ssl)
|