|
@@ -0,0 +1,40 @@
|
|
1
|
+# import the necessary packages
|
|
2
|
+from PIL import Image
|
|
3
|
+import pytesseract
|
|
4
|
+import argparse
|
|
5
|
+import cv2
|
|
6
|
+import os
|
|
7
|
+# construct the argument parse and parse the arguments
|
|
8
|
+# ap = argparse.ArgumentParser()
|
|
9
|
+# ap.add_argument("-i", "--image", required=True,
|
|
10
|
+# help="path to input image to be OCR'd")
|
|
11
|
+# ap.add_argument("-p", "--preprocess", type=str, default="thresh",
|
|
12
|
+# help="type of preprocessing to be done")
|
|
13
|
+# args = vars(ap.parse_args())
|
|
14
|
+
|
|
15
|
+# load the example image and convert it to grayscale
|
|
16
|
+image = cv2.imread('data\\test5.PNG')
|
|
17
|
+gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
18
|
+# check to see if we should apply thresholding to preprocess the
|
|
19
|
+# image
|
|
20
|
+# if args["preprocess"] == "thresh":
|
|
21
|
+gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
|
|
22
|
+# make a check to see if median blurring should be done to remove
|
|
23
|
+# noise
|
|
24
|
+# elif args["preprocess"] == "blur":
|
|
25
|
+# gray = cv2.medianBlur(gray, 3)
|
|
26
|
+# write the grayscale image to disk as a temporary file so we can
|
|
27
|
+# apply OCR to it
|
|
28
|
+filename = "{}.png".format(os.getpid())
|
|
29
|
+cv2.imwrite(filename, gray)
|
|
30
|
+
|
|
31
|
+# load the image as a PIL/Pillow image, apply OCR, and then delete
|
|
32
|
+# the temporary file
|
|
33
|
+pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract'
|
|
34
|
+text = pytesseract.image_to_string(Image.open(filename))
|
|
35
|
+os.remove(filename)
|
|
36
|
+print(text)
|
|
37
|
+# show the output images
|
|
38
|
+cv2.imshow("Image", image)
|
|
39
|
+cv2.imshow("Output", gray)
|
|
40
|
+cv2.waitKey(0)
|