Sanjayraju30 commited on
Commit
56810b2
·
verified ·
1 Parent(s): 68f0e96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -37
app.py CHANGED
@@ -1,43 +1,33 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from mmocr.apis import MMOCR
3
+ import cv2
4
+ import os
5
 
6
+ # Initialize MMOCR once
7
+ ocr = MMOCR(det='DB_r18', recog='SAR', device='cpu') # use 'cuda:0' if GPU is available
 
8
 
9
+ def detect_weight(image):
10
+ results = ocr.readtext(image, output=None)
11
+
12
+ detected_texts = [item['text'] for item in results[0]['result']]
13
+
14
+ # Search for weight-like patterns like "25.5 kg", "100kg", etc.
15
+ weight = "Not detected"
16
+ for text in detected_texts:
17
+ if 'kg' in text.lower():
18
+ weight = text
19
+ break
20
 
21
+ return f"Detected Weight: {weight}"
 
 
22
 
23
+ # Gradio UI
24
+ interface = gr.Interface(
25
+ fn=detect_weight,
26
+ inputs=gr.Image(type="filepath", label="Upload Weight Image"),
27
+ outputs=gr.Textbox(label="OCR Output"),
28
+ title="Weight Detector using MMOCR",
29
+ description="Upload an image with weight text (e.g., '25 kg'), and this app will detect it."
30
+ )
31
 
32
+ if __name__ == "__main__":
33
+ interface.launch()