-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_app.py
104 lines (77 loc) · 3.1 KB
/
Final_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
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
from flask import Flask, request, jsonify, render_template
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image
import mysql.connector
import os
app = Flask(__name__)
# Load the trained model
model = tf.keras.models.load_model("medicinal_plant_model.h5")
db_config = {
"host": "localhost",
"user": "root",
"password": "gold",
"database": "sih"
}
# Define a function for preprocessing the input image
def preprocess_image(image_path):
img = image.load_img(image_path, target_size=(128, 128))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0 # Normalize the image
return img_array
# Function to predict the plant from an input image
def predict_plant(image_path):
# Preprocess the image
img = preprocess_image(image_path)
# Make predictions
predictions = model.predict(img)
# Define the list of plant names
query = "SELECT Scientific_Name FROM sihdata"
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
cursor.execute(query)
plant_names = [row[0] for row in cursor.fetchall()]
cursor.close()
conn.close()
# Get the top predicted class
top_prediction = np.argmax(predictions[0])
# Map the prediction index to the corresponding plant name
predicted_plant = plant_names[top_prediction]
return predicted_plant
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file:
# Save the uploaded image temporarily
uploaded_image_path = "temp_image.jpg"
file.save(uploaded_image_path)
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
# Predict the plant from the uploaded image
predicted_plant = predict_plant(uploaded_image_path)
query = "SELECT Genus_Name, Species_Name, Usable_Part, Uses, Places FROM sihdata WHERE Scientific_Name = %s"
cursor.execute(query, (predicted_plant,))
plant_info = cursor.fetchone()
cursor.close()
conn.close()
# Delete the temporary image
os.remove(uploaded_image_path)
# Return the predictions as JSON response
response_data = {
'Scientific_Name': predicted_plant,
'genusName': plant_info[0],
'speciesName': plant_info[1],
'whichPart': plant_info[2],
'usesOfPart': plant_info[3],
'findPlace': plant_info[4]
}
return jsonify(response_data)
return render_template('Final.html')
if __name__ == '__main__':
app.run(debug=True)