Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,6 @@ from ultralytics import YOLO
|
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
-
import cv2
|
7 |
-
import numpy as np
|
8 |
|
9 |
# Load YOLOv8 model from Hugging Face
|
10 |
repo_id = "Norphel/nutri-ai-n2" # Replace with your Hugging Face model repo
|
@@ -37,7 +35,7 @@ foods = {
|
|
37 |
}
|
38 |
}
|
39 |
|
40 |
-
# Function to calculate bounding box area
|
41 |
def calculate_bounding_box_area(results):
|
42 |
areas = []
|
43 |
class_names = []
|
@@ -48,10 +46,10 @@ def calculate_bounding_box_area(results):
|
|
48 |
area = (x2 - x1) * (y2 - y1) # Calculate area
|
49 |
areas.append(area)
|
50 |
|
51 |
-
# Get
|
52 |
-
class_ids = box.cls.tolist() # This
|
53 |
|
54 |
-
#
|
55 |
for class_id in class_ids:
|
56 |
class_name = model.names[int(class_id)] # Convert class ID to class name
|
57 |
class_names.append(class_name) # Add class name to list
|
@@ -77,10 +75,10 @@ def run_yolo(image):
|
|
77 |
|
78 |
# Draw bounding boxes on the image and return the processed image
|
79 |
result_img = results[0].plot() # Draw bounding boxes on the image
|
80 |
-
result_pil = Image.fromarray(
|
81 |
-
print(detected_classes, bounding_areas)
|
82 |
return result_pil, detected_classes, bounding_areas
|
83 |
|
|
|
84 |
# Streamlit UI
|
85 |
st.title("Nutri-AI")
|
86 |
|
@@ -94,7 +92,6 @@ if uploaded_file:
|
|
94 |
st.write("π Running YOLOv8 detection...")
|
95 |
|
96 |
detected_image, detected_classes, bounding_areas = run_yolo(resized_image)
|
97 |
-
print(detected_classes, bounding_areas)
|
98 |
|
99 |
# Display detected image
|
100 |
st.image(detected_image, caption="Detected Objects", use_container_width=True)
|
|
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
from PIL import Image
|
5 |
import io
|
|
|
|
|
6 |
|
7 |
# Load YOLOv8 model from Hugging Face
|
8 |
repo_id = "Norphel/nutri-ai-n2" # Replace with your Hugging Face model repo
|
|
|
35 |
}
|
36 |
}
|
37 |
|
38 |
+
# Function to calculate bounding box area and class names
|
39 |
def calculate_bounding_box_area(results):
|
40 |
areas = []
|
41 |
class_names = []
|
|
|
46 |
area = (x2 - x1) * (y2 - y1) # Calculate area
|
47 |
areas.append(area)
|
48 |
|
49 |
+
# Get class IDs for the current bounding box (it can have multiple class IDs)
|
50 |
+
class_ids = box.cls.tolist() # This may be a list if multiple classes detected
|
51 |
|
52 |
+
# Add all detected class names for this box
|
53 |
for class_id in class_ids:
|
54 |
class_name = model.names[int(class_id)] # Convert class ID to class name
|
55 |
class_names.append(class_name) # Add class name to list
|
|
|
75 |
|
76 |
# Draw bounding boxes on the image and return the processed image
|
77 |
result_img = results[0].plot() # Draw bounding boxes on the image
|
78 |
+
result_pil = Image.fromarray(result_img) # Convert back to PIL
|
|
|
79 |
return result_pil, detected_classes, bounding_areas
|
80 |
|
81 |
+
|
82 |
# Streamlit UI
|
83 |
st.title("Nutri-AI")
|
84 |
|
|
|
92 |
st.write("π Running YOLOv8 detection...")
|
93 |
|
94 |
detected_image, detected_classes, bounding_areas = run_yolo(resized_image)
|
|
|
95 |
|
96 |
# Display detected image
|
97 |
st.image(detected_image, caption="Detected Objects", use_container_width=True)
|