Advertisement
Maroxtn

Untitled

May 27th, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import flask
  2. from flask import request, jsonify
  3. import numpy as np
  4. import PIL.Image
  5. import io
  6. from time import time
  7.  
  8. from detector import *
  9. from firebase_utils import *
  10. from recognize import *
  11.  
  12.  
  13. app = flask.Flask(__name__)
  14. app.config["DEBUG"] = True
  15.  
  16. number = 0
  17.  
  18. data = get_cars()
  19. vals, known_faces, known_ids = encode_people()
  20.  
  21.  
  22.  
  23. @app.route('/', methods=['GET'])
  24. def home():
  25. 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>
  26. <h1>Encode face</h1></br><form enctype="multipart/form-data" action="/encode_face" method="post"><input type="file" name="a"/><input type="submit"/></form>
  27. <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>'''
  28.  
  29.  
  30. def bytes_to_ndarray(bytes):
  31. bytes_io = bytearray(bytes)
  32. img = PIL.Image.open(io.BytesIO(bytes_io))
  33. return np.array(img)
  34.  
  35. @app.route('/get_plate', methods=['POST'])
  36. def get_plate():
  37.  
  38. global data
  39. file = request.files["a"].read()
  40. npimg = np.fromstring(file, np.uint8)
  41. img = bytes_to_ndarray(npimg)
  42.  
  43. message = get_plate_number(img)
  44.  
  45. if message["code"] == 1:
  46. return jsonify(match_car(message["n_1"], message["n_2"], data))
  47.  
  48. return jsonify(message)
  49.  
  50.  
  51. @app.route('/check_face', methods=['POST'])
  52. def check_face():
  53. file = request.files["a"].read()
  54. npimg = np.fromstring(file, np.uint8)
  55. img = bytes_to_ndarray(npimg)
  56.  
  57. message = recognize(img, known_faces, known_ids, vals)
  58.  
  59. return jsonify(message)
  60.  
  61. @app.route('/encode_face', methods=['POST'])
  62. def encode_face():
  63.  
  64. global number
  65. if len(request.files) > 0 and number ==0:
  66.  
  67. number += 1
  68. file = request.files["a"].read()
  69. npimg = np.fromstring(file, np.uint8)
  70. img = bytes_to_ndarray(npimg)
  71.  
  72. locations = get_face_locations(img)
  73. encodings = face_encodings(img, locations, model="large")
  74.  
  75. if len(encodings) > 1:
  76. return {"code": 0, "status": "More than one face found"}
  77.  
  78. if len(encodings) == 0:
  79. return {"code": -1, "status": "No face found, take a clearer image please"}
  80.  
  81. number -= 1
  82.  
  83. return {"face_encode":str(encodings[0].tolist()), "code": 1}
  84. else:
  85. print("in use")
  86.  
  87. return ""
  88.  
  89. @app.route('/test', methods=['POST', 'GET'])
  90. def test():
  91. print(str(request.files))
  92. return "hello"
  93.  
  94. if __name__ == '__main__':
  95. app.run(host='0.0.0.0', threaded=True)
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement