-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_main.py
41 lines (31 loc) · 1.28 KB
/
api_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from flask import Flask, request, jsonify
from api_calls import predict_result, predict_color
from flask_cors import CORS
app = Flask(__name__)
CORS(app, origins=['http://localhost:3001', 'https://auto-ai.onrender.com', 'https://cardetect.tech'])
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
def allowed_file(filename):
# xxx.png
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
file = request.files.get('file')
print(file)
if file is None or file.filename == "":
return jsonify({'error': 'no file'})
if not allowed_file(file.filename):
return jsonify({'error': 'format not supported'})
try:
img_bytes = file.read()
# disabled this because it does not work well.
# if predict_result(img_bytes, check_if_car=True):
result = predict_result(img_bytes)
color_prediction = predict_color(img_bytes)
result["color"] = color_prediction
return jsonify(result)
except Exception as e:
print(e)
return jsonify({'error': 'error during prediction'})
if __name__ == "__main__":
app.run(debug=True)