Skip to content

Instantly share code, notes, and snippets.

@CaptainFreak
Created June 5, 2018 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainFreak/5fef6f7da6d260523b48b0c0cd059190 to your computer and use it in GitHub Desktop.
Save CaptainFreak/5fef6f7da6d260523b48b0c0cd059190 to your computer and use it in GitHub Desktop.
Le shubhya maje mar. {Script for evaluating expression from image}
###
# Usage : python ocr.py --image image_name.png --preprocess blur
###
from PIL import Image
import pytesseract
import argparse
import cv2
import os
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())
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)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
print("Original:")
print(text)
print("Expression:")
text = text[:-2]
print(text)
print("Answer:")
print (eval(text))
# show the output images
#cv2.imshow("Image", image)
#cv2.imshow("Output", gray)
#cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment