0

I was trying to process a video where I have digits that I want to digitalize. I've copied the code from another answer in here, How can I extract numbers from video frames using Tesseract OCR?, the thing is I can't make it work.

I tried the following code

import cv2
import pytesseract

def getText(filename):
    img = cv2.imread(filename)
    HSV_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    h,s,v = cv2.split(HSV_img)
    v = cv2.GaussianBlur(v, (1,1), 0)
    thresh = cv2.threshold(v, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    cropped_thresh = thresh[120:410, 300:800]
    cv2.imwrite('{}.png'.format(filename),cropped_thresh)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, ksize=(1, 2))
    cropped_thresh = cv2.dilate(cropped_thresh, kernel)
    txt = pytesseract.image_to_string(cropped_thresh, config='--psm 6 digits')
    return txt
   

Here is the original frame of the video
Here is the image before the image_to_string function.

edit: In this image I upload as an example I get as output 17, whereas I want to get 7.7 or at least 77. Also I have very similar images with the same digits, and I get different outputs. For example, the next frame which is identical I get 717 and 4.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
bananegro
  • 1
  • 1
  • 1
    What do you mean you can't make it work? Can you explain to us what is wrong – Chris Mar 02 '23 at 23:49
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 03 '23 at 02:08
  • I've edited the question so it's more precise – bananegro Mar 04 '23 at 00:47

1 Answers1

0

Very important is the quality of your input image and you should only use b/w. You can play with the image parameter a little bit and will see, that the result could be very different:

Not the perfect result, if this is at all possible:

import subprocess
import cv2
import pytesseract

# Image manipulation
# Commands https://imagemagick.org/script/convert.php
mag_img = r'D:\Programme\ImageMagic\magick.exe'
con_bw = r"D:\Programme\ImageMagic\convert.exe"

in_file = r'77.png'
out_file = r'77_bw.png'

process = subprocess.run([con_bw, in_file,"-resize", "22%", "-threshold", "50%", "-brightness-contrast","-15x60", out_file])

# Pytesseract Process
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread(out_file)

custom_config = r'--psm 6 --oem 3 digit=.,1234567890'
text_from_image = pytesseract.image_to_string(img, config=custom_config)
print(text_from_image)

cv2.imshow('image',img)
cv2.waitKey(8000)
cv2.destroyAllWindows()

Output: enter image description here

Hermann12
  • 1,709
  • 2
  • 5
  • 14