Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -120,7 +120,12 @@ def predict(image):
|
|
| 120 |
Make prediction on input image.
|
| 121 |
"""
|
| 122 |
if model is None:
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
try:
|
| 126 |
# Preprocess image
|
|
@@ -132,19 +137,22 @@ def predict(image):
|
|
| 132 |
# Get probabilities for each class
|
| 133 |
probabilities = predictions[0]
|
| 134 |
|
| 135 |
-
# Create result dictionary
|
| 136 |
-
results = {
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
|
| 144 |
return results
|
| 145 |
|
| 146 |
except Exception as e:
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
|
| 149 |
|
| 150 |
# -----------------------------
|
|
|
|
| 120 |
Make prediction on input image.
|
| 121 |
"""
|
| 122 |
if model is None:
|
| 123 |
+
# Return zero confidence for all classes when model not loaded
|
| 124 |
+
return {class_name: 0.0 for class_name in class_names}
|
| 125 |
+
|
| 126 |
+
if image is None:
|
| 127 |
+
# Return zero confidence for all classes when no image provided
|
| 128 |
+
return {class_name: 0.0 for class_name in class_names}
|
| 129 |
|
| 130 |
try:
|
| 131 |
# Preprocess image
|
|
|
|
| 137 |
# Get probabilities for each class
|
| 138 |
probabilities = predictions[0]
|
| 139 |
|
| 140 |
+
# Create result dictionary with validated float values
|
| 141 |
+
results = {}
|
| 142 |
+
for i in range(len(class_names)):
|
| 143 |
+
prob = probabilities[i]
|
| 144 |
+
# Ensure the probability is a valid number
|
| 145 |
+
if prob is None or (isinstance(prob, float) and (np.isnan(prob) or np.isinf(prob))):
|
| 146 |
+
results[class_names[i]] = 0.0
|
| 147 |
+
else:
|
| 148 |
+
results[class_names[i]] = float(prob)
|
| 149 |
|
| 150 |
return results
|
| 151 |
|
| 152 |
except Exception as e:
|
| 153 |
+
print(f"Prediction error: {e}")
|
| 154 |
+
# Return zero confidence for all classes on error
|
| 155 |
+
return {class_name: 0.0 for class_name in class_names}
|
| 156 |
|
| 157 |
|
| 158 |
# -----------------------------
|