Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flask
- from flask import request, jsonify
- import numpy as np
- import PIL.Image
- import io
- from time import time
- from detector import *
- from firebase_utils import *
- from recognize import *
- app = flask.Flask(__name__)
- app.config["DEBUG"] = True
- number = 0
- data = get_cars()
- vals, known_faces, known_ids = encode_people()
- @app.route('/', methods=['GET'])
- def home():
- return '''<h1>Face recog</h1></br><form enctype="multipart/form-data" action="/check_face" method="post"><input type="file" name="a"/><input type="submit"/></form>
- <h1>Encode face</h1></br><form enctype="multipart/form-data" action="/encode_face" method="post"><input type="file" name="a"/><input type="submit"/></form>
- <h1>Car Plate Number</h1></br><form enctype="multipart/form-data" action="/get_plate" method="post"><input type="file" name="a"/><input type="submit"/></form>'''
- def bytes_to_ndarray(bytes):
- bytes_io = bytearray(bytes)
- img = PIL.Image.open(io.BytesIO(bytes_io))
- return np.array(img)
- @app.route('/get_plate', methods=['POST'])
- def get_plate():
- global data
- file = request.files["a"].read()
- npimg = np.fromstring(file, np.uint8)
- img = bytes_to_ndarray(npimg)
- message = get_plate_number(img)
- if message["code"] == 1:
- return jsonify(match_car(message["n_1"], message["n_2"], data))
- return jsonify(message)
- @app.route('/check_face', methods=['POST'])
- def check_face():
- file = request.files["a"].read()
- npimg = np.fromstring(file, np.uint8)
- img = bytes_to_ndarray(npimg)
- message = recognize(img, known_faces, known_ids, vals)
- return jsonify(message)
- @app.route('/encode_face', methods=['POST'])
- def encode_face():
- global number
- if len(request.files) > 0 and number ==0:
- number += 1
- file = request.files["a"].read()
- npimg = np.fromstring(file, np.uint8)
- img = bytes_to_ndarray(npimg)
- locations = get_face_locations(img)
- encodings = face_encodings(img, locations, model="large")
- if len(encodings) > 1:
- return {"code": 0, "status": "More than one face found"}
- if len(encodings) == 0:
- return {"code": -1, "status": "No face found, take a clearer image please"}
- number -= 1
- return {"face_encode":str(encodings[0].tolist()), "code": 1}
- else:
- print("in use")
- return ""
- @app.route('/test', methods=['POST', 'GET'])
- def test():
- print(str(request.files))
- return "hello"
- if __name__ == '__main__':
- app.run(host='0.0.0.0', threaded=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement