-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
133 lines (105 loc) · 4.11 KB
/
server.py
File metadata and controls
133 lines (105 loc) · 4.11 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import random
import string
import time
from flask import Flask, flash, request, redirect, url_for, send_from_directory, jsonify
from flask_cors import CORS, cross_origin
from werkzeug.utils import secure_filename
COST = 300
UPLOAD_FOLDER = os.path.join(os.getcwd(), "static")
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['CORS_HEADERS'] = 'Content-Type'
app.config['SECRET_KEY'] = 'myverysecretkey'
cors = CORS(app)
def random_string(stringLength=10):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_extension(filename):
return filename.rsplit('.', 1)[1].lower()
def get_quality(good, not_good):
return (good * 100 / (good + not_good))
def get_price(quality):
return (COST * quality) / 100
@app.route('/', methods=['GET', 'POST'])
@cross_origin(origin='localhost',headers=['Content-Type','Authorization'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return jsonify({
"message": "No file part"
})
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return jsonify({
"message": "No selected file"
})
if file and allowed_file(file.filename):
# save uploaded file to static with a random name
filename = secure_filename(file.filename)
ex = get_extension(filename)
new_filename = random_string(4) + "." + ex
# TODO: change image resolution
file.save(os.path.join(app.config['UPLOAD_FOLDER'], new_filename))
from wheat_quality_predictor import predict
path_file = os.path.join(app.config['UPLOAD_FOLDER'], new_filename)
print("Predicting", path_file)
good, not_good = predict(path_file)
print("Prediction Done")
# return payload
response = jsonify({
"url": url_for('uploaded_file', filename=new_filename),
"quality": get_quality(good, not_good),
"price": get_price(get_quality(good, not_good))
})
return response
elif request.method == 'GET':
return "AgroAI API"
# Test route
@app.route('/image', methods=['GET', 'POST'])
@cross_origin(origin='localhost',headers=['Content-Type','Authorization'])
def test_image():
print("Getting image")
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return jsonify({
"message": "No file part"
})
file = request.files['file']
print(file)
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return jsonify({
"message": "No selected file"
})
if file and allowed_file(file.filename):
# save uploaded file to static with a random name
filename = secure_filename(file.filename)
ex = get_extension(filename)
new_filename = random_string(4) + "." + ex
file.save(os.path.join(app.config['UPLOAD_FOLDER'], new_filename))
print("Testing image successfuly")
time.sleep(10)
response = jsonify({
"url": url_for('uploaded_file', filename=new_filename),
"quality": 50,
"price": 100,
})
return response
# Route to fetch uploaded images
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)