hassaanik commited on
Commit
f926a86
·
verified ·
1 Parent(s): 99d3103

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +51 -0
  2. templates/index.html +52 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ from PIL import Image
3
+ from io import BytesIO
4
+ import base64
5
+ from predict import predict_potato, predict_tomato
6
+ from model import potato_model, tomato_model
7
+ import torch
8
+
9
+ app = Flask(__name__)
10
+
11
+ # Load models
12
+ potato_model.load_state_dict(torch.load("models\\potato_model_statedict__f.pth", map_location=torch.device('cpu')))
13
+ tomato_model.load_state_dict(torch.load("models\\tomato_model_statedict__f.pth", map_location=torch.device('cpu')))
14
+
15
+ # potato_model = torch.load("Models\\potato_model_statedict__f.pth", map_location=torch.device('cpu'))
16
+ # potato_model.load_state_dict(torch.load("Models\\potato_model_statedict__f.pth", map_location=torch.device('cpu')))
17
+ # tomato_model = torch.load("Models\\tomato_model_statedict__f.pth", map_location=torch.device('cpu'))
18
+ # potato_model.load_state_dict(torch.load("Models\\tomato_model_statedict__f.pth", map_location=torch.device('cpu')))
19
+
20
+
21
+ @app.route('/')
22
+ def home():
23
+ # Default to potato model
24
+ return render_template('index.html', model_type='potato')
25
+
26
+ @app.route('/predict', methods=['POST'])
27
+ def predict():
28
+ # Get the selected model type
29
+ model_type = request.form['model_type']
30
+
31
+ # Get the image file from the request
32
+ file = request.files['file']
33
+
34
+ if model_type == 'tomato':
35
+ class_name, probability, image = predict_tomato(file, tomato_model)
36
+ background_image = r'static\\tomato_background.jpg'
37
+
38
+ else:
39
+ class_name, probability, image = predict_potato(file, potato_model)
40
+ background_image = r'static\\potato_background.webp'
41
+
42
+ # Convert image to base64 format
43
+ buffered = BytesIO()
44
+ image.save(buffered, format="JPEG")
45
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
46
+
47
+ # Pass the base64 encoded image and background image to the frontend
48
+ return render_template('index.html', image=img_str, class_name=class_name, probability=f"{probability * 100:.2f}%", background_image=background_image)
49
+
50
+ if __name__ == '__main__':
51
+ app.run(debug=True)
templates/index.html ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Plant Disease Classification</title>
7
+ <style>
8
+ body {
9
+ background-image: url("{{ background_image }}");
10
+ background-size: cover;
11
+ background-position: center;
12
+ font-family: Arial, sans-serif;
13
+ color: #fff;
14
+ text-align: center;
15
+ padding: 50px;
16
+ }
17
+ .container {
18
+ background-color: rgba(0, 0, 0, 0.7);
19
+ padding: 30px;
20
+ border-radius: 10px;
21
+ display: inline-block;
22
+ }
23
+ img {
24
+ max-width: 100%;
25
+ height: auto;
26
+ border-radius: 10px;
27
+ margin-top: 20px;
28
+ }
29
+ </style>
30
+ </head>
31
+ <body>
32
+ <div class="container">
33
+ <h1>Plant Disease Classification</h1>
34
+ <form method="POST" action="/predict" enctype="multipart/form-data">
35
+ <label for="model_type">Choose a model:</label>
36
+ <select name="model_type" id="model_type">
37
+ <option value="potato" selected>Potato</option>
38
+ <option value="tomato">Tomato</option>
39
+ </select>
40
+ <br><br>
41
+ <input type="file" name="file" accept="image/*" required>
42
+ <br><br>
43
+ <button type="submit">Predict</button>
44
+ </form>
45
+ {% if image %}
46
+ <h2>Prediction: {{ class_name }}</h2>
47
+ <h3>Confidence: {{ probability }}</h3>
48
+ <img src="data:image/jpeg;base64,{{ image }}" alt="Uploaded Image">
49
+ {% endif %}
50
+ </div>
51
+ </body>
52
+ </html>