Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import pytesseract
  4. from PIL import Image
  5.  
  6. # Path of working folder on Disk
  7. src_path = r"C:/Users/Ediz/Pictures/CODTEST/OCR/"
  8. pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
  9.  
  10.  
  11. def get_string(img_path):
  12. # Read image with opencv
  13. img = cv2.imread(img_path)
  14. # Convert to gray
  15. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  16.  
  17. # Apply dilation and erosion to remove some noise
  18. kernel = np.ones((1, 1), np.uint8)
  19. img = cv2.dilate(img, kernel, iterations=1)
  20. img = cv2.erode(img, kernel, iterations=1)
  21.  
  22. # Write image after removed noise
  23. cv2.imwrite(src_path + "removed_noise.png", img)
  24.  
  25. # Apply threshold to get image with only black and white
  26. #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
  27.  
  28. # Write the image after apply opencv to do some ...
  29. #cv2.imwrite(src_path + "thres.png", img)
  30.  
  31. # Recognize text with tesseract for python
  32. result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
  33.  
  34. # Remove template file
  35. # os.remove(temp)
  36.  
  37. return result
  38.  
  39.  
  40. print('--- Start recognize text from image ---')
  41. print(cv2.__version__)
  42.  
  43. print(get_string(src_path + "2.png"))
  44.  
  45. print("------ Done -------")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement