-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
43 lines (34 loc) · 1.29 KB
/
app.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
42
43
import torch
import torchvision.transforms as transforms
from flask import Flask, request, app, jsonify, url_for, render_template, redirect, flash, session, escape
from PIL import Image
app = Flask(__name__)
labels = {}
i=0
with open("labels.txt") as f:
for line in f:
labels[i]= line.strip()
i+=1
@app.route('/')
def home():
return render_template('home.html')
@app.route('/predict', methods = ['POST'])
def predict():
model = torch.jit.load('resnet_food.pt', map_location=torch.device('cpu'))
model.eval()
data = request.files['image']
img = Image.open(data.stream)
# print("Img height and img width: ", img.height,img.width)
transformations = transforms.Compose([transforms.Resize((224,224)),
transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
input_data = transformations(img)
input_data = input_data.unsqueeze(0)
output = model(input_data)
pred = torch.argmax(output)
conf = torch.max(output)
result = labels[pred.item()]
return render_template('home.html',result1=result,confi = '%.2f'%(conf.item()*100))
# @app.route('/predict',methods = ['POST'])
# def predict():
if __name__=='__main__':
app.run(debug=True)