Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import os
  2. import argparse
  3. import numpy as np
  4. import pytesseract
  5. import cv2
  6. from PIL import Image
  7.  
  8. filename = "image_processed.png"
  9.  
  10. def ProcessImage():
  11. # load the example image and convert it to grayscale
  12. image_path = "image_raw.png"
  13. image = cv2.imread(image_path)
  14. oryginal = cv2.imread(image_path)
  15. # convert to grayscale
  16. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  17. # get image shape
  18. height, width = image.shape
  19. # bitwise not to make negative
  20. # image = cv2.bitwise_not(image)
  21.  
  22. thresh = True
  23. blur = True
  24. # check to see if we should apply thresholding to preprocess the image
  25. if thresh:
  26. image = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  27. # make a check to see if median blurring should be done to remove noise
  28. if blur:
  29. image = cv2.medianBlur(image, 3)
  30. image = cv2.GaussianBlur(image, (3, 3), 0)
  31.  
  32. # write the grayscale image to disk as a temporary file so we can apply OCR to it
  33. cv2.imwrite(filename, image)
  34.  
  35. # ichl1=int(round(height*(.0)))
  36. # ichu1=int(round(height*(.5)))
  37. # icwl1=int(round(width*(.1770)))
  38. # icwu1=int(round(width*(.3125)))
  39.  
  40. # img_crop2 = image[width:, :int(height/2)]
  41. # img_crop3 = image[:int(width/2), int(height/2):]
  42. # img_crop4 = image[width:, int(height/2):]
  43.  
  44. cropList = [
  45. image[:int(height*0.33), :],
  46. image[int(height*0.33):int(height*0.66), :],
  47. image[int(height*0.66):, :]
  48. ]
  49.  
  50. image_part = image[50:150, 20:120]
  51. s = image_part.tostring()
  52. # cv2.imwrite("part.png", image_part)
  53. # print(s)
  54. out = open("output.txt", "w")
  55. for line in image_part:
  56. s = ' '.join(str(x) for x in line)
  57. out.write(s + "\n")
  58.  
  59. # cropList = [
  60. # img_crop1 = image[:width/2, :height/2],
  61. # img_crop2 = image[width:, :height/2],
  62. # img_crop3 = image[:width/2, height/2:],
  63. # img_crop4 = image[width:, height/2:]
  64. # ]
  65.  
  66. # imgList = []
  67.  
  68. # i = 0
  69. # for img in cropList:
  70. # filen = "{}.png".format(i)
  71. # i += 1
  72. # cv2.imwrite(filen, img)
  73. pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
  74. text = pytesseract.image_to_string(Image.open(filename))
  75. print("'" + text + "'")
  76. # for img in cropList:
  77. # text = pytesseract.image_to_string(img)
  78. # print("'" + text + "'")
  79.  
  80. def Extract():
  81. pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
  82. text = pytesseract.image_to_string(Image.open(filename))
  83. return text
  84.  
  85. # if (not os.path.exists(filename)):
  86. # ProcessImage()
  87.  
  88. ProcessImage()
  89.  
  90.  
  91. # t = Extract()
  92. # print(t)
  93.  
  94.  
  95. # word = ""
  96. # for l in text:
  97. # if l.isspace():
  98. # print(word)
  99. # word = ""
  100. # else:
  101. # word += l
  102. # print (word)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement