Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,57 +10,72 @@ import numpy as np
|
|
| 10 |
# Initialize EasyOCR Reader
|
| 11 |
reader = easyocr.Reader(['en'])
|
| 12 |
|
| 13 |
-
# Load the pre-trained model and processor
|
| 14 |
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-printed')
|
| 15 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-printed')
|
| 16 |
model.eval()
|
| 17 |
|
| 18 |
def enhance_image(image):
|
| 19 |
-
# Enhance the image for better OCR results
|
| 20 |
image = ImageEnhance.Contrast(image).enhance(2.0)
|
|
|
|
| 21 |
image = ImageOps.grayscale(image)
|
| 22 |
return image
|
| 23 |
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
reading_text = meter_reading.group(1) if meter_reading else "Not Found"
|
| 31 |
-
return sr_no_text, reading_text
|
| 32 |
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Convert PIL image to bytes for EasyOCR
|
| 41 |
-
img_byte_arr = BytesIO()
|
| 42 |
-
enhanced_image.save(img_byte_arr, format='PNG')
|
| 43 |
-
img_byte_arr = img_byte_arr.getvalue()
|
| 44 |
|
| 45 |
-
# Use EasyOCR to get better results
|
| 46 |
-
result = reader.readtext(img_byte_arr)
|
| 47 |
-
text = " ".join([item[1] for item in result])
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error: {str(e)}"
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
fn=extract_meter_reading,
|
| 59 |
-
inputs=gr.Image(type="numpy", label="Upload or Capture Meter Image"),
|
| 60 |
-
outputs="text",
|
| 61 |
-
title="Meter Reading and Serial Number Extractor",
|
| 62 |
-
description="Upload a meter image to extract the serial number and meter reading using OCR techniques."
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
# Launch the Gradio app
|
| 66 |
-
iface.launch()
|
|
|
|
| 10 |
# Initialize EasyOCR Reader
|
| 11 |
reader = easyocr.Reader(['en'])
|
| 12 |
|
| 13 |
+
# Load the pre-trained model and processor for fallback
|
| 14 |
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-printed')
|
| 15 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-printed')
|
| 16 |
model.eval()
|
| 17 |
|
| 18 |
def enhance_image(image):
|
|
|
|
| 19 |
image = ImageEnhance.Contrast(image).enhance(2.0)
|
| 20 |
+
image = ImageEnhance.Sharpness(image).enhance(2.0)
|
| 21 |
image = ImageOps.grayscale(image)
|
| 22 |
return image
|
| 23 |
|
| 24 |
|
| 25 |
+
def ocr_with_easyocr(pil_img):
|
| 26 |
+
# Convert PIL to bytes for EasyOCR
|
| 27 |
+
buf = BytesIO()
|
| 28 |
+
pil_img.save(buf, format='PNG')
|
| 29 |
+
return reader.readtext(buf.getvalue(), detail=0)
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
+
def ocr_with_trocr(pil_img):
|
| 33 |
+
pixel_values = processor(images=pil_img.convert("RGB"), return_tensors="pt").pixel_values
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
ids = model.generate(pixel_values)
|
| 36 |
+
return processor.batch_decode(ids, skip_special_tokens=True)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def extract_meter_reading(image):
|
| 40 |
+
try:
|
| 41 |
+
pil = Image.fromarray(image)
|
| 42 |
+
w, h = pil.size
|
| 43 |
+
# Define regions: reading top 40%, serial bottom 40%
|
| 44 |
+
top_region = pil.crop((0, 0, w, int(h * 0.4)))
|
| 45 |
+
bottom_region = pil.crop((0, int(h * 0.5), w, h))
|
| 46 |
+
# Enhance regions
|
| 47 |
+
top_enh = enhance_image(top_region)
|
| 48 |
+
bot_enh = enhance_image(bottom_region)
|
| 49 |
+
# OCR regions
|
| 50 |
+
top_texts = ocr_with_easyocr(top_enh) + [ocr_with_trocr(top_enh)]
|
| 51 |
+
bot_texts = ocr_with_easyocr(bot_enh) + [ocr_with_trocr(bot_enh)]
|
| 52 |
+
top_combined = " ".join(top_texts)
|
| 53 |
+
bot_combined = " ".join(bot_texts)
|
| 54 |
+
# Extract reading: look for digits near kwh
|
| 55 |
+
reading_match = re.search(r"(\d+)\s*(?=kwh|kw h|k w h)", top_combined, re.IGNORECASE)
|
| 56 |
+
if not reading_match:
|
| 57 |
+
# fallback: any 4-7 digit number
|
| 58 |
+
fallback = re.findall(r"\b\d{4,7}\b", top_combined)
|
| 59 |
+
reading = fallback[0] if fallback else "Not Found"
|
| 60 |
+
else:
|
| 61 |
+
reading = reading_match.group(1)
|
| 62 |
+
# Extract serial: longest digit sequence in bottom
|
| 63 |
+
serials = re.findall(r"\d{6,12}", bot_combined)
|
| 64 |
+
serial = max(serials, key=len) if serials else "Not Found"
|
| 65 |
+
return f"Serial Number: {serial}\nMeter Reading: {reading}"
|
| 66 |
except Exception as e:
|
| 67 |
return f"Error: {str(e)}"
|
| 68 |
|
| 69 |
+
# Gradio app
|
| 70 |
+
def main():
|
| 71 |
+
iface = gr.Interface(
|
| 72 |
+
fn=extract_meter_reading,
|
| 73 |
+
inputs=gr.Image(type="numpy", label="Upload or Capture Meter Image"),
|
| 74 |
+
outputs="text",
|
| 75 |
+
title="Meter Reading and Serial Number Extractor",
|
| 76 |
+
description="Upload a meter image; extracts serial number and meter reading using region-based OCR."
|
| 77 |
+
)
|
| 78 |
+
iface.launch()
|
| 79 |
|
| 80 |
+
if __name__ == '__main__':
|
| 81 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|