3
\$\begingroup\$

This is my first ever Python program. It checks every frame of a video file with Tesseract then uses OCR to recognise numbers in that frame and prints it to the console.

import pytesseract
import os
import cv2
import openpyxl
from numpy import interp
import numpy as np

# Video URL
TEST_VID = cv2.VideoCapture("VET2.mp4")
READING, IMG = TEST_VID.read()

# Frame Number
INDEX = 0

# Colours
black = [0, 0, 0]


while READING:

    TEST_VID.set(1, INDEX)
    READING, IMG = TEST_VID.read()
    RET, FRAME = TEST_VID.read()

    # If I want to print images
    # print("new frame: ", READING)
    # if not RET:
    #     break

    # # Assign a name for the file
    # NAME = "./image_frames/frame" + str(INDEX) + ".png"

    # # Assign the print statement
    # print("Extracting frames..." + NAME)
    # cv2.imwrite(NAME, FRAME)

    pixel = FRAME[2100, 1800]

    # If a pixel is black, use these values
    if np.all(pixel == black):

        # Image coordinates [y1:y2, x1:x2]
        speed = FRAME[2775:2883, 1199:1486]
        rpm = FRAME[2948:3014, 1190:1500]
        gear = FRAME[3187:3248, 1359:1415]

    # Tesseract directory
    pytesseract.pytesseract.tesseract_cmd = (
        r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    )

    # Print KPH to console
    print(
        "Speed = "
        + pytesseract.image_to_string(
            speed,
            lang="formula1",
            config="--psm 7 -c tessedit_char_whitelist=0123456789",
        )
    )
    # cv2.imshow("speed", speed)
    # cv2.waitKey(0)

    # Print RPM to console
    print(
        "RPM = "
        + pytesseract.image_to_string(
            rpm,
            lang="formula1",
            config="--psm 7 -c tessedit_char_whitelist=0123456789",
        )
    )
    # cv2.imshow("rpm", rpm)
    # cv2.waitKey(0)

    # Print Gear to console
    print(
        "Gear = "
        + pytesseract.image_to_string(
            gear,
            lang="formula1",
            config="--psm 7 -c tessedit_char_whitelist=0123456789",
        )
    )

    # cv2.imshow("gear", gear)
    # cv2.waitKey(0)

    print(INDEX)
    INDEX += 1
\$\endgroup\$
3
  • 1
    \$\begingroup\$ What version of python is this written in? \$\endgroup\$ – Linny Feb 20 '20 at 19:25
  • \$\begingroup\$ @Linny Python 3.7.3 \$\endgroup\$ – Dev Feb 20 '20 at 19:26
  • 1
    \$\begingroup\$ Welcome to Code Review! I rephrased your title to better follow the guidelines in How do I ask a good question? from the Help Center. An expressive title makes your question more appealing to reviewers. \$\endgroup\$ – AlexV Feb 20 '20 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.