Update app.py
Browse files
app.py
CHANGED
@@ -1,345 +1,3 @@
|
|
1 |
-
# import streamlit as st
|
2 |
-
|
3 |
-
# # Set title of the app
|
4 |
-
# st.title("Simple Streamlit App")
|
5 |
-
|
6 |
-
# # Add text input
|
7 |
-
# user_input = st.text_input("Enter your name:")
|
8 |
-
|
9 |
-
# # Display the input value
|
10 |
-
# if user_input:
|
11 |
-
# st.write(f"Hello, {user_input}!")
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
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 |
-
|
74 |
-
|
75 |
-
# !git clone 'https://github.com/facebookresearch/detectron2'
|
76 |
-
# dist = distutils.core.run_setup("./detectron2/setup.py")
|
77 |
-
# !python -m pip install {' '.join([f"'{x}'" for x in dist.install_requires])}
|
78 |
-
# sys.path.insert(0, os.path.abspath('./detectron2'))
|
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
|
97 |
-
# import warnings
|
98 |
-
|
99 |
-
# # Suppress warnings
|
100 |
-
# warnings.filterwarnings("ignore", category=FutureWarning)
|
101 |
-
# warnings.filterwarnings("ignore", category=UserWarning)
|
102 |
-
|
103 |
-
# # Try importing TensorFlow
|
104 |
-
# try:
|
105 |
-
# from tensorflow.keras.models import load_model
|
106 |
-
# from tensorflow.keras.preprocessing import image
|
107 |
-
# except ImportError:
|
108 |
-
# st.error("Failed to import TensorFlow. Please make sure it's installed correctly.")
|
109 |
-
|
110 |
-
# # Try importing PyTorch and Detectron2
|
111 |
-
# try:
|
112 |
-
# import torch
|
113 |
-
# from detectron2.engine import DefaultPredictor
|
114 |
-
# from detectron2.config import get_cfg
|
115 |
-
# from detectron2.utils.visualizer import Visualizer
|
116 |
-
# from detectron2.data import MetadataCatalog
|
117 |
-
# except ImportError:
|
118 |
-
# st.error("Failed to import PyTorch or Detectron2. Please make sure they're installed correctly.")
|
119 |
-
|
120 |
-
# # Load the trained models
|
121 |
-
# try:
|
122 |
-
# model_path_name = 'name_model_inception.h5'
|
123 |
-
# model_path_quality = 'type_model_inception.h5'
|
124 |
-
# detectron_config_path = 'watermelon.yaml'
|
125 |
-
# detectron_weights_path = 'Watermelon_model.pth'
|
126 |
-
|
127 |
-
# model_name = load_model(model_path_name)
|
128 |
-
# model_quality = load_model(model_path_quality)
|
129 |
-
# except Exception as e:
|
130 |
-
# st.error(f"Failed to load models: {str(e)}")
|
131 |
-
|
132 |
-
# # Streamlit app title
|
133 |
-
# st.title("Watermelon Quality and Damage Detection")
|
134 |
-
|
135 |
-
# # Upload image
|
136 |
-
# uploaded_file = st.file_uploader("Choose a watermelon image...", type=["jpg", "jpeg", "png"])
|
137 |
-
|
138 |
-
# if uploaded_file is not None:
|
139 |
-
# try:
|
140 |
-
# # Load the image
|
141 |
-
# img = image.load_img(uploaded_file, target_size=(224, 224))
|
142 |
-
# img_array = image.img_to_array(img)
|
143 |
-
# img_array = np.expand_dims(img_array, axis=0)
|
144 |
-
# img_array /= 255.0
|
145 |
-
|
146 |
-
# # Display uploaded image
|
147 |
-
# st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
148 |
-
|
149 |
-
# # Predict watermelon name
|
150 |
-
# pred_name = model_name.predict(img_array)
|
151 |
-
# predicted_name = 'Watermelon'
|
152 |
-
|
153 |
-
# # Predict watermelon quality
|
154 |
-
# pred_quality = model_quality.predict(img_array)
|
155 |
-
# predicted_class_quality = np.argmax(pred_quality, axis=1)
|
156 |
-
|
157 |
-
# # Define labels for watermelon quality
|
158 |
-
# label_map_quality = {
|
159 |
-
# 0: "Good",
|
160 |
-
# 1: "Mild",
|
161 |
-
# 2: "Rotten"
|
162 |
-
# }
|
163 |
-
|
164 |
-
# predicted_quality = label_map_quality[predicted_class_quality[0]]
|
165 |
-
|
166 |
-
# # Display predictions
|
167 |
-
# st.write(f"Fruit Type Detection: {predicted_name}")
|
168 |
-
# st.write(f"Fruit Quality Classification: {predicted_quality}")
|
169 |
-
|
170 |
-
# # If the quality is 'Mild' or 'Rotten', pass the image to the mask detection model
|
171 |
-
# if predicted_quality in ["Mild", "Rotten"]:
|
172 |
-
# st.write("Passing the image to the mask detection model for damage detection...")
|
173 |
-
|
174 |
-
# # Load the image again for the mask detection (Detectron2 requires the original image)
|
175 |
-
# im = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
|
176 |
-
|
177 |
-
# # Setup Detectron2 configuration for watermelon
|
178 |
-
# cfg = get_cfg()
|
179 |
-
# cfg.merge_from_file(detectron_config_path)
|
180 |
-
# cfg.MODEL.WEIGHTS = detectron_weights_path
|
181 |
-
# cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
182 |
-
# cfg.MODEL.DEVICE = 'cpu' # Use CPU for inference
|
183 |
-
|
184 |
-
# predictor = DefaultPredictor(cfg)
|
185 |
-
# predictor.model.load_state_dict(torch.load(detectron_weights_path, map_location=torch.device('cpu')))
|
186 |
-
|
187 |
-
# # Run prediction on the image
|
188 |
-
# outputs = predictor(im)
|
189 |
-
|
190 |
-
# # Visualize the predictions
|
191 |
-
# v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=0.8)
|
192 |
-
# out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
193 |
-
|
194 |
-
# # Display the output
|
195 |
-
# st.image(out.get_image()[:, :, ::-1], caption="Detected Damage", use_column_width=True)
|
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)}")
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
# ///////////////////////////////////Working
|
342 |
-
|
343 |
|
344 |
import streamlit as st
|
345 |
import numpy as np
|
@@ -370,219 +28,6 @@ except ImportError:
|
|
370 |
import torch
|
371 |
import detectron2
|
372 |
|
373 |
-
from detectron2.engine import DefaultPredictor
|
374 |
-
from detectron2.config import get_cfg
|
375 |
-
from detectron2.utils.visualizer import Visualizer
|
376 |
-
from detectron2.data import MetadataCatalog
|
377 |
-
|
378 |
-
# # Load the trained models
|
379 |
-
# @st.cache_resource
|
380 |
-
# def load_models():
|
381 |
-
# try:
|
382 |
-
# model_path_name = 'name_model_inception.h5'
|
383 |
-
# model_path_quality = 'type_model_inception.h5'
|
384 |
-
# model_name = load_model(model_path_name)
|
385 |
-
# model_quality = load_model(model_path_quality)
|
386 |
-
# return model_name, model_quality
|
387 |
-
# except Exception as e:
|
388 |
-
# st.error(f"Failed to load models: {str(e)}")
|
389 |
-
# return None, None
|
390 |
-
|
391 |
-
# model_name, model_quality = load_models()
|
392 |
-
|
393 |
-
# # Setup Detectron2 configuration for watermelon
|
394 |
-
# @st.cache_resource
|
395 |
-
# def load_detectron_model():
|
396 |
-
# cfg = get_cfg()
|
397 |
-
# cfg.merge_from_file("watermelon.yaml")
|
398 |
-
# cfg.MODEL.WEIGHTS = "Watermelon_model.pth"
|
399 |
-
# cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
400 |
-
# cfg.MODEL.DEVICE = 'cpu' # Use CPU for inference
|
401 |
-
# predictor = DefaultPredictor(cfg)
|
402 |
-
# return predictor, cfg
|
403 |
-
|
404 |
-
# predictor, cfg = load_detectron_model()
|
405 |
-
|
406 |
-
# # Streamlit app title
|
407 |
-
# st.title("Watermelon Quality and Damage Detection")
|
408 |
-
|
409 |
-
# # Upload image
|
410 |
-
# uploaded_file = st.file_uploader("Choose a watermelon image...", type=["jpg", "jpeg", "png"])
|
411 |
-
|
412 |
-
# if uploaded_file is not None:
|
413 |
-
# try:
|
414 |
-
# # Load the image
|
415 |
-
# img = image.load_img(uploaded_file, target_size=(224, 224))
|
416 |
-
# img_array = image.img_to_array(img)
|
417 |
-
# img_array = np.expand_dims(img_array, axis=0)
|
418 |
-
# img_array /= 255.0
|
419 |
-
|
420 |
-
# # Display uploaded image
|
421 |
-
# st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
422 |
-
|
423 |
-
# # Predict watermelon name
|
424 |
-
# pred_name = model_name.predict(img_array)
|
425 |
-
# predicted_name = 'Watermelon'
|
426 |
-
|
427 |
-
# # Predict watermelon quality
|
428 |
-
# pred_quality = model_quality.predict(img_array)
|
429 |
-
# predicted_class_quality = np.argmax(pred_quality, axis=1)
|
430 |
-
|
431 |
-
# # Define labels for watermelon quality
|
432 |
-
# label_map_quality = {
|
433 |
-
# 0: "Good",
|
434 |
-
# 1: "Mild",
|
435 |
-
# 2: "Rotten"
|
436 |
-
# }
|
437 |
-
|
438 |
-
# predicted_quality = label_map_quality[predicted_class_quality[0]]
|
439 |
-
|
440 |
-
# # Display predictions
|
441 |
-
# st.write(f"Fruit Type Detection: {predicted_name}")
|
442 |
-
# st.write(f"Fruit Quality Classification: {predicted_quality}")
|
443 |
-
|
444 |
-
# # If the quality is 'Mild' or 'Rotten', pass the image to the mask detection model
|
445 |
-
# if predicted_quality in ["Mild", "Rotten"]:
|
446 |
-
# st.write("Passing the image to the mask detection model for damage detection...")
|
447 |
-
|
448 |
-
# # Load the image again for the mask detection (Detectron2 requires the original image)
|
449 |
-
# im = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
|
450 |
-
|
451 |
-
# # Run prediction on the image
|
452 |
-
# outputs = predictor(im)
|
453 |
-
|
454 |
-
# # Visualize the predictions
|
455 |
-
# v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=0.8)
|
456 |
-
# out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
457 |
-
|
458 |
-
# # Display the output
|
459 |
-
# st.image(out.get_image()[:, :, ::-1], caption="Detected Damage", use_column_width=True)
|
460 |
-
|
461 |
-
# except Exception as e:
|
462 |
-
# st.error(f"An error occurred during processing: {str(e)}")
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
# import streamlit as st
|
487 |
-
# import numpy as np
|
488 |
-
# import cv2
|
489 |
-
# import torch
|
490 |
-
# from PIL import Image
|
491 |
-
# from tensorflow.keras.models import load_model
|
492 |
-
# from tensorflow.keras.preprocessing import image
|
493 |
-
# from detectron2.engine import DefaultPredictor
|
494 |
-
# from detectron2.config import get_cfg
|
495 |
-
# from detectron2.utils.visualizer import Visualizer
|
496 |
-
# from detectron2.data import MetadataCatalog
|
497 |
-
|
498 |
-
# # Suppress warnings
|
499 |
-
# import warnings
|
500 |
-
# import tensorflow as tf
|
501 |
-
# warnings.filterwarnings("ignore")
|
502 |
-
# tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
|
503 |
-
|
504 |
-
# @st.cache_resource
|
505 |
-
# def load_models():
|
506 |
-
# model_name = load_model('name_model_inception.h5')
|
507 |
-
# model_quality = load_model('type_model_inception.h5')
|
508 |
-
# return model_name, model_quality
|
509 |
-
|
510 |
-
# model_name, model_quality = load_models()
|
511 |
-
|
512 |
-
# # Detectron2 setup
|
513 |
-
# @st.cache_resource
|
514 |
-
# def load_detectron_model(fruit_name):
|
515 |
-
# cfg = get_cfg()
|
516 |
-
# cfg.merge_from_file(f"{fruit_name.lower()}.yaml")
|
517 |
-
# cfg.MODEL.WEIGHTS = f"{fruit_name.lower()}_model.pth"
|
518 |
-
# cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
519 |
-
# cfg.MODEL.DEVICE = 'cpu'
|
520 |
-
# predictor = DefaultPredictor(cfg)
|
521 |
-
# return predictor, cfg
|
522 |
-
|
523 |
-
# # Labels
|
524 |
-
# label_map_name = {
|
525 |
-
# 0: "Banana", 1: "Cucumber", 2: "Grape", 3: "Kaki", 4: "Papaya",
|
526 |
-
# 5: "Peach", 6: "Pear", 7: "Pepper", 8: "Strawberry", 9: "Watermelon",
|
527 |
-
# 10: "Tomato"
|
528 |
-
# }
|
529 |
-
# label_map_quality = {0: "Good", 1: "Mild", 2: "Rotten"}
|
530 |
-
|
531 |
-
# def predict_fruit(img):
|
532 |
-
# # Preprocess image
|
533 |
-
# img = Image.fromarray(img.astype('uint8'), 'RGB')
|
534 |
-
# img = img.resize((224, 224))
|
535 |
-
# x = image.img_to_array(img)
|
536 |
-
# x = np.expand_dims(x, axis=0)
|
537 |
-
# x = x / 255.0
|
538 |
-
|
539 |
-
# # Predict
|
540 |
-
# pred_name = model_name.predict(x)
|
541 |
-
# pred_quality = model_quality.predict(x)
|
542 |
-
|
543 |
-
# predicted_name = label_map_name[np.argmax(pred_name, axis=1)[0]]
|
544 |
-
# predicted_quality = label_map_quality[np.argmax(pred_quality, axis=1)[0]]
|
545 |
-
|
546 |
-
# return predicted_name, predicted_quality, img
|
547 |
-
|
548 |
-
# def main():
|
549 |
-
# st.title("Fruit Quality and Damage Detection")
|
550 |
-
# st.write("Upload an image of a fruit to detect its type, quality, and potential damage.")
|
551 |
-
|
552 |
-
# uploaded_file = st.file_uploader("Choose a fruit image...", type=["jpg", "jpeg", "png"])
|
553 |
-
|
554 |
-
# if uploaded_file is not None:
|
555 |
-
# image = Image.open(uploaded_file)
|
556 |
-
# st.image(image, caption="Uploaded Image", use_column_width=True)
|
557 |
-
|
558 |
-
# if st.button("Analyze"):
|
559 |
-
# predicted_name, predicted_quality, img = predict_fruit(np.array(image))
|
560 |
-
|
561 |
-
# st.write(f"Fruit Type: {predicted_name}")
|
562 |
-
# st.write(f"Fruit Quality: {predicted_quality}")
|
563 |
-
|
564 |
-
# if predicted_name.lower() in ["kaki", "tomato", "strawberry", "pepper", "pear", "peach", "papaya", "watermelon", "grape", "banana", "cucumber"] and predicted_quality in ["Mild", "Rotten"]:
|
565 |
-
# st.write("Detecting damage...")
|
566 |
-
# predictor, cfg = load_detectron_model(predicted_name)
|
567 |
-
# outputs = predictor(cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))
|
568 |
-
# v = Visualizer(np.array(img), MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=0.8)
|
569 |
-
# out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
570 |
-
# st.image(out.get_image(), caption="Damage Detection Result", use_column_width=True)
|
571 |
-
# else:
|
572 |
-
# st.write("No damage detection performed for this fruit or quality level.")
|
573 |
-
|
574 |
-
# if __name__ == "__main__":
|
575 |
-
# main()
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
|
587 |
import streamlit as st
|
588 |
import numpy as np
|
@@ -628,7 +73,7 @@ def load_detectron_model(fruit_name):
|
|
628 |
label_map_name = {
|
629 |
0: "Banana", 1: "Cucumber", 2: "Grape", 3: "Kaki", 4: "Papaya",
|
630 |
5: "Peach", 6: "Pear", 7: "Pepper", 8: "Strawberry", 9: "Watermelon",
|
631 |
-
10: "
|
632 |
}
|
633 |
label_map_quality = {0: "Good", 1: "Mild", 2: "Rotten"}
|
634 |
|
@@ -650,7 +95,7 @@ def predict_fruit(img):
|
|
650 |
return predicted_name, predicted_quality, img
|
651 |
|
652 |
def main():
|
653 |
-
st.title("
|
654 |
st.write("Upload an image of a fruit to detect its type, quality, and potential damage.")
|
655 |
|
656 |
uploaded_file = st.file_uploader("Choose a fruit image...", type=["jpg", "jpeg", "png"])
|
@@ -662,11 +107,11 @@ def main():
|
|
662 |
if st.button("Analyze"):
|
663 |
predicted_name, predicted_quality, img = predict_fruit(np.array(image))
|
664 |
|
665 |
-
st.write(f"
|
666 |
-
st.write(f"
|
667 |
|
668 |
if predicted_name.lower() in ["kaki", "tomato", "strawberry", "pepper", "pear", "peach", "papaya", "watermelon", "grape", "banana", "cucumber"] and predicted_quality in ["Mild", "Rotten"]:
|
669 |
-
st.write("
|
670 |
try:
|
671 |
predictor, cfg = load_detectron_model(predicted_name)
|
672 |
outputs = predictor(cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
import streamlit as st
|
33 |
import numpy as np
|
|
|
73 |
label_map_name = {
|
74 |
0: "Banana", 1: "Cucumber", 2: "Grape", 3: "Kaki", 4: "Papaya",
|
75 |
5: "Peach", 6: "Pear", 7: "Pepper", 8: "Strawberry", 9: "Watermelon",
|
76 |
+
10: "tomato"
|
77 |
}
|
78 |
label_map_quality = {0: "Good", 1: "Mild", 2: "Rotten"}
|
79 |
|
|
|
95 |
return predicted_name, predicted_quality, img
|
96 |
|
97 |
def main():
|
98 |
+
st.title("Automated Fruits Monitoring System")
|
99 |
st.write("Upload an image of a fruit to detect its type, quality, and potential damage.")
|
100 |
|
101 |
uploaded_file = st.file_uploader("Choose a fruit image...", type=["jpg", "jpeg", "png"])
|
|
|
107 |
if st.button("Analyze"):
|
108 |
predicted_name, predicted_quality, img = predict_fruit(np.array(image))
|
109 |
|
110 |
+
st.write(f"Fruits Type Detection: {predicted_name}")
|
111 |
+
st.write(f"Fruits Quality Classification: {predicted_quality}")
|
112 |
|
113 |
if predicted_name.lower() in ["kaki", "tomato", "strawberry", "pepper", "pear", "peach", "papaya", "watermelon", "grape", "banana", "cucumber"] and predicted_quality in ["Mild", "Rotten"]:
|
114 |
+
st.write("Segmentation of Defective Region:")
|
115 |
try:
|
116 |
predictor, cfg = load_detectron_model(predicted_name)
|
117 |
outputs = predictor(cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))
|