Spaces:
Sleeping
Sleeping
File size: 3,645 Bytes
0590b95 f4861ec 0590b95 f4861ec 0590b95 136c114 136e7c4 f4861ec 0590b95 136e7c4 f4861ec 136e7c4 f4861ec 136c114 136e7c4 136c114 136e7c4 f4861ec 0590b95 136e7c4 0590b95 136c114 136e7c4 136c114 f4861ec 136e7c4 136c114 136e7c4 0590b95 136e7c4 0590b95 f4861ec 136e7c4 f4861ec 136e7c4 f4861ec 136e7c4 f4861ec 136e7c4 f4861ec 136c114 136e7c4 0590b95 f4861ec 136e7c4 136c114 136e7c4 f4861ec 136c114 136e7c4 f4861ec 136e7c4 f4861ec 136e7c4 f4861ec 0590b95 136e7c4 f4861ec 136e7c4 0590b95 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import gradio as gr
from weight_detector import WeightDetector
import tempfile
import os
detector = WeightDetector()
def process_input(image_source: str, image_upload=None, image_url: str = "") -> dict:
"""Process webcam/image and return weight + IST time"""
temp_img_path = None
try:
# Handle webcam/image upload
if image_source == "webcam" and image_upload is not None:
img = image_upload
elif image_source == "upload" and image_upload is not None:
img = image_upload
elif image_source == "url" and image_url:
import requests
from io import BytesIO
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
else:
return {
"weight": None,
"message": "β οΈ No image provided!",
"image": None,
"time": detector.get_current_ist()
}
# Save to temp file
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
temp_img_path = f.name
img.save(f.name)
# Detect weight
return detector.detect_weight(temp_img_path)
except Exception as e:
return {
"weight": None,
"message": f"β οΈ Error: {str(e)}",
"image": None,
"time": detector.get_current_ist()
}
finally:
if temp_img_path and os.path.exists(temp_img_path):
os.remove(temp_img_path)
# Gradio UI
with gr.Blocks(title="Auto Weight Logger") as demo:
gr.Markdown("""
# **βοΈ Auto Weight Logger (7-Segment OCR)**
**Capture weight from digital balances using a webcam or image upload.**
- β
Optimized for **7-segment displays** (e.g., lab balances)
- π
Logs **IST time** automatically
- π« Detects **blurry/glare** images
""")
with gr.Row():
with gr.Column():
image_source = gr.Radio(
["webcam", "upload", "url"],
label="Input Source",
value="webcam"
)
image_upload = gr.Image(
sources=["webcam", "upload"],
type="pil",
label="Capture/Upload Image",
interactive=True
)
image_url = gr.Textbox(
label="Image URL (if selected)",
visible=False
)
submit_btn = gr.Button("Detect Weight", variant="primary")
with gr.Column():
weight_value = gr.Number(
label="Detected Weight (g)",
interactive=False
)
detection_time = gr.Textbox(
label="Detection Time (IST)",
interactive=False
)
result_message = gr.Textbox(
label="Result",
interactive=False
)
annotated_image = gr.Image(
label="Annotated Image",
interactive=False
)
# Show/hide URL input
def toggle_url_visibility(source):
return gr.Textbox(visible=source == "url")
image_source.change(
toggle_url_visibility,
inputs=image_source,
outputs=image_url
)
# Process input
submit_btn.click(
process_input,
inputs=[image_source, image_upload, image_url],
outputs=[weight_value, detection_time, result_message, annotated_image]
)
demo.launch() |