Advertisement
slik1977

OCR 1.1

Dec 24th, 2021
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import pytesseract
  2. import cv2
  3.  
  4. pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
  5.  
  6. import numpy as np
  7.  
  8. img = cv2.imread('test.png')
  9.  
  10. # get grayscale image
  11. def get_grayscale(image):
  12.     return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  13.  
  14. # noise removal
  15. def remove_noise(image):
  16.     return cv2.medianBlur(image, 5)
  17.  
  18. # thresholding
  19. def thresholding(image):
  20.     return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY)[1]
  21.  
  22. # dilation
  23. def dilate(image):
  24.     kernel = np.ones((5, 5), np.uint8)
  25.     return cv2.dilate(image, kernel, iterations=1)
  26.  
  27. # erosion
  28. def erode(image):
  29.     kernel = np.ones((5, 5), np.uint8)
  30.     return cv2.erode(image, kernel, iterations=1)
  31.  
  32. # opening - erosion followed by dilation
  33. def opening(image):
  34.     kernel = np.ones((5, 5), np.uint8)
  35.     return cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
  36.  
  37. # canny edge detection
  38. def canny(image):
  39.     return cv2.Canny(image, 100, 200)
  40.  
  41. img = get_grayscale(img)
  42. #img = remove_noise(img)
  43. #img = thresholding(img)
  44. #img = dilate(img)
  45. #img = erode(img)
  46. #img = opening(img)
  47. #img = canny(img)
  48.  
  49. # selection and output of the found words on the picture
  50. custom_config = r'-c tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyz-\ QWERTYUIOPASDFGHJKLZXCVBNM --psm 12'
  51. text = pytesseract.image_to_string(img, config=custom_config)
  52. words = text.split()
  53.  
  54. with open('WordsFromWindowsScreen.txt', 'w') as f:
  55.     for word in words:
  56.         if (len(word) > 2): f.write(word + " ")
  57. data = pytesseract.image_to_data(img, config=custom_config)
  58. for i, item in enumerate(data.splitlines()):
  59.     if i == 0:
  60.         continue
  61.     item = item.split()
  62.     try:
  63.         x, y, w, h = int(item[6]), int(item[7]), int(item[8]), int(item[9])
  64.         cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 1)
  65.         cv2.putText(img, item[11], (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0), 1)
  66.     except IndexError:
  67.         print("missed")
  68.  
  69. # output of the result
  70. cv2.imshow("Output", img)
  71. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement