Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -103,22 +103,33 @@ except Exception as e:
|
|
103 |
|
104 |
def classify_food(image):
|
105 |
"""Classify food image using the pre-trained model"""
|
|
|
106 |
try:
|
107 |
-
if model_loaded:
|
108 |
-
|
109 |
-
image = Image.fromarray(image)
|
110 |
-
image = processor(images=image, return_tensors="pt")
|
111 |
-
with torch.no_grad():
|
112 |
-
outputs = model(**image)
|
113 |
-
predicted_idx = torch.argmax(outputs.logits, dim=-1).item()
|
114 |
-
food_name = model.config.id2label.get(predicted_idx, "Unknown Food")
|
115 |
-
return food_name.lower() #Convert classification to lowercase
|
116 |
-
else:
|
117 |
return "Unknown"
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
except Exception as e:
|
120 |
-
print("Classify food error
|
121 |
-
return "Unknown"
|
122 |
|
123 |
# -------------------------------------------------
|
124 |
# USDA API Integration - REMOVED for local HF Spaces deployment
|
|
|
103 |
|
104 |
def classify_food(image):
|
105 |
"""Classify food image using the pre-trained model"""
|
106 |
+
print("classify_food function called") # Check if this function is even called
|
107 |
try:
|
108 |
+
if not model_loaded:
|
109 |
+
print("Model not loaded, returning 'Unknown'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
return "Unknown"
|
111 |
|
112 |
+
print(f"Image type: {type(image)}") # Check the type of the image
|
113 |
+
|
114 |
+
if isinstance(image, np.ndarray):
|
115 |
+
print("Image is a numpy array, converting to PIL Image")
|
116 |
+
image = Image.fromarray(image)
|
117 |
+
|
118 |
+
print(f"Image mode: {image.mode}") # Check image mode (e.g., RGB, L)
|
119 |
+
|
120 |
+
image = processor(images=image, return_tensors="pt")
|
121 |
+
print(f"Processed image: {image}") # Print the output of the processor
|
122 |
+
|
123 |
+
with torch.no_grad():
|
124 |
+
outputs = model(**image)
|
125 |
+
predicted_idx = torch.argmax(outputs.logits, dim=-1).item()
|
126 |
+
food_name = model.config.id2label.get(predicted_idx, "Unknown Food")
|
127 |
+
print(f"Predicted food name: {food_name}") # Print the predicted food name
|
128 |
+
return food_name.lower() # Convert classification to lowercase
|
129 |
+
|
130 |
except Exception as e:
|
131 |
+
print(f"Classify food error: {e}") # Print the full error message
|
132 |
+
return "Unknown" # If an exception arises make sure to create a default case
|
133 |
|
134 |
# -------------------------------------------------
|
135 |
# USDA API Integration - REMOVED for local HF Spaces deployment
|