ocr.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import tkinter as tk
  3. import pytesseract
  4. import cv2
  5. import pyperclip
  6. import numpy as np
  7. from pytesseract import Output
  8. # import argparse
  9. # import pyautogui as gui
  10. # from threading import Thread, Event
  11. # from PIL import Image, ImageOps, ImageEnhance
  12. # from time import sleep
  13. # thread = Thread(target=gui.prompt)
  14. def thin_font(image):
  15. image = cv2.bitwise_not(image)
  16. kernel = np.ones((2, 2), np.uint8)
  17. image = cv2.erode(image, kernel, iterations=1)
  18. image = cv2.bitwise_not(image)
  19. return (image)
  20. def thick_font(image):
  21. image = cv2.bitwise_not(image)
  22. kernel = np.ones((2, 2), np.uint8)
  23. image = cv2.dilate(image, kernel, iterations=1)
  24. image = cv2.bitwise_not(image)
  25. return (image)
  26. def noise_removal(image):
  27. kernel = np.ones((1, 1), np.uint8)
  28. image = cv2.dilate(image, kernel, iterations=1)
  29. kernel = np.ones((1, 1), np.uint8)
  30. image = cv2.erode(image, kernel, iterations=1)
  31. image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
  32. image = cv2.medianBlur(image, 3)
  33. return (image)
  34. def process_tess(file):
  35. image = cv2.imread(file)
  36. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  37. # gray = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  38. # gray = thin_font(gray)
  39. # _thresh, im_bw = cv2.threshold(gray, 210, 230, cv2.THRESH_BINARY)
  40. results = pytesseract.image_to_data(gray, output_type=Output.DICT)
  41. text = '\n'.join(results['text'])
  42. return text
  43. def main_tess(ext='.png'):
  44. im_files = [f for f in os.listdir() if f.endswith(ext)]
  45. im_files.sort(key=lambda x: os.stat(os.path.join(x)).st_mtime, reverse=True)
  46. text = process_tess(im_files[0])
  47. pyperclip.copy(text)
  48. root = tk.Tk()
  49. T = tk.Text(root, height=10, width=50)
  50. T.pack()
  51. T.insert(tk.END, text)
  52. tk.mainloop()
  53. # os.remove(im_files[0])
  54. # gui.prompt(title='ocr', default=text)
  55. # parser = argparse.ArgumentParser()
  56. # parser.add_argument('-file', type=str, help='name of the scanned file to be converted',
  57. # default=img_files[0])
  58. # parser.add_argument('-p', '--preprocess', type=str, default='thresh',
  59. # help='type of preprocessing to be done')
  60. # args = parser.parse_args()
  61. # image = cv2.imread(args.file)
  62. # rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  63. # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  64. # if args.preprocess == "thresh":
  65. # gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  66. # elif args.preprocess == "blur":
  67. # gray = cv2.medianBlur(gray, 3)
  68. # cv2.imwrite('new_image.png', gray)
  69. # print('tesseract version {}'.format(pytesseract.get_tesseract_version()))
  70. # print('processing', args.file)
  71. # results = pytesseract.image_to_data(gray, output_type=Output.DICT)
  72. # text = ' '.join(results['text'])
  73. # pyperclip.copy(text)
  74. # gui.prompt(title='ocr', default=text)
  75. # text = pytesseract.image_to_string(gray)
  76. if __name__ == '__main__':
  77. main_tess()