Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def read_screen():
- spinner = Halo(text='Reading screen', spinner='bouncingBar')
- spinner.start()
- screenshot_file="Screens/to_ocr.png"
- screen_grab(screenshot_file)
- #prepare argparse
- ap = argparse.ArgumentParser(description='HQ_Bot')
- ap.add_argument("-i", "--image", required=False,default=screenshot_file,help="path to input image to be OCR'd")
- ap.add_argument("-p", "--preprocess", type=str, default="thresh", help="type of preprocessing to be done")
- args = vars(ap.parse_args())
- # load the image
- image = cv2.imread(args["image"])
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- if args["preprocess"] == "thresh":
- gray = cv2.threshold(gray, 0, 255,
- cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
- elif args["preprocess"] == "blur":
- gray = cv2.medianBlur(gray, 3)
- # store grayscale image as a temp file to apply OCR
- filename = "Screens/{}.png".format(os.getpid())
- cv2.imwrite(filename, gray)
- # load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file
- pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'
- #ENG
- #text = pytesseract.image_to_string(Image.open(filename))
- #VIET
- text = pytesseract.image_to_string(Image.open(filename), lang='vie')
- os.remove(filename)
- os.remove(screenshot_file)
- # show the output images
- '''cv2.imshow("Image", image)
- cv2.imshow("Output", gray)
- os.remove(screenshot_file)
- if cv2.waitKey(0):
- cv2.destroyAllWindows()
- print(text)
- '''
- spinner.succeed()
- spinner.stop()
- return text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement