Created
May 24, 2019 08:33
-
-
Save JeonghunLee/34a52143b0979e0d32aaba669561be2c 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
import cv2 | |
import sys | |
import pytesseract | |
import numpy as np | |
from matplotlib import pyplot as plt | |
#import matplotlib.pyplot as plt | |
# | |
# /usr/share/tesseract-ocr/4.00/tessdata | |
# | |
def ImageToText(string,img): | |
# psm | |
# 0 = Orientation and script detection (OSD) only. | |
# 1 = Automatic page segmentation with OSD. | |
# 2 = Automatic page segmentation, but no OSD, or OCR. (not implemented) | |
# 3 = Fully automatic page segmentation, but no OSD. (Default) | |
# 4 = Assume a single column of text of variable sizes. | |
# 5 = Assume a single uniform block of vertically aligned text. | |
# 6 = Assume a single uniform block of text. | |
# 7 = Treat the image as a single text line. | |
# 8 = Treat the image as a single word. | |
# 9 = Treat the image as a single word in a circle. | |
# 10 = Treat the image as a single character. | |
# 11 = Sparse text. Find as much text as possible in no particular order. | |
# 12 = Sparse text with OSD. | |
# 13 = Raw line. Treat the image as a single text line, | |
# bypassing hacks that are Tesseract-specific. | |
config = ('-l eng+kor --psm 6') | |
text = pytesseract.image_to_string(img,config=config) | |
print"------start --------(%s)" %(string) | |
print(text) | |
print"------end ---------\n" | |
return text | |
if __name__ == '__main__': | |
if len(sys.argv) < 2 : | |
print('Usage : python ocr_simple.py image.jpg') | |
sys.exit(1) | |
imPath = sys.argv[1] | |
print("Open CV Version"+ cv2.__version__) | |
img = cv2.imread(imPath,cv2.IMREAD_GRAYSCALE) | |
#Global Thresholding (v = 127) | |
ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) | |
#Adaptive Mean Thresholding | |
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2) | |
#Adaptive Gaussian Thresholding | |
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2) | |
#others | |
#blur = cv2.GaussianBlur(img,(5,5),0) | |
#ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) | |
#th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2) | |
#th3 = cv2.Laplacian(th3,cv2.CV_64F) | |
titles = ['Original Image', 'Global Thresholding (v = 127)', | |
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding'] | |
images = [img, th1, th2, th3] | |
for i in xrange(4): | |
data = ImageToText(titles[i],images[i]) | |
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray') | |
plt.title(titles[i]) | |
plt.xlabel(data) | |
plt.xticks([]),plt.yticks([]) | |
plt.show() | |
# ImageToText(titles[0],img) | |
# ImageToText(titles[1],th1) | |
# ImageToText(titles[2],th2) | |
# ImageToText(titles[3],th3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment