Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. def read_screen():
  2. spinner = Halo(text='Reading screen', spinner='bouncingBar')
  3. spinner.start()
  4. screenshot_file="Screens/to_ocr.png"
  5. screen_grab(screenshot_file)
  6.  
  7. #prepare argparse
  8. ap = argparse.ArgumentParser(description='HQ_Bot')
  9. ap.add_argument("-i", "--image", required=False,default=screenshot_file,help="path to input image to be OCR'd")
  10. ap.add_argument("-p", "--preprocess", type=str, default="thresh", help="type of preprocessing to be done")
  11. args = vars(ap.parse_args())
  12.  
  13. # load the image
  14. image = cv2.imread(args["image"])
  15. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  16.  
  17. if args["preprocess"] == "thresh":
  18. gray = cv2.threshold(gray, 0, 255,
  19. cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  20. elif args["preprocess"] == "blur":
  21. gray = cv2.medianBlur(gray, 3)
  22.  
  23. # store grayscale image as a temp file to apply OCR
  24. filename = "Screens/{}.png".format(os.getpid())
  25. cv2.imwrite(filename, gray)
  26.  
  27. # load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file
  28. pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'
  29. #ENG
  30. #text = pytesseract.image_to_string(Image.open(filename))
  31.  
  32. #VIET
  33. text = pytesseract.image_to_string(Image.open(filename), lang='vie')
  34.  
  35. os.remove(filename)
  36. os.remove(screenshot_file)
  37.  
  38. # show the output images
  39.  
  40. '''cv2.imshow("Image", image)
  41. cv2.imshow("Output", gray)
  42. os.remove(screenshot_file)
  43. if cv2.waitKey(0):
  44. cv2.destroyAllWindows()
  45. print(text)
  46. '''
  47. spinner.succeed()
  48. spinner.stop()
  49. return text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement