mefet commited on
Commit
5d3744e
·
verified ·
1 Parent(s): 293ead4

Create Sapp.py

Browse files
Files changed (1) hide show
  1. Sapp.py +76 -0
Sapp.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ from cal import load_model, predict_image, calculate_calories
6
+
7
+ # Load the model
8
+ model = load_model()
9
+
10
+ # Set up the sidebar
11
+ st.sidebar.title("Green Food Calorie Detector")
12
+ st.sidebar.write("Upload an image or use your camera to take a picture.")
13
+
14
+ option = st.sidebar.selectbox(
15
+ 'How would you like to provide the image?',
16
+ ('Upload an image', 'Use camera')
17
+ )
18
+
19
+ image_path = None
20
+ if option == 'Upload an image':
21
+ uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png", "webp"])
22
+ if uploaded_file is not None:
23
+ image = Image.open(uploaded_file)
24
+ if image.mode == 'RGBA':
25
+ image = image.convert('RGB')
26
+ image_path = "uploaded_image.jpg"
27
+ image.save(image_path)
28
+ elif option == 'Use camera':
29
+ camera_image = st.sidebar.camera_input("Take a picture")
30
+ if camera_image is not None:
31
+ image = Image.open(camera_image)
32
+ if image.mode == 'RGBA':
33
+ image = image.convert('RGB')
34
+ image_path = "camera_image.jpg"
35
+ image.save(image_path)
36
+
37
+ if image_path:
38
+ # Display the image and classification results in columns
39
+ col1, col2 = st.columns(2)
40
+
41
+ with col1:
42
+ st.image(image, caption='Captured Image.', use_column_width=True)
43
+ st.write("")
44
+ st.write("Classifying...")
45
+
46
+ # Predict the image
47
+ image_with_boxes, detection_details = predict_image(image_path, model)
48
+
49
+ with col2:
50
+ # Display the image with bounding boxes and labels
51
+ st.image(image_with_boxes, caption='Processed Image.', use_column_width=True)
52
+
53
+ # Calculate and display detected items and their calories
54
+ detected_items = calculate_calories(detection_details)
55
+ st.markdown("<h3>Detection Results:</h3>", unsafe_allow_html=True)
56
+ for item, calories, confidence in detected_items:
57
+ st.markdown(f"<p style='font-size:18px;'>✓ Detected {item} ({calories} cal/100g) - Confidence: {confidence:.2%}</p>", unsafe_allow_html=True)
58
+
59
+ # Footer
60
+ st.markdown("""
61
+ <style>
62
+ .footer {
63
+ position: fixed;
64
+ left: 0;
65
+ bottom: 0;
66
+ width: 100%;
67
+ background-color: #f1f1f1;
68
+ color: black;
69
+ text-align: center;
70
+ padding: 10px;
71
+ }
72
+ </style>
73
+ <div class="footer">
74
+ <p>Green Food Calorie Detector © 2023</p>
75
+ </div>
76
+ """, unsafe_allow_html=True)