Senasu commited on
Commit
8c2dfc9
·
verified ·
1 Parent(s): 396c6c7

Upload Main Files

Browse files
Files changed (3) hide show
  1. app.py +115 -0
  2. cnn_model.h5 +3 -0
  3. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from tensorflow.keras.models import load_model
3
+ from PIL import Image
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ import seaborn as sns
7
+
8
+ model = load_model('cnn_model.h5')
9
+
10
+ def process_image(img):
11
+ img = img.convert('RGB')
12
+ img = img.resize((64, 64))
13
+ img = np.array(img)
14
+ img = img / 255.0
15
+ img = np.expand_dims(img, axis=0)
16
+ return img
17
+
18
+ st.title('Fruits and Vegetables Image Recognition :grapes: :carrot: :kiwifruit: :broccoli:')
19
+ st.write('Upload a vegetable or fruit image and the model will recognize it.')
20
+
21
+ file = st.file_uploader('Select an image', type=['jpg', 'jpeg', 'png'])
22
+
23
+ if file is not None:
24
+ # Display the uploaded image
25
+ img = Image.open(file)
26
+ st.image(img, caption='Uploaded Image')
27
+
28
+ # Preprocess the image
29
+ image = process_image(img)
30
+
31
+ # Model prediction
32
+ with st.spinner('Classifying the image...'):
33
+ predictions = model.predict(image)
34
+ predicted_class = np.argmax(predictions)
35
+ predicted_prob = predictions[0][predicted_class]
36
+
37
+ # Class names for prediction
38
+ class_names = ['capsicum',
39
+ 'sweetcorn',
40
+ 'orange',
41
+ 'tomato',
42
+ 'turnip',
43
+ 'ginger',
44
+ 'raddish',
45
+ 'pomegranate',
46
+ 'pineapple',
47
+ 'jalepeno',
48
+ 'apple',
49
+ 'carrot',
50
+ 'lettuce',
51
+ 'bell pepper',
52
+ 'eggplant',
53
+ 'beetroot',
54
+ 'kiwi',
55
+ 'pear',
56
+ 'cabbage',
57
+ 'cauliflower',
58
+ 'paprika',
59
+ 'lemon',
60
+ 'sweetpotato',
61
+ 'grapes',
62
+ 'cucumber',
63
+ 'corn',
64
+ 'banana',
65
+ 'garlic',
66
+ 'chilli pepper',
67
+ 'watermelon',
68
+ 'mango',
69
+ 'peas',
70
+ 'onion',
71
+ 'potato',
72
+ 'spinach',
73
+ 'soy beans']
74
+
75
+ # Display the prediction
76
+ st.subheader(f"Prediction: {class_names[predicted_class]}")
77
+ st.write(f"Confidence: {predicted_prob * 100:.2f}%")
78
+
79
+ # Display prediction probabilities
80
+ st.write("Prediction Probabilities for Each Class:")
81
+ probabilities = predictions[0]
82
+ prob_dict = {class_names[i]: probabilities[i] for i in range(len(class_names))}
83
+
84
+ # Plot settings
85
+ sns.set(style="white") # Clean style with no grid background
86
+
87
+ # Create the figure
88
+ fig, ax = plt.subplots(figsize=(12, 8)) # Adjust figure size for better readability
89
+
90
+ # Plot the bar chart
91
+ ax.bar(list(prob_dict.keys()), list(prob_dict.values()), color='#197339', edgecolor='black')
92
+ ax.set_ylabel('Probability', fontsize=14)
93
+ ax.set_title('Prediction Probabilities for Each Class', fontsize=16)
94
+
95
+ # Rotate x-axis labels for better readability
96
+ plt.xticks(rotation=90, ha='right', fontsize=10)
97
+
98
+ # Annotate bars with percentage values
99
+ for index, value in enumerate(prob_dict.values()):
100
+ ax.text(index, value, f'{value * 100:.2f}%', va='bottom', ha='center', fontsize=10)
101
+
102
+ # Remove background grid and spines
103
+ ax.spines['top'].set_visible(False)
104
+ ax.spines['right'].set_visible(False)
105
+ ax.spines['left'].set_visible(False)
106
+ ax.spines['bottom'].set_visible(False)
107
+ ax.grid(False)
108
+
109
+ # Adjust layout to prevent clipping
110
+ fig.tight_layout()
111
+
112
+ # Display the plot in Streamlit
113
+ st.pyplot(fig)
114
+
115
+
cnn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc7b471fe33286efef8309dfa70e30846d46453843b5e5b7241126fe04a9b64f
3
+ size 134111056
requirements.txt ADDED
File without changes