Issue
i can not extract text with reliable accuracy from an image
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image_path = 'crop.png'
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply more aggressive thresholding and additional morphology operations
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
custom_config = r'--oem 1 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ'
text = pytesseract.image_to_string(thresh, config=custom_config)
print(text)
Here is my code i am getting reliabe output check out here
https://colab.research.google.com/drive/11utvWD3s6DqqGZQEnk5cKIAj46ZLsF5y?usp=sharing
Solution
I get the right output, if I use a b&w image, resized to 43%:
import cv2
import pytesseract
img = cv2.imread('crop.png',cv2.IMREAD_UNCHANGED)
print(img.shape) # Print image shape
# cv2.imshow("Original", img)
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 65, 255, cv2.THRESH_BINARY)
print(thresh)
# resize image
scale_percent = 43 # percent of original size
width = int(blackAndWhiteImage.shape[1] * scale_percent / 100)
height = int(blackAndWhiteImage.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(blackAndWhiteImage, dim, interpolation = cv2.INTER_AREA)
# OCR resized Black & White image
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
custom_config = r'--psm 6 --oem 3 -c tessedit_char_whitelist=ABCDEFTGHIJKLMNOPQRSTUVWXYZ'
tex = pytesseract.image_to_string(resized, config=custom_config)
print(tex)
### Display resized image
cv2.imshow("resized", resized)
# Save the resized image
cv2.imwrite("resized_crop.png", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
###
Output:
TLYTILAER
NSNOPMARC
AFSENNACT
DHOMEDZEW
EMSINODEH
PKELIZAWS
CCJIXCZTN
AINTENDKZ
DETROBAPB
Answered By - Hermann12
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.