Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pytesseract
- import cv2
- pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
- import numpy as np
- img = cv2.imread('test.png')
- img = cv2.resize(img, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_CUBIC)
- # Convert to gray
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- # Apply dilation and erosion to remove some noise
- kernel = np.ones((1, 1), np.uint8)
- img = cv2.dilate(img, kernel, iterations=1)
- img = cv2.erode(img, kernel, iterations=1)
- # Apply blur to smooth out the edges
- #img = cv2.GaussianBlur(img, (5, 5), 0)
- #img = cv2.bilateralFilter(img,9,75,75)
- img = cv2.medianBlur(img, 3)
- # Apply threshold to get image with only b&w (binarization)
- img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
- cv2.threshold(img,127,255,cv2.THRESH_BINARY)
- cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
- # selection and output of the found words on the picture
- custom_config = r'-c tessedit_char_whitelist=!$&()-./0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\_abcdefghijklmnopqrstuvwxyz --oem 3 --psm 12 '
- text = pytesseract.image_to_string(img, config=custom_config)
- words = text.split()
- with open('WordsFromWindowsScreen.txt', 'w') as f:
- for word in words:
- f.write(word + " ")
- data = pytesseract.image_to_data(img, config=custom_config)
- for i, item in enumerate(data.splitlines()):
- if i == 0:
- continue
- item = item.split()
- try:
- x, y, w, h = int(item[6]), int(item[7]), int(item[8]), int(item[9])
- cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 1)
- cv2.putText(img, item[11], (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0), 1)
- except IndexError:
- continue
- # output of the result
- cv2.imshow("Output", img)
- cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement