Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
timestamp = gr.Textbox(label="🕒 Captured At (IST)")
|
24 |
-
snapshot = gr.Image(label="📸 Snapshot Image")
|
25 |
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|