Thirupathi986 commited on
Commit
498e2af
Β·
verified Β·
1 Parent(s): 8aa4b0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -62
app.py CHANGED
@@ -23,64 +23,67 @@ def load_model():
23
 
24
  model = load_model()
25
 
26
- # --- PAGE SELECTION BUTTONS (visible on mobile too) ---
27
  st.title("🍽️ NutriDetect: AI Nutrition Companion")
28
- page = st.radio("Navigate to", ["Home", "Try It", "About"], horizontal=True)
29
-
30
- # --- HOME PAGE ---
31
- if page == "Home":
32
- st.image("back_image.png", width=800)
33
- st.markdown("""
34
- Welcome to **NutriDetect** – detect food items from images using a custom-trained YOLOv8 model.
35
-
36
- - πŸ“· Upload food images
37
- - 🧠 Detect with YOLO
38
- - ⚑ Built for mobile and real-time use
39
- """)
40
-
41
- # --- TRY IT PAGE ---
42
- elif page == "Try It":
43
- st.header("πŸš€ Try Food Detection")
44
- uploaded_file = st.file_uploader("Upload a food image", type=["jpg", "jpeg", "png"])
45
-
46
- if uploaded_file:
47
- img_id = str(uuid.uuid4()) + ".jpg"
48
- image_path = os.path.join(TEMP_IMAGE_DIR, img_id)
49
- with open(image_path, "wb") as f:
50
- f.write(uploaded_file.read())
51
- st.image(image_path, caption="Uploaded Image", width=400)
52
-
53
- if st.button("πŸ” Detect Food"):
54
- with st.spinner("Detecting..."):
55
- results = model.predict(source=image_path, save=True, save_txt=False)
56
-
57
- # Find latest prediction folder
58
- detect_dir = "runs/detect"
59
- all_preds = sorted(os.listdir(detect_dir), key=lambda x: os.path.getmtime(os.path.join(detect_dir, x)))
60
- latest_run = os.path.join(detect_dir, all_preds[-1])
61
- pred_image_path = os.path.join(latest_run, img_id)
62
-
63
- if os.path.exists(pred_image_path):
64
- st.image(pred_image_path, caption="Detection Result", width=800)
65
- else:
66
- st.warning("Prediction image not found.")
67
-
68
- boxes = results[0].boxes
69
- names = model.names
70
- st.subheader("πŸ“‹ Detected Items")
71
- if len(boxes) == 0:
72
- st.warning("No objects detected.")
73
- else:
74
- for box in boxes:
75
- class_id = int(box.cls)
76
- conf = float(box.conf)
77
- label = names[class_id]
78
- st.markdown(f"- **{label}** (Confidence: {conf:.2f})")
79
-
80
- # --- ABOUT PAGE ---
81
- elif page == "About":
82
- st.header("πŸ“– About NutriDetect")
83
- st.markdown("""
 
 
 
84
  **NutriDetect** is an AI-powered food recognition system developed to simplify dietary assessment and support healthier eating habits.
85
 
86
  This project was accepted at the **ACM Conference**, showcasing innovation in AI-powered nutritional informatics.
@@ -95,17 +98,17 @@ NutriDetect uses the YOLOv8 object detection model trained on over 55,000 curate
95
 
96
  ### 🧠 Why It Matters
97
 
98
- - Enables quick and accurate food recognition
99
- - Designed for mobile, AR, and wearable integration
100
  - Built with a culturally diverse dataset for global usage
101
 
102
  ---
103
 
104
  ### πŸ”¬ Research Highlights
105
 
106
- - mAP@50 of **75.9%**
107
- - Precision of **72.4%**
108
- - Trained on NVIDIA RTX 4060 (8GB VRAM)
109
  - Dataset curated from UECFOOD256, OID, and more
110
 
111
  *NutriDetect is a step toward smarter, AI-powered food logging and healthcare.*
 
23
 
24
  model = load_model()
25
 
26
+ # --- LAYOUT ---
27
  st.title("🍽️ NutriDetect: AI Nutrition Companion")
28
+
29
+ col_nav, col_main = st.columns([1, 5]) # Navigation on the left
30
+
31
+ with col_nav:
32
+ st.markdown("### πŸ”Ž Menu")
33
+ page = st.radio("", ["Home", "Try It", "About"])
34
+
35
+ with col_main:
36
+ if page == "Home":
37
+ st.image("back_image.png", width=800)
38
+ st.markdown("""
39
+ Welcome to **NutriDetect** – detect food items from images using a custom-trained YOLOv8 model.
40
+
41
+ - πŸ“· Upload food images
42
+ - 🧠 Detect with YOLO
43
+ - ⚑ Built for mobile and real-time use
44
+ """)
45
+
46
+ elif page == "Try It":
47
+ st.header("πŸš€ Try Food Detection")
48
+ uploaded_file = st.file_uploader("Upload a food image", type=["jpg", "jpeg", "png"])
49
+
50
+ if uploaded_file:
51
+ img_id = str(uuid.uuid4()) + ".jpg"
52
+ image_path = os.path.join(TEMP_IMAGE_DIR, img_id)
53
+ with open(image_path, "wb") as f:
54
+ f.write(uploaded_file.read())
55
+ st.image(image_path, caption="Uploaded Image", width=400)
56
+
57
+ if st.button("πŸ” Detect Food"):
58
+ with st.spinner("Detecting..."):
59
+ results = model.predict(source=image_path, save=True, save_txt=False)
60
+
61
+ # Find the latest result image
62
+ detect_dir = "runs/detect"
63
+ all_preds = sorted(os.listdir(detect_dir), key=lambda x: os.path.getmtime(os.path.join(detect_dir, x)))
64
+ latest_run = os.path.join(detect_dir, all_preds[-1])
65
+ pred_image_path = os.path.join(latest_run, img_id)
66
+
67
+ if os.path.exists(pred_image_path):
68
+ st.image(pred_image_path, caption="Detection Result", width=800)
69
+ else:
70
+ st.warning("Prediction image not found.")
71
+
72
+ boxes = results[0].boxes
73
+ names = model.names
74
+ st.subheader("πŸ“‹ Detected Items")
75
+ if len(boxes) == 0:
76
+ st.warning("No objects detected.")
77
+ else:
78
+ for box in boxes:
79
+ class_id = int(box.cls)
80
+ conf = float(box.conf)
81
+ label = names[class_id]
82
+ st.markdown(f"- **{label}** (Confidence: {conf:.2f})")
83
+
84
+ elif page == "About":
85
+ st.header("πŸ“– About NutriDetect")
86
+ st.markdown("""
87
  **NutriDetect** is an AI-powered food recognition system developed to simplify dietary assessment and support healthier eating habits.
88
 
89
  This project was accepted at the **ACM Conference**, showcasing innovation in AI-powered nutritional informatics.
 
98
 
99
  ### 🧠 Why It Matters
100
 
101
+ - Enables quick and accurate food recognition
102
+ - Designed for mobile, AR, and wearable integration
103
  - Built with a culturally diverse dataset for global usage
104
 
105
  ---
106
 
107
  ### πŸ”¬ Research Highlights
108
 
109
+ - mAP@50 of **75.9%**
110
+ - Precision of **72.4%**
111
+ - Trained on NVIDIA RTX 4060 (8GB VRAM)
112
  - Dataset curated from UECFOOD256, OID, and more
113
 
114
  *NutriDetect is a step toward smarter, AI-powered food logging and healthcare.*