|
|
import gradio as gr |
|
|
import pytesseract |
|
|
from PIL import Image |
|
|
import re |
|
|
|
|
|
|
|
|
def process_meter_image(image): |
|
|
|
|
|
if not isinstance(image, Image.Image): |
|
|
image = Image.fromarray(image) |
|
|
|
|
|
|
|
|
text = pytesseract.image_to_string(image) |
|
|
|
|
|
|
|
|
info = { |
|
|
'brand': '', |
|
|
'model': '', |
|
|
'type': '', |
|
|
'reading': '', |
|
|
'voltage': '', |
|
|
'current': '', |
|
|
'frequency': '', |
|
|
'calibration': '', |
|
|
'property': '', |
|
|
'year': '', |
|
|
'serial': '' |
|
|
} |
|
|
|
|
|
|
|
|
patterns = { |
|
|
'brand': r'HEXING', |
|
|
'model': r'HXE\d+', |
|
|
'type': r'\dPhase \dWire Meter', |
|
|
'reading': r'(\d+)\s*kWh', |
|
|
'voltage': r'(\d+)V', |
|
|
'frequency': r'(\d+)Hz', |
|
|
'calibration': r'(\d+)imp/kWh', |
|
|
'property': r'Property of ([^\n]+)', |
|
|
'year': r'20\d{2}', |
|
|
'serial': r'No\.(\d+)' |
|
|
} |
|
|
|
|
|
for key, pattern in patterns.items(): |
|
|
match = re.search(pattern, text) |
|
|
if match: |
|
|
info[key] = match.group(1) if len(match.groups()) > 0 else match.group(0) |
|
|
|
|
|
|
|
|
output = f""" |
|
|
Meter Information: |
|
|
------------------ |
|
|
Brand: {info['brand']} |
|
|
Model: {info['model']} |
|
|
Type: {info['type']} |
|
|
Current Reading: {info['reading']} |
|
|
Voltage Rating: {info['voltage']} |
|
|
Frequency: {info['frequency']} |
|
|
Calibration: {info['calibration']} |
|
|
Property of: {info['property']} |
|
|
Manufacturing Year: {info['year']} |
|
|
Serial Number: {info['serial']} |
|
|
""" |
|
|
|
|
|
return output |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=process_meter_image, |
|
|
inputs=gr.Image(type="pil"), |
|
|
outputs=gr.Textbox(label="Extracted Information"), |
|
|
title="Electricity Meter OCR", |
|
|
description="Upload an image of an electricity meter to extract information", |
|
|
examples=[["meter_image.jpg"]] |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch() |