Shivam3002 commited on
Commit
c3ef86d
·
verified ·
1 Parent(s): 842c7e8

Update app.py

Browse files

Image Preprocessing: Enhance contrast and apply grayscale to improve text extraction.

Custom Regex: Adjust regex to capture the serial number format seen in the image.

Alternative Models: Consider using easyocr for better performance on printed meter readings.

Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -1,46 +1,58 @@
1
  import gradio as gr
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
23
- image = Image.fromarray(image).convert("RGB")
24
- pixel_values = processor(images=image, return_tensors="pt").pixel_values
25
 
26
- # Generate text from image
27
- with torch.no_grad():
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
 
 
37
  # Create Gradio interface
38
  iface = gr.Interface(
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
 
1
  import gradio as gr
2
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ from PIL import Image, ImageEnhance, ImageOps
4
  import torch
5
  import re
6
+ import easyocr
7
+
8
+ # Initialize EasyOCR Reader
9
+ reader = easyocr.Reader(['en'])
10
 
11
  # Load the pre-trained model and processor
12
  processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-printed')
13
  model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-printed')
14
  model.eval()
15
 
16
+ def enhance_image(image):
17
+ # Enhance the image for better OCR results
18
+ image = ImageEnhance.Contrast(image).enhance(2.0)
19
+ image = ImageOps.grayscale(image)
20
+ return image
21
+
22
+
23
  def extract_details(text):
24
  # Use regex to extract serial number and meter reading
25
+ sr_no = re.search(r'(?:Sr\.No|SR\.NO|Serial Number|SR NO)[:\s]*([\d]+)', text, re.IGNORECASE)
26
+ meter_reading = re.search(r'(?:Reading|Meter Reading|kWh|KW H)[:\s]*([\d]+)', text, re.IGNORECASE)
27
  sr_no_text = sr_no.group(1) if sr_no else "Not Found"
28
  reading_text = meter_reading.group(1) if meter_reading else "Not Found"
29
  return sr_no_text, reading_text
30
 
31
+
32
  def extract_meter_reading(image):
33
  try:
34
+ # Enhance image
35
+ image = Image.fromarray(image)
36
+ enhanced_image = enhance_image(image)
37
 
38
+ # Use EasyOCR to get better results
39
+ result = reader.readtext(enhanced_image)
40
+ text = " ".join([item[1] for item in result])
 
41
 
42
+ # Extract details from the OCR text
43
+ sr_no, reading = extract_details(text)
44
  return f"Serial Number: {sr_no}\nMeter Reading: {reading}"
45
  except Exception as e:
46
  return f"Error: {str(e)}"
47
 
48
+
49
  # Create Gradio interface
50
  iface = gr.Interface(
51
  fn=extract_meter_reading,
52
  inputs=gr.Image(type="numpy", label="Upload or Capture Meter Image"),
53
  outputs="text",
54
  title="Meter Reading and Serial Number Extractor",
55
+ description="Upload a meter image to extract the serial number and meter reading using OCR techniques."
56
  )
57
 
58
  # Launch the Gradio app