Sanjayraju30 commited on
Commit
eff70bd
·
verified ·
1 Parent(s): 54e6892

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -1,29 +1,44 @@
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
9
 
10
- ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
11
- weight, _ = extract_weight_from_image(img)
12
- return weight, ist_time, img
 
 
 
 
 
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 scale display** to auto-detect the weight in kilograms.")
 
17
 
18
- with gr.Row():
19
- image_input = gr.Image(type="pil", label="📷 Upload or Capture Image")
20
- output_weight = gr.Textbox(label="⚖️ Detected Weight")
21
 
22
- with gr.Row():
23
- timestamp = gr.Textbox(label="🕒 Captured At (IST)")
24
- snapshot = gr.Image(label="📸 Snapshot Image")
25
 
26
- submit = gr.Button("🔍 Detect Weight")
27
- submit.click(process_image, inputs=image_input, outputs=[output_weight, timestamp, snapshot])
28
 
29
- demo.launch()
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ import torch
4
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
5
  from datetime import datetime
6
  import pytz
 
7
 
8
+ # Load the model and processor from Hugging Face
9
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
10
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
11
 
12
+ # Function to detect weight text from image
13
+ def detect_weight(image):
14
+ try:
15
+ # Preprocess image
16
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
17
+ # Run model
18
+ generated_ids = model.generate(pixel_values)
19
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
20
 
21
+ # Try to extract weight-like number
22
+ import re
23
+ match = re.search(r"(\d{1,3}(\.\d{1,2})?)", generated_text)
24
+ weight = match.group(1) if match else "Not detected"
25
 
26
+ # Get IST time
27
+ ist = pytz.timezone('Asia/Kolkata')
28
+ current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
29
 
30
+ return f"Weight: {weight} kg\nCaptured At: {current_time}", image
 
 
31
 
32
+ except Exception as e:
33
+ return f"Error: {str(e)}", image
34
 
35
+ # Gradio UI
36
+ interface = gr.Interface(
37
+ fn=detect_weight,
38
+ inputs=gr.Image(type="pil", label="Upload or Capture Image"),
39
+ outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
40
+ title="⚖️ Auto Weight Detector (No Tesseract)",
41
+ description="Detects weight from digital scale image using AI-based OCR (no Tesseract)."
42
+ )
43
+
44
+ interface.launch()