Spaces:
Sleeping
Sleeping
Update app.py
Browse filesextract both the serial number (Sr. No) and meter reading from the uploaded meter images.
app.py
CHANGED
|
@@ -2,12 +2,21 @@ import gradio as gr
|
|
| 2 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
# Load the pre-trained model and processor
|
| 7 |
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-printed')
|
| 8 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-printed')
|
| 9 |
model.eval()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def extract_meter_reading(image):
|
| 12 |
try:
|
| 13 |
# Convert and preprocess the image
|
|
@@ -19,7 +28,9 @@ def extract_meter_reading(image):
|
|
| 19 |
generated_ids = model.generate(pixel_values)
|
| 20 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
return f"Error: {str(e)}"
|
| 25 |
|
|
@@ -28,8 +39,8 @@ iface = gr.Interface(
|
|
| 28 |
fn=extract_meter_reading,
|
| 29 |
inputs=gr.Image(type="numpy", label="Upload or Capture Meter Image"),
|
| 30 |
outputs="text",
|
| 31 |
-
title="Meter Reading Extractor",
|
| 32 |
-
description="Upload a meter image to extract the reading using a lightweight OCR model."
|
| 33 |
)
|
| 34 |
|
| 35 |
# Launch the Gradio app
|
|
|
|
| 2 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
# Load the pre-trained model and processor
|
| 8 |
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-printed')
|
| 9 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-printed')
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
+
def extract_details(text):
|
| 13 |
+
# Use regex to extract serial number and meter reading
|
| 14 |
+
sr_no = re.search(r'(?:Sr\.No|SR\.NO|Serial Number|SR NO)[:\s]*([\w\d\-]+)', text, re.IGNORECASE)
|
| 15 |
+
meter_reading = re.search(r'(?:Reading|Meter Reading|Reading:)[:\s]*([\d\.]+)', text, re.IGNORECASE)
|
| 16 |
+
sr_no_text = sr_no.group(1) if sr_no else "Not Found"
|
| 17 |
+
reading_text = meter_reading.group(1) if meter_reading else "Not Found"
|
| 18 |
+
return sr_no_text, reading_text
|
| 19 |
+
|
| 20 |
def extract_meter_reading(image):
|
| 21 |
try:
|
| 22 |
# Convert and preprocess the image
|
|
|
|
| 28 |
generated_ids = model.generate(pixel_values)
|
| 29 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 30 |
|
| 31 |
+
# Extract details from the generated text
|
| 32 |
+
sr_no, reading = extract_details(generated_text)
|
| 33 |
+
return f"Serial Number: {sr_no}\nMeter Reading: {reading}"
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error: {str(e)}"
|
| 36 |
|
|
|
|
| 39 |
fn=extract_meter_reading,
|
| 40 |
inputs=gr.Image(type="numpy", label="Upload or Capture Meter Image"),
|
| 41 |
outputs="text",
|
| 42 |
+
title="Meter Reading and Serial Number Extractor",
|
| 43 |
+
description="Upload a meter image to extract the serial number and meter reading using a lightweight OCR model."
|
| 44 |
)
|
| 45 |
|
| 46 |
# Launch the Gradio app
|