Sanjayraju30 commited on
Commit
37bea37
Β·
verified Β·
1 Parent(s): 0d669a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -29
app.py CHANGED
@@ -1,32 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from datetime import datetime
3
  import pytz
4
- from ocr_engine import extract_weight_from_image
5
-
6
- def process_image(img):
7
- if img is None:
8
- return "No image uploaded", None, None, "No OCR output"
9
-
10
- ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
11
- weight, confidence, raw_text = extract_weight_from_image(img)
12
- return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, raw_text
13
-
14
- with gr.Blocks(title="βš–οΈ Auto Weight Logger") as demo:
15
- gr.Markdown("## βš–οΈ Auto Weight Logger")
16
- gr.Markdown("πŸ“· **Upload or capture an image of a digital weight scale.** The app will auto-detect the weight in kilograms using OCR.")
17
-
18
- with gr.Row():
19
- image_input = gr.Image(type="pil", label="Upload / Capture Image")
20
- output_weight = gr.Textbox(label="βš–οΈ Detected Weight (in kg)")
21
-
22
- with gr.Row():
23
- timestamp = gr.Textbox(label="πŸ•’ Captured At (IST)")
24
- snapshot = gr.Image(label="πŸ“Έ Snapshot Image")
25
-
26
- with gr.Row():
27
- debug_output = gr.Textbox(label="πŸͺ΅ Raw OCR Output")
28
-
29
- submit = gr.Button("πŸ” Detect Weight")
30
- submit.click(process_image, inputs=image_input, outputs=[output_weight, timestamp, snapshot, debug_output])
31
-
32
- demo.launch()
 
1
+ import os
2
+ import subprocess
3
+
4
+ # Force install mmocr at runtime if not available
5
+ try:
6
+ from mmocr.utils.ocr import MMOCR
7
+ print("βœ… MMOCR module loaded successfully.")
8
+ except ImportError:
9
+ print("⚠️ MMOCR not found, installing...")
10
+ subprocess.run(["pip", "install", "mmocr==0.5.0", "--no-deps"])
11
+ from mmocr.utils.ocr import MMOCR
12
+ print("βœ… MMOCR installed and loaded.")
13
+
14
+ from ocr_engine import extract_weight_from_image
15
  import gradio as gr
16
  from datetime import datetime
17
  import pytz
18
+ from PIL import Image
19
+
20
+ def process_image(image):
21
+ if image is None:
22
+ return "No image provided", "", None, None
23
+
24
+ weight, debug_text = extract_weight_from_image(image)
25
+
26
+ # Get IST time
27
+ ist = pytz.timezone('Asia/Kolkata')
28
+ captured_at = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S %Z")
29
+
30
+ return f"{weight} kg" if weight else "Not detected", captured_at, image, debug_text
31
+
32
+ gr.Interface(
33
+ fn=process_image,
34
+ inputs=gr.Image(type="pil", label="Upload Weight Image"),
35
+ outputs=[
36
+ gr.Textbox(label="Detected Weight"),
37
+ gr.Textbox(label="Captured At (IST)"),
38
+ gr.Image(label="Snapshot"),
39
+ gr.Textbox(label="OCR Debug Output")
40
+ ],
41
+ title="Auto Weight Logger using MMOCR",
42
+ description="Upload a weight scale image. This app uses MMOCR to detect the weight."
43
+ ).launch()