-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (44 loc) · 1.68 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
from flask import Flask, redirect, render_template, request
from PIL import Image
import torchvision.transforms.functional as TF
import CNN
import numpy as np
import torch
import pandas as pd
disease_info = pd.read_csv('disease_info.csv' , encoding='cp1252')
model = CNN.CNN(39)
model.load_state_dict(torch.load("plant_disease_model_1_latest.pt"))
model.eval()
def prediction(image_path):
image = Image.open(image_path)
image = image.resize((224, 224))
input_data = TF.to_tensor(image)
input_data = input_data.view((-1, 3, 224, 224))
output = model(input_data)
output = output.detach().numpy()
index = np.argmax(output)
return index
app = Flask(__name__)
@app.route('/')
@app.route('/index')
def ai_engine_page():
return render_template('index.html')
@app.route('/submit', methods=['POST'])
def submit():
if request.method == 'POST':
image = request.files['image']
filename = image.filename
file_path = os.path.join('static/uploads', filename)
image.save(file_path)
print(file_path)
pred = prediction(file_path)
title = disease_info['disease_name'][pred]
description = disease_info['description'][pred]
prevent = disease_info['Possible Steps'][pred]
image_url = disease_info['image_url'][pred]
return render_template('index.html', filename=filename, title=title, description=description, prevent=prevent, image_url=image_url)
# Add a default response in case the request method is not POST
return redirect('/index')
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5001)