User:Simon/self directed research/OCR preprocessing: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 1: Line 1:
Pre-processing for OCR:<br>
Pre-processing for OCR:<br>
<code>
# import the necessary packages
#from PIL
import Image
import pytesseract
import argparse
import cv2
import os


    # import the necessary packages
# construct the argument parse and parse the arguments
    #from PIL
ap = argparse.ArgumentParser()
    import Image
ap.add_argument("-i", "--image", required=True,
    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")
help="path to input image to be OCR'd")
    ap.add_argument("-p", "--preprocess", type=str, default="thresh",
ap.add_argument("-p", "--preprocess", type=str, default="thresh",
help="type of preprocessing to be done")
help="type of preprocessing to be done")
    args = vars(ap.parse_args())
args = vars(ap.parse_args())
    # load the example image and convert it to grayscale
 
    image = cv2.imread(args["image"])
# load the example image and convert it to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.imread(args["image"])
    # check to see if we should apply thresholding to preprocess the
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # image
 
    if args["preprocess"] == "thresh":
# check to see if we should apply thresholding to preprocess the
# image
if args["preprocess"] == "thresh":
gray = cv2.threshold(gray, 0, 255,
gray = cv2.threshold(gray, 0, 255,
    cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    # make a check to see if median blurring should be done to remove
 
    # noise
# make a check to see if median blurring should be done to remove
    elif args["preprocess"] == "blur":
# noise
elif args["preprocess"] == "blur":
gray = cv2.medianBlur(gray, 3)
gray = cv2.medianBlur(gray, 3)
    # write the grayscale image to disk as a temporary file so we can
 
    # apply OCR to it
# write the grayscale image to disk as a temporary file so we can
    filename = "{}.png".format(os.getpid())
# apply OCR to it
    cv2.imwrite(filename, gray)
filename = "{}.png".format(os.getpid())
    # load the image as a PIL/Pillow image, apply OCR, and then delete
cv2.imwrite(filename, gray)
    # the temporary file
 
    text = pytesseract.image_to_string(Image.open(filename))
# load the image as a PIL/Pillow image, apply OCR, and then delete
    os.remove(filename)
# the temporary file
    print(text)
text = pytesseract.image_to_string(Image.open(filename))
    # show the output images
os.remove(filename)
    cv2.imshow("Image", image)
print(text)
    cv2.imshow("Output", gray)
 
    cv2.waitKey(0)</code>
# show the output images
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)</code>

Revision as of 16:23, 5 September 2019

Pre-processing for OCR:

  1. import the necessary packages
  2. from PIL

import Image import pytesseract import argparse import cv2 import os

  1. 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())

  1. load the example image and convert it to grayscale

image = cv2.imread(args["image"]) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

  1. check to see if we should apply thresholding to preprocess the
  2. image

if args["preprocess"] == "thresh": gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

  1. make a check to see if median blurring should be done to remove
  2. noise

elif args["preprocess"] == "blur": gray = cv2.medianBlur(gray, 3)

  1. write the grayscale image to disk as a temporary file so we can
  2. apply OCR to it

filename = "{}.png".format(os.getpid()) cv2.imwrite(filename, gray)

  1. load the image as a PIL/Pillow image, apply OCR, and then delete
  2. the temporary file

text = pytesseract.image_to_string(Image.open(filename)) os.remove(filename) print(text)

  1. show the output images

cv2.imshow("Image", image) cv2.imshow("Output", gray) cv2.waitKey(0)