Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,30 +12,38 @@ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
|
12 |
|
13 |
# Enhance image before OCR
|
14 |
def enhance_image(image):
|
15 |
-
|
16 |
-
image =
|
17 |
-
|
18 |
-
image =
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
return image
|
21 |
|
22 |
# Extract weight
|
23 |
def detect_weight(image):
|
24 |
try:
|
25 |
processed_image = enhance_image(image)
|
|
|
|
|
26 |
pixel_values = processor(images=processed_image, return_tensors="pt").pixel_values
|
27 |
generated_ids = model.generate(pixel_values)
|
28 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
29 |
|
30 |
-
# Extract number
|
31 |
match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)", generated_text)
|
32 |
weight = match.group(1) if match else "Not detected"
|
33 |
|
34 |
-
#
|
35 |
ist = pytz.timezone('Asia/Kolkata')
|
36 |
current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
37 |
|
38 |
-
return f"Weight: {weight} kg\nCaptured At: {current_time}", image
|
39 |
except Exception as e:
|
40 |
return f"Error: {str(e)}", image
|
41 |
|
@@ -45,7 +53,7 @@ interface = gr.Interface(
|
|
45 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
46 |
outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
|
47 |
title="⚖️ Auto Weight Detector (No Tesseract)",
|
48 |
-
description="Detects weight from digital scale image using Hugging Face TrOCR."
|
49 |
)
|
50 |
|
51 |
interface.launch()
|
|
|
12 |
|
13 |
# Enhance image before OCR
|
14 |
def enhance_image(image):
|
15 |
+
# Convert to grayscale
|
16 |
+
image = image.convert("L")
|
17 |
+
# Invert (light text on dark bg works better)
|
18 |
+
image = ImageOps.invert(image)
|
19 |
+
# Increase contrast and sharpness
|
20 |
+
image = ImageEnhance.Contrast(image).enhance(2.0)
|
21 |
+
image = ImageEnhance.Sharpness(image).enhance(2.0)
|
22 |
+
# Resize (bigger = easier for OCR)
|
23 |
+
image = image.resize((image.width * 2, image.height * 2))
|
24 |
+
# Convert back to RGB for model compatibility
|
25 |
+
image = image.convert("RGB")
|
26 |
return image
|
27 |
|
28 |
# Extract weight
|
29 |
def detect_weight(image):
|
30 |
try:
|
31 |
processed_image = enhance_image(image)
|
32 |
+
|
33 |
+
# Send to Hugging Face OCR model
|
34 |
pixel_values = processor(images=processed_image, return_tensors="pt").pixel_values
|
35 |
generated_ids = model.generate(pixel_values)
|
36 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
37 |
|
38 |
+
# Extract number using regex
|
39 |
match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)", generated_text)
|
40 |
weight = match.group(1) if match else "Not detected"
|
41 |
|
42 |
+
# Get current IST time
|
43 |
ist = pytz.timezone('Asia/Kolkata')
|
44 |
current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
45 |
|
46 |
+
return f"Weight: {weight} kg\nCaptured At: {current_time} (IST)", image
|
47 |
except Exception as e:
|
48 |
return f"Error: {str(e)}", image
|
49 |
|
|
|
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 (No Tesseract)",
|
56 |
+
description="Detects weight from digital scale image using Hugging Face TrOCR. Shows weight and capture time (IST)."
|
57 |
)
|
58 |
|
59 |
interface.launch()
|