Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,58 +16,58 @@
|
|
16 |
|
17 |
|
18 |
|
19 |
-
import streamlit as st
|
20 |
-
from tensorflow.keras.models import load_model
|
21 |
-
from tensorflow.keras.preprocessing import image
|
22 |
-
import numpy as np
|
23 |
-
from PIL import Image
|
24 |
|
25 |
-
# Load the pre-trained models
|
26 |
-
@st.cache_resource
|
27 |
-
def load_models():
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
model1, model2 = load_models()
|
33 |
|
34 |
-
# Label mappings
|
35 |
-
label_map1 = {
|
36 |
-
|
37 |
-
|
38 |
-
}
|
39 |
|
40 |
-
label_map2 = {
|
41 |
-
|
42 |
-
}
|
43 |
|
44 |
-
# Streamlit app layout
|
45 |
-
st.title("Fruit Classifier")
|
46 |
|
47 |
-
# Upload image
|
48 |
-
uploaded_file = st.file_uploader("Choose an image of a fruit", type=["jpg", "jpeg", "png"])
|
49 |
|
50 |
-
if uploaded_file is not None:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
|
73 |
|
@@ -79,6 +79,18 @@ if uploaded_file is not None:
|
|
79 |
|
80 |
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
# import streamlit as st
|
83 |
# import numpy as np
|
84 |
# import cv2
|
@@ -184,3 +196,138 @@ if uploaded_file is not None:
|
|
184 |
|
185 |
# except Exception as e:
|
186 |
# st.error(f"An error occurred during processing: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
|
19 |
+
# import streamlit as st
|
20 |
+
# from tensorflow.keras.models import load_model
|
21 |
+
# from tensorflow.keras.preprocessing import image
|
22 |
+
# import numpy as np
|
23 |
+
# from PIL import Image
|
24 |
|
25 |
+
# # Load the pre-trained models
|
26 |
+
# @st.cache_resource
|
27 |
+
# def load_models():
|
28 |
+
# model1 = load_model('name_model_inception.h5') # Update with your Hugging Face model path
|
29 |
+
# model2 = load_model('type_model_inception.h5') # Update with your Hugging Face model path
|
30 |
+
# return model1, model2
|
31 |
|
32 |
+
# model1, model2 = load_models()
|
33 |
|
34 |
+
# # Label mappings
|
35 |
+
# label_map1 = {
|
36 |
+
# 0: "Banana", 1: "Cucumber", 2: "Grape", 3: "Kaki", 4: "Papaya",
|
37 |
+
# 5: "Peach", 6: "Pear", 7: "Pepper", 8: "Strawberry", 9: "Watermelon", 10: "Tomato"
|
38 |
+
# }
|
39 |
|
40 |
+
# label_map2 = {
|
41 |
+
# 0: "Good", 1: "Mild", 2: "Rotten"
|
42 |
+
# }
|
43 |
|
44 |
+
# # Streamlit app layout
|
45 |
+
# st.title("Fruit Classifier")
|
46 |
|
47 |
+
# # Upload image
|
48 |
+
# uploaded_file = st.file_uploader("Choose an image of a fruit", type=["jpg", "jpeg", "png"])
|
49 |
|
50 |
+
# if uploaded_file is not None:
|
51 |
+
# # Display the uploaded image
|
52 |
+
# img = Image.open(uploaded_file)
|
53 |
+
# st.image(img, caption="Uploaded Image", use_column_width=True)
|
54 |
|
55 |
+
# # Preprocess the image
|
56 |
+
# img = img.resize((224, 224)) # Resize image to match the model input
|
57 |
+
# img_array = image.img_to_array(img)
|
58 |
+
# img_array = np.expand_dims(img_array, axis=0)
|
59 |
+
# img_array = img_array / 255.0 # Normalize the image
|
60 |
|
61 |
+
# # Make predictions
|
62 |
+
# pred1 = model1.predict(img_array)
|
63 |
+
# pred2 = model2.predict(img_array)
|
64 |
|
65 |
+
# predicted_class1 = np.argmax(pred1, axis=1)
|
66 |
+
# predicted_class2 = np.argmax(pred2, axis=1)
|
67 |
|
68 |
+
# # Display results
|
69 |
+
# st.write(f"**Type Detection**: {label_map1[predicted_class1[0]]}")
|
70 |
+
# st.write(f"**Condition Detection**: {label_map2[predicted_class2[0]]}")
|
71 |
|
72 |
|
73 |
|
|
|
79 |
|
80 |
|
81 |
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
# import streamlit as st
|
95 |
# import numpy as np
|
96 |
# import cv2
|
|
|
196 |
|
197 |
# except Exception as e:
|
198 |
# st.error(f"An error occurred during processing: {str(e)}")
|
199 |
+
|
200 |
+
|
201 |
+
|
202 |
+
|
203 |
+
|
204 |
+
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
+
|
209 |
+
|
210 |
+
|
211 |
+
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
import streamlit as st
|
216 |
+
import numpy as np
|
217 |
+
import cv2
|
218 |
+
import warnings
|
219 |
+
import os
|
220 |
+
|
221 |
+
# Suppress warnings
|
222 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
223 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
224 |
+
|
225 |
+
# Try importing TensorFlow
|
226 |
+
try:
|
227 |
+
from tensorflow.keras.models import load_model
|
228 |
+
from tensorflow.keras.preprocessing import image
|
229 |
+
except ImportError:
|
230 |
+
st.error("Failed to import TensorFlow. Please make sure it's installed correctly.")
|
231 |
+
|
232 |
+
# Try importing PyTorch and Detectron2
|
233 |
+
try:
|
234 |
+
import torch
|
235 |
+
import detectron2
|
236 |
+
except ImportError:
|
237 |
+
with st.spinner("Installing PyTorch and Detectron2..."):
|
238 |
+
os.system("pip install torch torchvision")
|
239 |
+
os.system("pip install 'git+https://github.com/facebookresearch/detectron2.git'")
|
240 |
+
|
241 |
+
import torch
|
242 |
+
import detectron2
|
243 |
+
|
244 |
+
from detectron2.engine import DefaultPredictor
|
245 |
+
from detectron2.config import get_cfg
|
246 |
+
from detectron2.utils.visualizer import Visualizer
|
247 |
+
from detectron2.data import MetadataCatalog
|
248 |
+
|
249 |
+
# Load the trained models
|
250 |
+
@st.cache_resource
|
251 |
+
def load_models():
|
252 |
+
try:
|
253 |
+
model_path_name = 'name_model_inception.h5'
|
254 |
+
model_path_quality = 'type_model_inception.h5'
|
255 |
+
model_name = load_model(model_path_name)
|
256 |
+
model_quality = load_model(model_path_quality)
|
257 |
+
return model_name, model_quality
|
258 |
+
except Exception as e:
|
259 |
+
st.error(f"Failed to load models: {str(e)}")
|
260 |
+
return None, None
|
261 |
+
|
262 |
+
model_name, model_quality = load_models()
|
263 |
+
|
264 |
+
# Streamlit app title
|
265 |
+
st.title("Watermelon Quality and Damage Detection")
|
266 |
+
|
267 |
+
# Upload image
|
268 |
+
uploaded_file = st.file_uploader("Choose a watermelon image...", type=["jpg", "jpeg", "png"])
|
269 |
+
|
270 |
+
if uploaded_file is not None:
|
271 |
+
try:
|
272 |
+
# Load the image
|
273 |
+
img = image.load_img(uploaded_file, target_size=(224, 224))
|
274 |
+
img_array = image.img_to_array(img)
|
275 |
+
img_array = np.expand_dims(img_array, axis=0)
|
276 |
+
img_array /= 255.0
|
277 |
+
|
278 |
+
# Display uploaded image
|
279 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
280 |
+
|
281 |
+
# Predict watermelon name
|
282 |
+
pred_name = model_name.predict(img_array)
|
283 |
+
predicted_name = 'Watermelon'
|
284 |
+
|
285 |
+
# Predict watermelon quality
|
286 |
+
pred_quality = model_quality.predict(img_array)
|
287 |
+
predicted_class_quality = np.argmax(pred_quality, axis=1)
|
288 |
+
|
289 |
+
# Define labels for watermelon quality
|
290 |
+
label_map_quality = {
|
291 |
+
0: "Good",
|
292 |
+
1: "Mild",
|
293 |
+
2: "Rotten"
|
294 |
+
}
|
295 |
+
|
296 |
+
predicted_quality = label_map_quality[predicted_class_quality[0]]
|
297 |
+
|
298 |
+
# Display predictions
|
299 |
+
st.write(f"Fruit Type Detection: {predicted_name}")
|
300 |
+
st.write(f"Fruit Quality Classification: {predicted_quality}")
|
301 |
+
|
302 |
+
# If the quality is 'Mild' or 'Rotten', pass the image to the mask detection model
|
303 |
+
if predicted_quality in ["Mild", "Rotten"]:
|
304 |
+
st.write("Passing the image to the mask detection model for damage detection...")
|
305 |
+
|
306 |
+
# Load the image again for the mask detection (Detectron2 requires the original image)
|
307 |
+
im = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
|
308 |
+
|
309 |
+
# Setup Detectron2 configuration for watermelon
|
310 |
+
@st.cache_resource
|
311 |
+
def load_detectron_model():
|
312 |
+
cfg = get_cfg()
|
313 |
+
cfg.merge_from_file("watermelon.yaml")
|
314 |
+
cfg.MODEL.WEIGHTS = "Watermelon_model.pth"
|
315 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
316 |
+
cfg.MODEL.DEVICE = 'cpu' # Use CPU for inference
|
317 |
+
predictor = DefaultPredictor(cfg)
|
318 |
+
return predictor
|
319 |
+
|
320 |
+
predictor = load_detectron_model()
|
321 |
+
|
322 |
+
# Run prediction on the image
|
323 |
+
outputs = predictor(im)
|
324 |
+
|
325 |
+
# Visualize the predictions
|
326 |
+
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=0.8)
|
327 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
328 |
+
|
329 |
+
# Display the output
|
330 |
+
st.image(out.get_image()[:, :, ::-1], caption="Detected Damage", use_column_width=True)
|
331 |
+
|
332 |
+
except Exception as e:
|
333 |
+
st.error(f"An error occurred during processing: {str(e)}")
|