liveBook · Manning

OCR

As explained in Digital Twins in Action

OCR is the automated recognition and conversion of text in images or scanned documents into machine-readable characters.

Optical character recognition (OCR) converts text in images or scanned documents into machine-readable characters. It can recover historical readings from records that a digital twin cannot directly process, such as scanned utility bills. section 2.3 section 2.3.1

A practical extraction pipeline separates recognition from field selection. Tesseract first converts an image to text; format-specific patterns then select the meter reading and date. The extraction logic must be tuned to the bill format before it is reused across an archive. section 2.3.1

Listing 2.1 Extracting the meter reading by using OCR to digitize data
from PIL import Image
import pytesseract, re

def extract_meter_reading(text):
  pattern = r"Current Meter Reading:\s*(\d+)"  #1
  match = re.search(pattern, text)
  return int(match.group(1)) if match else None

def extract_date(text):
  pattern = r"(\d{1,2}/\d{1,2}/\d{4})"  #2
  matches = re.findall(pattern, text)
  return matches[-1] if matches else None

def read_bill():
  image = Image.open(r"./images/gasBill.png")
  text = pytesseract.image_to_string(image)

  meter_reading = extract_meter_reading(text)
  date = extract_date(text)

  return {
    "meter_reading": meter_reading,
    "date": date
  }

This Python listing reads a bill image with Tesseract, then uses regular expressions to select the current meter reading and the last date found. section 2.3.1

Reading a photographed meter

Crop the photograph to the digit area, convert it to grayscale, and invert it before running OCR. The example restricts recognition to digits and extracts the reading 42678. The reading can be stored with the photograph’s timestamp and location. section 2.3.2

Listing 2.2 Example code that extracts a reading of an analog meter using OCR
import cv2, pytesseract
import numpy as np
from PIL import Image, ImageOps

def extract_meter_reading(image_path, display_results=True):
  img = Image.open(image_path)
  crop_box = (1000, 890, 1700, 1030)  #1
  cropped_img = img.crop(crop_box)  #2
  gray_img = ImageOps.grayscale(cropped_img) #3
  inverted_img = ImageOps.invert(gray_img)  #4
  meter_reading = pytesseract.image_to_string(  #5
    inverted_img,
    config='--psm 7 -c tessedit_char_whitelist=0123456789'
  )
  meter_reading = meter_reading.strip()  #6

  return meter_reading

The listing shows the image preprocessing steps and Tesseract configuration for a single line of digits. section 2.3.2

OCR is imperfect, so extracted data needs validation before it is relied on. For automated meter reading, edge processing can transmit the recognized value instead of every image. One camera reduced a 100 KB image to a 10-byte reading, but required calibration and retraining for shadows, glare, and condensation. Periodically sending a raw image can help verify the processing. section 2.3 section 3.4.1

How Digital Twins in Action covers it

How deep it goes
Works through image preprocessing and field extraction, then addresses edge deployment and calibration.
Examples
Uses scanned utility bills and a photographed analog water meter to demonstrate extraction.
What you can do after
Apply it via Tesseract and Python to extract bill fields or meter digits from images.
How it connects
Connects OCR to digital twins, computer vision, edge processing, and machine learning.

Also explained in

Compare how 2 books explain OCR →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.