| import streamlit as st |
| from ultralytics import YOLO |
| from huggingface_hub import hf_hub_download |
| from PIL import Image |
| import io |
| import cv2 |
| import numpy as np |
|
|
| |
| repo_id = "Norphel/nutri-ai-n2" |
| model_file = "best.pt" |
| model_path = hf_hub_download(repo_id=repo_id, filename=model_file) |
| model = YOLO(model_path) |
|
|
| |
| foods = { |
| "kewadatsi": { |
| "calories": 400, |
| "protein": "10g", |
| "fat": "0.4g", |
| "carbohydrates": "92g", |
| "sodium": "20mg" |
| }, |
| "emadatsi": { |
| "calories": 275, |
| "protein": "10g", |
| "fat": "0.4g", |
| "carbohydrates": "92g", |
| "sodium": "850mg" |
| }, |
| "rice": { |
| "calories": 130, |
| "protein": "2.7g", |
| "fat": "0.3g", |
| "carbohydrates": "28g", |
| "sodium": "1mg" |
| } |
| } |
|
|
| |
| def calculate_bounding_box_area(results): |
| areas = [] |
| class_names = [] |
|
|
| |
| for box in results[0].boxes: |
| x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) |
| area = (x2 - x1) * (y2 - y1) |
| areas.append(area) |
| |
| |
| class_ids = box.cls.tolist() |
| print(class_ids) |
| |
| for class_id in class_ids: |
| class_name = model.names[int(class_id)] |
| class_names.append(class_name) |
|
|
| return class_names, areas |
|
|
|
|
| |
| def process_image(uploaded_file): |
| if uploaded_file is not None: |
| image = Image.open(uploaded_file) |
| image = image.resize((640, 640)) |
|
|
| return image |
| return None |
|
|
|
|
| |
| def run_yolo(image): |
| results = model(image) |
|
|
| detected_classes, bounding_areas = calculate_bounding_box_area(results) |
|
|
| |
| result_img = results[0].plot() |
| result_pil = Image.fromarray(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)) |
| print(detected_classes, bounding_areas) |
| return result_pil, detected_classes, bounding_areas |
|
|
| |
| st.title("Nutri-AI") |
|
|
| uploaded_file = st.file_uploader("Upload an image (PNG, JPG, JPEG)", type=["png", "jpg", "jpeg"]) |
|
|
| if uploaded_file: |
| resized_image = process_image(uploaded_file) |
|
|
| if resized_image: |
| st.image(resized_image, caption="Resized Image (640x640)", use_container_width=True) |
| st.write("🔍 Running YOLOv8 detection...") |
|
|
| detected_image, detected_classes, bounding_areas = run_yolo(resized_image) |
| print(detected_classes, bounding_areas) |
|
|
| |
| st.image(detected_image, caption="Detected Objects", use_container_width=True) |
|
|
| |
| for i, class_name in enumerate(detected_classes): |
| st.subheader(f"🍲 Detected: {class_name.capitalize()}") |
| st.write(f"🟡 **Bounding Box Area:** {bounding_areas[i]:,.2f} pixels²") |
|
|
| if class_name in foods: |
| st.write("📊 **Nutritional Information:**") |
| for key, value in foods[class_name].items(): |
| st.write(f"🔹 {key.capitalize()}: {value}") |
|
|