Norphel commited on
Commit
e6d8d9e
Β·
verified Β·
1 Parent(s): af6ea8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -3,6 +3,8 @@ from ultralytics import YOLO
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,7 +37,7 @@ foods = {
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,10 +48,10 @@ def calculate_bounding_box_area(results):
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,10 +77,10 @@ def run_yolo(image):
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,6 +94,7 @@ if uploaded_file:
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)
 
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
  }
38
  }
39
 
40
+ # Function to calculate bounding box area
41
  def calculate_bounding_box_area(results):
42
  areas = []
43
  class_names = []
 
48
  area = (x2 - x1) * (y2 - y1) # Calculate area
49
  areas.append(area)
50
 
51
+ # Get the class ID for the current bounding box
52
+ class_ids = box.cls.tolist() # This returns a list of class IDs
53
 
54
+ # Convert class ID(s) to class name(s)
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
 
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(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)) # Convert back to PIL
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
  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)