Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,36 +10,42 @@ import re
|
|
10 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
11 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
12 |
|
13 |
-
#
|
14 |
def enhance_image(image):
|
15 |
# Convert to grayscale
|
16 |
image = image.convert("L")
|
17 |
-
# Invert
|
18 |
image = ImageOps.invert(image)
|
19 |
# Increase contrast and sharpness
|
20 |
-
image = ImageEnhance.Contrast(image).enhance(2.
|
21 |
-
image = ImageEnhance.Sharpness(image).enhance(
|
22 |
-
# Resize (bigger = easier
|
23 |
-
image = image.resize((image.width *
|
24 |
-
# Convert back to RGB for model
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# Extract weight
|
29 |
def detect_weight(image):
|
30 |
try:
|
|
|
31 |
processed_image = enhance_image(image)
|
32 |
|
33 |
-
#
|
34 |
pixel_values = processor(images=processed_image, return_tensors="pt").pixel_values
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
# Extract
|
39 |
-
match = re.search(r"(\d{1,4}(?:\.\d{1,
|
40 |
weight = match.group(1) if match else "Not detected"
|
41 |
|
42 |
-
#
|
43 |
ist = pytz.timezone('Asia/Kolkata')
|
44 |
current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
45 |
|
@@ -52,8 +58,8 @@ interface = gr.Interface(
|
|
52 |
fn=detect_weight,
|
53 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
54 |
outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
|
55 |
-
title="⚖️ Auto Weight Detector (
|
56 |
-
description="Detects weight from digital scale image using Hugging Face
|
57 |
)
|
58 |
|
59 |
interface.launch()
|
|
|
10 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
11 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
12 |
|
13 |
+
# Preprocess image to enhance OCR accuracy
|
14 |
def enhance_image(image):
|
15 |
# Convert to grayscale
|
16 |
image = image.convert("L")
|
17 |
+
# Invert for better contrast
|
18 |
image = ImageOps.invert(image)
|
19 |
# Increase contrast and sharpness
|
20 |
+
image = ImageEnhance.Contrast(image).enhance(2.5)
|
21 |
+
image = ImageEnhance.Sharpness(image).enhance(3.0)
|
22 |
+
# Resize (bigger = easier to read digits clearly)
|
23 |
+
image = image.resize((image.width * 3, image.height * 3))
|
24 |
+
# Convert back to RGB for model
|
25 |
+
return image.convert("RGB")
|
26 |
+
|
27 |
+
# Extract accurate decimal weight
|
|
|
28 |
def detect_weight(image):
|
29 |
try:
|
30 |
+
# Enhance image
|
31 |
processed_image = enhance_image(image)
|
32 |
|
33 |
+
# OCR using Hugging Face
|
34 |
pixel_values = processor(images=processed_image, return_tensors="pt").pixel_values
|
35 |
+
# Use slightly longer decoding to improve accuracy
|
36 |
+
generated_ids = model.generate(
|
37 |
+
pixel_values,
|
38 |
+
max_length=64,
|
39 |
+
num_beams=4, # Beam search to improve precision
|
40 |
+
early_stopping=True
|
41 |
+
)
|
42 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
43 |
|
44 |
+
# Extract full decimal weight like 52.75 or 18.89
|
45 |
+
match = re.search(r"(\d{1,4}(?:\.\d{1,4})?)", generated_text)
|
46 |
weight = match.group(1) if match else "Not detected"
|
47 |
|
48 |
+
# Timestamp in IST
|
49 |
ist = pytz.timezone('Asia/Kolkata')
|
50 |
current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
51 |
|
|
|
58 |
fn=detect_weight,
|
59 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
60 |
outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
|
61 |
+
title="⚖️ Auto Weight Detector (Decimal Accurate)",
|
62 |
+
description="Detects full weight including decimals (e.g., 52.75 kg) from digital scale image using Hugging Face OCR."
|
63 |
)
|
64 |
|
65 |
interface.launch()
|