Sanjayraju30 commited on
Commit
d55b56b
ยท
verified ยท
1 Parent(s): b90c552

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -113
app.py CHANGED
@@ -1,119 +1,37 @@
1
  import gradio as gr
2
- from weight_detector import WeightDetector
3
- import tempfile
4
- import os
5
 
6
- detector = WeightDetector()
 
 
7
 
8
- def process_input(image_source: str, image_upload=None, image_url: str = "") -> dict:
9
- """Process webcam/image and return weight + IST time"""
10
- temp_img_path = None
11
- try:
12
- # Handle webcam/image upload
13
- if image_source == "webcam" and image_upload is not None:
14
- img = image_upload
15
- elif image_source == "upload" and image_upload is not None:
16
- img = image_upload
17
- elif image_source == "url" and image_url:
18
- import requests
19
- from io import BytesIO
20
- response = requests.get(image_url)
21
- img = Image.open(BytesIO(response.content))
22
- else:
23
- return {
24
- "weight": None,
25
- "message": "โš ๏ธ No image provided!",
26
- "image": None,
27
- "time": detector.get_current_ist()
28
- }
29
-
30
- # Save to temp file
31
- with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
32
- temp_img_path = f.name
33
- img.save(f.name)
34
-
35
- # Detect weight
36
- return detector.detect_weight(temp_img_path)
37
-
38
- except Exception as e:
39
- return {
40
- "weight": None,
41
- "message": f"โš ๏ธ Error: {str(e)}",
42
- "image": None,
43
- "time": detector.get_current_ist()
44
- }
45
- finally:
46
- if temp_img_path and os.path.exists(temp_img_path):
47
- os.remove(temp_img_path)
48
 
49
- # Gradio UI
50
- with gr.Blocks(title="Auto Weight Logger") as demo:
51
- gr.Markdown("""
52
- # **โš–๏ธ Auto Weight Logger (7-Segment OCR)**
53
- **Capture weight from digital balances using a webcam or image upload.**
54
- - โœ… Optimized for **7-segment displays** (e.g., lab balances)
55
- - ๐Ÿ“… Logs **IST time** automatically
56
- - ๐Ÿšซ Detects **blurry/glare** images
57
- """)
58
-
59
  with gr.Row():
60
- with gr.Column():
61
- image_source = gr.Radio(
62
- ["webcam", "upload", "url"],
63
- label="Input Source",
64
- value="webcam"
65
- )
66
-
67
- image_upload = gr.Image(
68
- sources=["webcam", "upload"],
69
- type="pil",
70
- label="Capture/Upload Image",
71
- interactive=True
72
- )
73
-
74
- image_url = gr.Textbox(
75
- label="Image URL (if selected)",
76
- visible=False
77
- )
78
-
79
- submit_btn = gr.Button("Detect Weight", variant="primary")
80
-
81
- with gr.Column():
82
- weight_value = gr.Number(
83
- label="Detected Weight (g)",
84
- interactive=False
85
- )
86
-
87
- detection_time = gr.Textbox(
88
- label="Detection Time (IST)",
89
- interactive=False
90
- )
91
-
92
- result_message = gr.Textbox(
93
- label="Result",
94
- interactive=False
95
- )
96
-
97
- annotated_image = gr.Image(
98
- label="Annotated Image",
99
- interactive=False
100
- )
101
-
102
- # Show/hide URL input
103
- def toggle_url_visibility(source):
104
- return gr.Textbox(visible=source == "url")
105
-
106
- image_source.change(
107
- toggle_url_visibility,
108
- inputs=image_source,
109
- outputs=image_url
110
- )
111
-
112
- # Process input
113
- submit_btn.click(
114
- process_input,
115
- inputs=[image_source, image_upload, image_url],
116
- outputs=[weight_value, detection_time, result_message, annotated_image]
117
- )
118
 
119
- demo.launch()
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ from datetime import datetime
4
+ from ocr_engine import extract_weight
5
 
6
+ def process_image(image):
7
+ if image is None:
8
+ return "No image received", None, None
9
 
10
+ weight = extract_weight(image)
11
+ now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
12
+ return f"{weight} grams", now, image
13
+
14
+ with gr.Blocks() as demo:
15
+ gr.Markdown("## ๐Ÿ“ท Auto Weight Logger โ€” OCR-Based Smart Scale Reader")
16
+ gr.Markdown("Upload a snapshot of a digital weight display or use webcam. The app extracts weight using OCR.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
 
 
 
 
 
 
 
 
 
 
18
  with gr.Row():
19
+ webcam = gr.Image(source="webcam", type="pil", label="๐Ÿ“ธ Capture from Webcam")
20
+ upload = gr.Image(source="upload", type="pil", label="๐Ÿ“ Or Upload Image")
21
+
22
+ image_input = gr.State()
23
+
24
+ def choose_image(w, u):
25
+ return w if w else u
26
+
27
+ webcam.change(fn=choose_image, inputs=[webcam, upload], outputs=image_input)
28
+ upload.change(fn=choose_image, inputs=[webcam, upload], outputs=image_input)
29
+
30
+ submit_btn = gr.Button("๐Ÿš€ Detect Weight")
31
+ weight_out = gr.Textbox(label="Detected Weight")
32
+ time_out = gr.Textbox(label="Captured At (IST)")
33
+ snapshot_out = gr.Image(label="Snapshot")
34
+
35
+ submit_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot_out])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ demo.launch()