Created
May 24, 2021 11:06
-
-
Save kaskichandrakant/5123edb9be568fd755454ffbb532e339 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import pytesseract | |
import argparse | |
import cv2 | |
import os | |
# construct the argument parse and parse the arguments | |
ap = argparse.ArgumentParser() | |
ap.add_argument("-i", "--image", required=True, | |
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()) | |
# from PIL import Image | |
# import pytesseract | |
# image = args["image"] | |
# text = pytesseract.image_to_string(Image.open(image), lang="eng") | |
# print(text) | |
# this much code is enough to recognise typed text with white background | |
# load the example image and convert it to grayscale | |
image = cv2.imread(args["image"]) | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
# check to see if we should apply thresholding to preprocess the | |
# image | |
if args["preprocess"] == "thresh": | |
gray = cv2.threshold(gray, 0, 255, | |
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] | |
# make a check to see if median blurring should be done to remove | |
# noise | |
elif args["preprocess"] == "blur": | |
gray = cv2.medianBlur(gray, 3) | |
# write the grayscale image to disk as a temporary file so we can | |
# apply OCR to it | |
filename = "{}.png".format(os.getpid()) | |
cv2.imwrite(filename, gray) | |
text = pytesseract.image_to_string(Image.open(filename)) | |
os.remove(filename) | |
print(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment