pyesonekyaw commited on
Commit
bd7d1bb
1 Parent(s): 7fc0592

added show_error=True

Browse files
Files changed (1) hide show
  1. app.py +42 -39
app.py CHANGED
@@ -77,45 +77,48 @@ def predict_image(inp):
77
  """
78
  Performs inference for a given input image and returns the prediction and CAM image.
79
  """
80
- material_label, material_label_idx, material_probs = materials_model.predict(inp)
81
- material_preds = {name: prob for name, prob in zip(material_names, material_probs.tolist())}
82
-
83
- if material_label == 'paper':
84
- specific_label, specific_label_idx, specific_probs = paper_model.predict(inp)
85
- specific_preds = {name: prob for name, prob in zip(paper_names, specific_probs.tolist())}
86
- specific_label = paper_names[int(specific_label_idx)]
87
- recyclable_qn = paper_item_num_dict[specific_label][0]
88
- recyclable_advice = paper_item_num_dict[specific_label][1]
89
-
90
- elif material_label == 'plastic':
91
- specific_label, specific_label_idx, specific_probs = plastic_model.predict(inp)
92
- specific_preds = {name: prob for name, prob in zip(plastic_names, specific_probs.tolist())}
93
- specific_label = plastic_names[int(specific_label_idx)]
94
- recyclable_qn = plastic_item_num_dict[specific_label][0]
95
- recyclable_advice = plastic_item_num_dict[specific_label][1]
96
-
97
- elif material_label == 'glass':
98
- specific_label, specific_label_idx, specific_probs = glass_model.predict(inp)
99
- specific_preds = {name: prob for name, prob in zip(glass_names, specific_probs.tolist())}
100
- specific_label = glass_names[int(specific_label_idx)]
101
- recyclable_qn = glass_item_num_dict[specific_label][0]
102
- recyclable_advice = glass_item_num_dict[specific_label][1]
103
-
104
- elif material_label == 'metal':
105
- specific_label, specific_label_idx, specific_probs = metal_model.predict(inp)
106
- specific_preds = {name: prob for name, prob in zip(metal_names, specific_probs.tolist())}
107
- specific_label = metal_names[int(specific_label_idx)]
108
- recyclable_qn = metal_item_num_dict[specific_label][0]
109
- recyclable_advice = metal_item_num_dict[specific_label][1]
110
-
111
- elif material_label == 'others':
112
- specific_label, specific_label_idx, specific_probs = others_model.predict(inp)
113
- specific_preds = {name: prob for name, prob in zip(other_names, specific_probs.tolist())}
114
- specific_label = other_names[int(specific_label_idx)]
115
- recyclable_qn = others_item_num_dict[specific_label][0]
116
- recyclable_advice = others_item_num_dict[specific_label][1]
117
-
118
- return material_preds, specific_preds, recyclable_qn, recyclable_advice
 
 
 
119
 
120
 
121
  with gr.Blocks(title="Trash Classification", css="#custom_header {min-height: 3rem} #custom_title {min-height: 3rem; text-align: center}") as demo:
 
77
  """
78
  Performs inference for a given input image and returns the prediction and CAM image.
79
  """
80
+ try:
81
+ material_label, material_label_idx, material_probs = materials_model.predict(inp)
82
+ material_preds = {name: prob for name, prob in zip(material_names, material_probs.tolist())}
83
+
84
+ if material_label == 'paper':
85
+ specific_label, specific_label_idx, specific_probs = paper_model.predict(inp)
86
+ specific_preds = {name: prob for name, prob in zip(paper_names, specific_probs.tolist())}
87
+ specific_label = paper_names[int(specific_label_idx)]
88
+ recyclable_qn = paper_item_num_dict[specific_label][0]
89
+ recyclable_advice = paper_item_num_dict[specific_label][1]
90
+
91
+ elif material_label == 'plastic':
92
+ specific_label, specific_label_idx, specific_probs = plastic_model.predict(inp)
93
+ specific_preds = {name: prob for name, prob in zip(plastic_names, specific_probs.tolist())}
94
+ specific_label = plastic_names[int(specific_label_idx)]
95
+ recyclable_qn = plastic_item_num_dict[specific_label][0]
96
+ recyclable_advice = plastic_item_num_dict[specific_label][1]
97
+
98
+ elif material_label == 'glass':
99
+ specific_label, specific_label_idx, specific_probs = glass_model.predict(inp)
100
+ specific_preds = {name: prob for name, prob in zip(glass_names, specific_probs.tolist())}
101
+ specific_label = glass_names[int(specific_label_idx)]
102
+ recyclable_qn = glass_item_num_dict[specific_label][0]
103
+ recyclable_advice = glass_item_num_dict[specific_label][1]
104
+
105
+ elif material_label == 'metal':
106
+ specific_label, specific_label_idx, specific_probs = metal_model.predict(inp)
107
+ specific_preds = {name: prob for name, prob in zip(metal_names, specific_probs.tolist())}
108
+ specific_label = metal_names[int(specific_label_idx)]
109
+ recyclable_qn = metal_item_num_dict[specific_label][0]
110
+ recyclable_advice = metal_item_num_dict[specific_label][1]
111
+
112
+ elif material_label == 'others':
113
+ specific_label, specific_label_idx, specific_probs = others_model.predict(inp)
114
+ specific_preds = {name: prob for name, prob in zip(other_names, specific_probs.tolist())}
115
+ specific_label = other_names[int(specific_label_idx)]
116
+ recyclable_qn = others_item_num_dict[specific_label][0]
117
+ recyclable_advice = others_item_num_dict[specific_label][1]
118
+
119
+ return material_preds, specific_preds, recyclable_qn, recyclable_advice
120
+ except:
121
+ raise Exception("Invalid file format! Please only upload .jpg or .png files!")
122
 
123
 
124
  with gr.Blocks(title="Trash Classification", css="#custom_header {min-height: 3rem} #custom_title {min-height: 3rem; text-align: center}") as demo: