wko-projekt/API/api.py

61 lines
1.7 KiB
Python
Raw Normal View History

# python -m pip install flask
2023-02-01 01:09:19 +01:00
# export FLASK_APP=API/api.py
# flask run --without-threads
2023-01-27 00:11:51 +01:00
from flask import Flask, request, jsonify
from yolo import YOLO
2023-01-27 00:11:51 +01:00
import json
import yolo_video
import base64
app = Flask(__name__)
2023-02-01 01:09:19 +01:00
yolo_model = YOLO()
2023-01-27 00:11:51 +01:00
"""API_address/ping/<int:id>"""
@app.route("/ping/<int:id>", methods=["GET"])
def get_ping(id: int):
print(f'Request for PING {id=} received')
id = id + 1
return jsonify(id), 200
"""API_address/test"""
@app.get("/test")
def test():
o = {'str': 'okokok'}
return jsonify(o), 200
2023-01-27 01:30:57 +01:00
@app.route('/getPlatesNumber', methods=['POST'])
def getPlatesNumber():
res = json.loads(request.data)
# decoded data
decoded_data = base64.b64decode(res["imageFile"])
2023-02-01 01:09:19 +01:00
img_file = open('API/base64.png', 'wb')
2023-01-27 01:30:57 +01:00
img_file.write(decoded_data)
img_file.close()
2023-02-01 01:09:19 +01:00
image_path = r'API/base64.png'
2023-01-27 01:30:57 +01:00
base64_b, texts = yolo_video.detect_license_plate(model=yolo_model, img_path=image_path, i=0)
res = {'platesNumber': texts}
return jsonify(res), 200
"""API_address/detectLicense?img="""
2023-01-27 00:11:51 +01:00
@app.route("/detectLicense", methods=['POST'])
def detectLicensePlate():
2023-01-27 00:11:51 +01:00
res = json.loads(request.data)
# decoded data
decoded_data = base64.b64decode(res["imageFile"])
2023-02-01 01:09:19 +01:00
img_file = open('API/base64.png', 'wb')
2023-01-27 00:11:51 +01:00
img_file.write(decoded_data)
img_file.close()
2023-02-01 01:09:19 +01:00
image_path = r'API/base64.png'
2023-01-27 01:30:57 +01:00
base64_b, texts = yolo_video.detect_license_plate(model=yolo_model, img_path=image_path, i=0)
2023-01-27 00:11:51 +01:00
return jsonify(str(base64_b)), 200
2023-02-01 01:09:19 +01:00
# if __name__ == '__main__':
# app.run(threaded=False, host='0.0.0.0', port=8080)
2023-01-27 00:11:51 +01:00
2023-02-01 01:09:19 +01:00
### to run api just paste following to terminal from root dir of project
# export FLASK_APP=API/api.py
# flask run --without-threads