-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
61 lines (47 loc) · 1.75 KB
/
api.py
File metadata and controls
61 lines (47 loc) · 1.75 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
# way to upload image
# way to save the image
# function to make prediction on the image
# show the result
import os
from flask import Flask
from flask import request
from flask import render_template
import numpy as np
import random
app = Flask(__name__)
UPLOAD_FOLDER = "/home/sankarshan/Documents/code/web_api_for_computer_vision/static"
# GET: accessing the website
# POST: accessing the form
@app.route("/", methods = ["GET", "POST"])
def upload_predict():
# this function should show index.html
# simulate the model prediction value
if request.method == "POST":
image_file = request.files["image"]
if image_file:
image_location = os.path.join(
UPLOAD_FOLDER,
image_file.filename
)
image_file.save(image_location)
pred_class, pred_proba = get_prediction()
return render_template("index.html",
prediction_class = pred_class,
prediction_proba = pred_proba,
image_file = image_file.filename)
return render_template("index.html",
prediction_class = "no image given",
prediction_proba = 0, image_file=None)
def get_prediction():
"""This is a random prediction working as a baseline
Returns:
Prediction class and probability
"""
################################################
# put your original prediction code here
################################################
pred_proba = np.round(np.random.rand(), 4)
pred_class = random.choice(["cat", "dog", "monkey"])
return pred_class, pred_proba
if __name__ == "__main__":
app.run(port=12000, debug=True)