[Python] Performing OCR of Seven Segment Display Multimeter

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    Firstly, I am very very new to these things, and I have come this far with the help of ChatGPT.

    We recorded some videos of two multimeters that have seven-segment displays. I want to OCR them to use later to sketch graphs. I am using a config file that has names and xy-coordinates. My code works, but when I see the cropped pictures, I think they are very readable. However, OCR doesn't read most of them, and the ones it reads are all wrong. How can I achieve it to read all that correctly?

    Original image:

    [​IMG]

    Resulting image:

    [​IMG]

    My code:

    # -- coding: utf-8 --
    import cv2 import pytesseract

    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

    with open('config.txt', 'r') as f: lines = f.readlines()
    for line in lines: parts = line.strip().split()
    if len(parts) != 9:
    continue

    video_name = parts[0]
    volt_y1, volt_y2, volt_x1, volt_x2 = map(int, parts[1:5])
    curr_y1, curr_y2, curr_x1, curr_x2 = map(int, parts[5:9])

    cap = cv2.VideoCapture(video_name)

    fps = cap.get(cv2.CAP_PROP_FPS)
    frame_interval = int(fps * 0.5)

    frame_count = 0

    while True:
    ret, frame = cap.read()
    if not ret:
    break

    if frame_count % frame_interval == 0:
    volt_crop = frame[volt_y1:volt_y2, volt_x1:volt_x2]
    curr_crop = frame[curr_y1:curr_y2, curr_x1:curr_x2]


    volt_crop_gray = cv2.cvtColor(volt_crop, cv2.COLOR_BGR2GRAY)
    volt_crop_thresh = cv2.threshold(volt_crop_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

    curr_crop_gray = cv2.cvtColor(curr_crop, cv2.COLOR_BGR2GRAY)
    curr_crop_thresh = cv2.threshold(curr_crop_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

    # OCR
    volt_text = pytesseract.image_to_string(volt_crop_thresh, config='--psm 7', lang='7seg')
    curr_text = pytesseract.image_to_string(curr_crop_thresh, config='--psm 7', lang='7seg')

    cv2.putText(volt_crop_thresh, f'Volt: {volt_text.strip()}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) # Kırmızı
    cv2.putText(curr_crop_thresh, f'Current: {curr_text.strip()}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) # Yeşil

    cv2.imshow('Voltmetre Crop', volt_crop_thresh)
    cv2.imshow('Ampermetre Crop', curr_crop_thresh)

    if cv2.waitKey(1) & 0xFF == 27:
    break

    frame_count += 1

    cap.release()
    cv2.destroyAllWindows() `

    Continue reading...

Compartilhe esta Página