Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pytesseract
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
def process_meter_image(image):
|
| 7 |
+
# Convert to PIL Image if needed
|
| 8 |
+
if not isinstance(image, Image.Image):
|
| 9 |
+
image = Image.fromarray(image)
|
| 10 |
+
|
| 11 |
+
# Extract text using OCR
|
| 12 |
+
text = pytesseract.image_to_string(image)
|
| 13 |
+
|
| 14 |
+
# Parse meter information
|
| 15 |
+
info = {
|
| 16 |
+
'brand': '',
|
| 17 |
+
'model': '',
|
| 18 |
+
'type': '',
|
| 19 |
+
'reading': '',
|
| 20 |
+
'voltage': '',
|
| 21 |
+
'current': '',
|
| 22 |
+
'frequency': '',
|
| 23 |
+
'calibration': '',
|
| 24 |
+
'property': '',
|
| 25 |
+
'year': '',
|
| 26 |
+
'serial': ''
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Extract information using regex patterns
|
| 30 |
+
patterns = {
|
| 31 |
+
'brand': r'HEXING',
|
| 32 |
+
'model': r'HXE\d+',
|
| 33 |
+
'type': r'\dPhase \dWire Meter',
|
| 34 |
+
'reading': r'(\d+)\s*kWh',
|
| 35 |
+
'voltage': r'(\d+)V',
|
| 36 |
+
'frequency': r'(\d+)Hz',
|
| 37 |
+
'calibration': r'(\d+)imp/kWh',
|
| 38 |
+
'property': r'Property of ([^\n]+)',
|
| 39 |
+
'year': r'20\d{2}',
|
| 40 |
+
'serial': r'No\.(\d+)'
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
for key, pattern in patterns.items():
|
| 44 |
+
match = re.search(pattern, text)
|
| 45 |
+
if match:
|
| 46 |
+
info[key] = match.group(1) if len(match.groups()) > 0 else match.group(0)
|
| 47 |
+
|
| 48 |
+
# Format output
|
| 49 |
+
output = f"""
|
| 50 |
+
Meter Information:
|
| 51 |
+
------------------
|
| 52 |
+
Brand: {info['brand']}
|
| 53 |
+
Model: {info['model']}
|
| 54 |
+
Type: {info['type']}
|
| 55 |
+
Current Reading: {info['reading']}
|
| 56 |
+
Voltage Rating: {info['voltage']}
|
| 57 |
+
Frequency: {info['frequency']}
|
| 58 |
+
Calibration: {info['calibration']}
|
| 59 |
+
Property of: {info['property']}
|
| 60 |
+
Manufacturing Year: {info['year']}
|
| 61 |
+
Serial Number: {info['serial']}
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
return output
|
| 65 |
+
|
| 66 |
+
# Create Gradio interface
|
| 67 |
+
iface = gr.Interface(
|
| 68 |
+
fn=process_meter_image,
|
| 69 |
+
inputs=gr.Image(type="pil"),
|
| 70 |
+
outputs=gr.Textbox(label="Extracted Information"),
|
| 71 |
+
title="Electricity Meter OCR",
|
| 72 |
+
description="Upload an image of an electricity meter to extract information",
|
| 73 |
+
examples=[["meter_image.jpg"]]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Launch the app
|
| 77 |
+
iface.launch()
|