0

I am a newbie on OCR manipulation and extraction data from images. After searching for solution I did find some code but it didn't work for my use case, it didn't extract correctly all characters, at most 2 of them.

I want to get the characters on this image:

example

I tried this solution:

image = cv2.imread('./images/screenshot_2023_11_16_15_41_24.png')

# Assuming 4 characters in a 36x9 image
char_width = image.shape[1] // 4
char_height = image.shape[0]

characters = []
characters_slices = [(0, 9), (9, 18), (18, 27), (27, 36)]  # Adjust based on your image
for start, end in characters_slices:
    char = image[0:char_height, start:end]
    characters.append(char)

# Perform OCR on each character
extracted_text = ""
for char in characters:
    char_text = pytesseract.image_to_string(char, config='--psm 10 --oem 3 -c char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
    extracted_text += char_text.strip() + " "

print("Extracted Text:", extracted_text)

Output would be: 'H9FA'

Thanks.

1 Answer 1

0

I use ImageMagick. You need a good black & white picture in the right size:

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'alphaNumerical.png'
out_file = r'alphaNumerical_bw.png'

# Play with black and white and contrast for better results
process = subprocess.run([con_bw, in_file, "-resize", "350%","-threshold","80%","-rotate", "-2", out_file])

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

# Parameters see tesseract doc 
custom_config = r'--psm 11 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890' 

tex = pytesseract.image_to_string(img, config=custom_config)
print(tex)

with open("cartootn.txt", 'w') as f:
    f.writelines(tex)

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

Output: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.