how do I get rid of the pixels so that pytesseract gives the text correctly

  Kiến thức lập trình

I do analytics for promotional codes on streams, and I need it to display accurately. However, everything is not so rosy.

In the picture, it should have displayed “YLJ332JHNJ6J”enter image description here. And this is what it shows me:
Hope someone can help. I will be glad to everyone.enter image description here
My code is:

import cv2
import numpy as np
import pytesseract
import mss
import os
import pyautogui
import pyperclip

monitor = {"top": 422, "left": 1140, "width": 230, "height": 35}

notification_pos = None
pytesseract.pytesseract.tesseract_cmd = 'D:/Tesseract/tesseract.exe'  # your path may be different
with mss.mss() as sct:
    while True:
        frame = np.array(sct.grab(monitor))
        hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        sensitivity = 15
        lower_white = np.array([0, 154, 0])
        upper_white = np.array([179, 255, 255])
        mask=cv2.inRange(hsv_image,lower_white,upper_white)
        invert = 255 - mask
        invert = cv2.medianBlur(invert, 5) 
        text = pytesseract.image_to_string(invert, lang='eng', config= '--oem 3, --psm 7')
        text = pytesseract.image_to_string(invert, lang='eng', config= '--oem 3, --psm 7')
        text_without_newline = text.replace('n', '').replace(" ", "")

        if text != None and len(text_without_newline) == 12:
            print(text_without_newline)
            pyperclip.copy(text_without_newline)
                
        cv2.imshow('Screen', invert)
    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

cv2.destroyAllWindows()

New contributor

Flayer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT