Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,12 +9,8 @@ from io import BytesIO
|
|
9 |
# Initialize detector
|
10 |
detector = WeightDetector()
|
11 |
|
12 |
-
def process_input(image_source: str, image_upload=None, image_url: str = "") ->
|
13 |
-
"""
|
14 |
-
Process image from different sources (upload, webcam, or URL)
|
15 |
-
Returns:
|
16 |
-
tuple: (detected_weight, detection_metadata, annotated_image)
|
17 |
-
"""
|
18 |
temp_img_path = None
|
19 |
|
20 |
try:
|
@@ -25,7 +21,12 @@ def process_input(image_source: str, image_upload=None, image_url: str = "") ->
|
|
25 |
response = requests.get(image_url)
|
26 |
img = Image.open(BytesIO(response.content))
|
27 |
else:
|
28 |
-
return
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Save to temp file for processing
|
31 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
|
@@ -33,29 +34,52 @@ def process_input(image_source: str, image_upload=None, image_url: str = "") ->
|
|
33 |
img.save(f.name)
|
34 |
|
35 |
# Detect weight
|
36 |
-
weight,
|
37 |
|
38 |
# Format result message
|
39 |
if weight is not None:
|
40 |
-
message = f"β
Detected weight: {weight}g"
|
41 |
-
if len(metadata) > 1:
|
42 |
-
message += f" (from {len(metadata)} possible values)"
|
43 |
else:
|
44 |
-
message = "β No weight value detected"
|
45 |
|
46 |
-
return
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
except Exception as e:
|
49 |
-
return
|
|
|
|
|
|
|
|
|
|
|
50 |
finally:
|
51 |
if temp_img_path and os.path.exists(temp_img_path):
|
52 |
os.unlink(temp_img_path)
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# Gradio interface
|
55 |
-
with gr.Blocks(title="Auto Weight Logger") as demo:
|
56 |
gr.Markdown("""
|
57 |
-
# Auto Weight Logger
|
58 |
-
Capture or upload an image of a
|
59 |
""")
|
60 |
|
61 |
with gr.Row():
|
@@ -63,38 +87,55 @@ with gr.Blocks(title="Auto Weight Logger") as demo:
|
|
63 |
image_source = gr.Radio(
|
64 |
["upload", "url"],
|
65 |
label="Image Source",
|
66 |
-
value="upload"
|
|
|
67 |
)
|
68 |
|
69 |
image_upload = gr.Image(
|
70 |
-
sources=["upload"],
|
71 |
type="pil",
|
72 |
-
label="Upload Image"
|
|
|
73 |
)
|
74 |
|
75 |
image_url = gr.Textbox(
|
76 |
label="Image URL",
|
77 |
-
visible=False
|
|
|
78 |
)
|
79 |
|
80 |
-
submit_btn = gr.Button("Detect Weight")
|
81 |
|
82 |
with gr.Column():
|
83 |
weight_value = gr.Number(
|
84 |
-
label="Detected Weight (
|
85 |
-
interactive=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
)
|
87 |
|
88 |
result_message = gr.Textbox(
|
89 |
-
label="
|
90 |
-
interactive=False
|
|
|
91 |
)
|
92 |
|
93 |
annotated_image = gr.Image(
|
94 |
label="Annotated Image",
|
95 |
-
interactive=False
|
|
|
96 |
)
|
97 |
|
|
|
|
|
|
|
|
|
|
|
98 |
# Show/hide URL input based on selection
|
99 |
def toggle_url_visibility(source):
|
100 |
return gr.Textbox(visible=source == "url")
|
@@ -109,7 +150,12 @@ with gr.Blocks(title="Auto Weight Logger") as demo:
|
|
109 |
submit_btn.click(
|
110 |
process_input,
|
111 |
inputs=[image_source, image_upload, image_url],
|
112 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
113 |
)
|
114 |
|
115 |
# For Hugging Face Spaces
|
|
|
9 |
# Initialize detector
|
10 |
detector = WeightDetector()
|
11 |
|
12 |
+
def process_input(image_source: str, image_upload=None, image_url: str = "") -> dict:
|
13 |
+
"""Process image and return results with IST"""
|
|
|
|
|
|
|
|
|
14 |
temp_img_path = None
|
15 |
|
16 |
try:
|
|
|
21 |
response = requests.get(image_url)
|
22 |
img = Image.open(BytesIO(response.content))
|
23 |
else:
|
24 |
+
return {
|
25 |
+
"weight": None,
|
26 |
+
"message": "No valid image provided",
|
27 |
+
"image": None,
|
28 |
+
"time": detector.get_current_ist()
|
29 |
+
}
|
30 |
|
31 |
# Save to temp file for processing
|
32 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
|
|
|
34 |
img.save(f.name)
|
35 |
|
36 |
# Detect weight
|
37 |
+
weight, time, annotated_img = detector.detect_weight(temp_img_path)
|
38 |
|
39 |
# Format result message
|
40 |
if weight is not None:
|
41 |
+
message = f"β
Detected weight: {weight:.2f}g at {time}"
|
|
|
|
|
42 |
else:
|
43 |
+
message = f"β No weight value detected at {time}"
|
44 |
|
45 |
+
return {
|
46 |
+
"weight": weight,
|
47 |
+
"message": message,
|
48 |
+
"image": annotated_img,
|
49 |
+
"time": time
|
50 |
+
}
|
51 |
|
52 |
except Exception as e:
|
53 |
+
return {
|
54 |
+
"weight": None,
|
55 |
+
"message": f"Error: {str(e)}",
|
56 |
+
"image": None,
|
57 |
+
"time": detector.get_current_ist()
|
58 |
+
}
|
59 |
finally:
|
60 |
if temp_img_path and os.path.exists(temp_img_path):
|
61 |
os.unlink(temp_img_path)
|
62 |
|
63 |
+
# Custom CSS for better mobile display
|
64 |
+
css = """
|
65 |
+
#mobile-view {
|
66 |
+
display: none;
|
67 |
+
}
|
68 |
+
@media screen and (max-width: 768px) {
|
69 |
+
#desktop-view {
|
70 |
+
display: none;
|
71 |
+
}
|
72 |
+
#mobile-view {
|
73 |
+
display: block;
|
74 |
+
}
|
75 |
+
}
|
76 |
+
"""
|
77 |
+
|
78 |
# Gradio interface
|
79 |
+
with gr.Blocks(title="Auto Weight Logger", css=css) as demo:
|
80 |
gr.Markdown("""
|
81 |
+
# ποΈ Auto Weight Logger
|
82 |
+
Capture or upload an image of a digital scale to automatically detect the weight value.
|
83 |
""")
|
84 |
|
85 |
with gr.Row():
|
|
|
87 |
image_source = gr.Radio(
|
88 |
["upload", "url"],
|
89 |
label="Image Source",
|
90 |
+
value="upload",
|
91 |
+
elem_id="source-select"
|
92 |
)
|
93 |
|
94 |
image_upload = gr.Image(
|
95 |
+
sources=["upload", "webcam"],
|
96 |
type="pil",
|
97 |
+
label="Upload Image or Use Webcam",
|
98 |
+
elem_id="image-upload"
|
99 |
)
|
100 |
|
101 |
image_url = gr.Textbox(
|
102 |
label="Image URL",
|
103 |
+
visible=False,
|
104 |
+
elem_id="image-url"
|
105 |
)
|
106 |
|
107 |
+
submit_btn = gr.Button("Detect Weight", variant="primary")
|
108 |
|
109 |
with gr.Column():
|
110 |
weight_value = gr.Number(
|
111 |
+
label="Detected Weight (grams)",
|
112 |
+
interactive=False,
|
113 |
+
elem_id="weight-value"
|
114 |
+
)
|
115 |
+
|
116 |
+
detection_time = gr.Textbox(
|
117 |
+
label="Detection Time (IST)",
|
118 |
+
interactive=False,
|
119 |
+
elem_id="detection-time"
|
120 |
)
|
121 |
|
122 |
result_message = gr.Textbox(
|
123 |
+
label="Result",
|
124 |
+
interactive=False,
|
125 |
+
elem_id="result-message"
|
126 |
)
|
127 |
|
128 |
annotated_image = gr.Image(
|
129 |
label="Annotated Image",
|
130 |
+
interactive=False,
|
131 |
+
elem_id="annotated-image"
|
132 |
)
|
133 |
|
134 |
+
# Mobile view toggle
|
135 |
+
with gr.Column(visible=False, elem_id="mobile-view"):
|
136 |
+
gr.Markdown("### Mobile Instructions")
|
137 |
+
gr.Markdown("1. Tap 'Webcam' to capture\n2. Tap 'Detect Weight'")
|
138 |
+
|
139 |
# Show/hide URL input based on selection
|
140 |
def toggle_url_visibility(source):
|
141 |
return gr.Textbox(visible=source == "url")
|
|
|
150 |
submit_btn.click(
|
151 |
process_input,
|
152 |
inputs=[image_source, image_upload, image_url],
|
153 |
+
outputs={
|
154 |
+
"weight": weight_value,
|
155 |
+
"message": result_message,
|
156 |
+
"image": annotated_image,
|
157 |
+
"time": detection_time
|
158 |
+
}
|
159 |
)
|
160 |
|
161 |
# For Hugging Face Spaces
|