Sanjayraju30 commited on
Commit
c1c018a
Β·
verified Β·
1 Parent(s): 6e93fb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -5,33 +5,32 @@ 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()
 
5
 
6
  def process_image(image):
7
  if image is None:
8
+ return "No image provided", None, None
 
9
  weight = extract_weight(image)
10
  now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
11
  return f"{weight} grams", now, image
12
 
13
  with gr.Blocks() as demo:
14
  gr.Markdown("## πŸ“· Auto Weight Logger β€” OCR-Based Smart Scale Reader")
15
+ gr.Markdown("Upload a snapshot or use webcam. The app extracts weight using OCR.")
16
 
17
  with gr.Row():
18
+ camera_input = gr.Camera(label="πŸ“Έ Capture via Webcam", type="pil")
19
+ upload_input = gr.Image(label="πŸ“ Upload Image", type="pil")
20
 
21
+ selected_image = gr.State()
22
 
23
+ def choose_latest_image(cam_img, upload_img):
24
+ return cam_img if cam_img is not None else upload_img
25
 
26
+ camera_input.change(fn=choose_latest_image, inputs=[camera_input, upload_input], outputs=selected_image)
27
+ upload_input.change(fn=choose_latest_image, inputs=[camera_input, upload_input], outputs=selected_image)
28
 
29
+ btn = gr.Button("πŸš€ Detect Weight")
30
  weight_out = gr.Textbox(label="Detected Weight")
31
  time_out = gr.Textbox(label="Captured At (IST)")
32
+ preview = gr.Image(label="Snapshot")
33
 
34
+ btn.click(fn=process_image, inputs=selected_image, outputs=[weight_out, time_out, preview])
35
 
36
  demo.launch()