https://github.com/UB-Mannheim/tesseract/wiki
Download tessaract
OCR - Optical Character Recognition κ΄ν λ¬Έμ μΈμ
!pip install pytesseract
import pytesseract
# r : λ€μ μ€λ λ¬Έμμ΄μ κ·Έλλ‘ κΈ°μ΅
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
print("C:\Program Files\Tesseract-OCR\tesseract.exe")
import cv2
img = cv2.imread('./data/text.png')
text = pytesseract.image_to_string(img, lang = 'eng')
print(text)
import cv2
img = cv2.imread('./data/story.png')
text = pytesseract.image_to_string(img, lang = 'kor')
print(text)
import cv2
img = cv2.imread('./data/invoice.jpg')
text = pytesseract.image_to_string(img, lang = 'kor')
print(text)
# νλ°±μΌλ‘ λ§λ€κΈ°
import cv2
img = cv2.imread('./data/invoice.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('img',img)
cv2.imshow('img_gray',img_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
# νλ°±μΌλ‘ λ§λ€κΈ°
import cv2
img = cv2.imread('./data/invoice.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, img_binary = cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY)
text = pytesseract.image_to_string(img_binary, lang = 'kor')
cv2.imshow('img',img)
cv2.imshow('img_gray',img_gray)
cv2.imshow('img_binary',img_binary)
cv2.waitKey(0)
cv2.destroyAllWindows()
# μ΄μ§ν μ
κ·Έλ μ΄λ
import cv2
img = cv2.imread('./data/sodoku.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, img_binary = cv2.threshold(img_gray,50,255,cv2.THRESH_BINARY)
cv2.imshow('img',img)
cv2.imshow('img_gray',img_gray)
cv2.imshow('img_binary',img_binary)
cv2.waitKey(0)
cv2.destroyAllWindows()
# μ΄μ§ν μ
κ·Έλ μ΄λ = μ μν μ΄μ§ν λ°©λ²
# λΉμ μν΄μ μ΄λ―Έμ§μ κ°μ΄ κ· μΌνμ§ μμλ μ¬μ©νλ λ°©λ²
# μμμ λλ μ μμλ³λ‘ μ΄μ§νλ₯Ό μ§ν > λ¬Έν±κ° (μκ³κ°)μ μμμ κ³μ°
import cv2
img = cv2.imread('./data/sodoku.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, img_binary = cv2.threshold(img_gray,50,255,cv2.THRESH_BINARY)
binary = cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 9,5)
cv2.imshow('img',img)
cv2.imshow('img_gray',img_gray)
cv2.imshow('img_binary',img_binary)
cv2.imshow('binary',binary)
cv2.waitKey(0)
cv2.destroyAllWindows()
# μ€μμ μ μΈν μμμ blur μ²λ¦¬
import cv2
video = cv2.VideoCapture(0)
while video.isOpened:
ret, img = video.read()
if not ret:
break
width = img.shape[1]
height = img.shape[0]
img = cv2.flip(img,1)
# μ¬κ°ν μμͺ½ μμμ κ°
tmp = img[int(height/6) :int(height/6*5),int(width/3): int(width/3*2) ]
# μ΄λ―Έμ§ μ 체 blur μ²λ¦¬
img = cv2.blur (img, (20,20))
# μ€μ μμμ blur μ²λ¦¬λκΈ° μ κ° μ§μ΄λκΈ°
img[int(height/6) :int(height/6*5),int(width/3): int(width/3*2) ]= tmp
k = cv2.waitKey(30)
if k == 49:
break
cv2.imshow('img',img)
video.release()
cv2.destroyAllWindows()
## μ€μμ μ μΈν μμμ blur μ²λ¦¬
import cv2
video = cv2.VideoCapture(0)
while video.isOpened:
ret, img = video.read()
if not ret:
break
width = img.shape[1]
height = img.shape[0]
#img = cv2.flip(img,1)
# μ¬κ°ν μμͺ½ μμμ κ°
tmp = img[int(height/6) :int(height/6*5),int(width/3): int(width/3*2) ]
# μ΄λ―Έμ§ μ 체 blur μ²λ¦¬
img = cv2.blur (img, (20,20))
# μ€μ μμμ blur μ²λ¦¬λκΈ° μ κ° μ§μ΄λκΈ°
img[int(height/6) :int(height/6*5),int(width/3): int(width/3*2) ]= tmp
# blur μμ λ°λ리 νμνκΈ°
cv2.rectangle(img,(int(width/3), int(height/6)),# μ’μλ¨
(int(width/3*2), int(height/6*5)), # μ°νλ¨
(255,255,255), 2 #μμ, λκ»
)
#1, μμ μμ μλ κ°μ μ¬μ©ν΄μ OCRνκΈ°
#2. μνλ κ² (μ΄λ¦)μ΄ μμΌλ©΄ μΉ΄λ©λΌ μ’
λ£νκΈ°
k = cv2.waitKey(30)
if k == 49:
break
elif k == 50:
# μ΄μ§νλ₯Ό νκΈ° μν΄μ νλ°±μΌλ‘ λ³ννκΈ°
gray = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)
# μ μν μ΄μ§ν νκΈ°
binary = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 9,5)
import numpy as np
kernel = np.ones((3,3),np.uint8)
close = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
text = pytesseract.image_to_string(binary, lang ='kor')
print(text)
import matplotlib.pyplot as plt
plt.imshow(binary,cmap ='gray')
plt.show()
cv2.imshow('img',img)
video.release()
cv2.destroyAllWindows()
## μ€μμ μ μΈν μμμ blur μ²λ¦¬
import cv2
video = cv2.VideoCapture(0)
while video.isOpened:
ret, img = video.read()
if not ret:
break
width = img.shape[1]
height = img.shape[0]
#img = cv2.flip(img,1)
# μ¬κ°ν μμͺ½ μμμ κ°
tmp = img[int(height/6) :int(height/6*5),int(width/3): int(width/3*2) ]
# μ΄λ―Έμ§ μ 체 blur μ²λ¦¬
img = cv2.blur (img, (20,20))
# μ€μ μμμ blur μ²λ¦¬λκΈ° μ κ° μ§μ΄λκΈ°
img[int(height/6) :int(height/6*5),int(width/3): int(width/3*2) ]= tmp
# blur μμ λ°λ리 νμνκΈ°
cv2.rectangle(img,(int(width/3), int(height/6)),# μ’μλ¨
(int(width/3*2), int(height/6*5)), # μ°νλ¨
(255,255,255), 2 #μμ, λκ»
)
#1, μμ μμ μλ κ°μ μ¬μ©ν΄μ OCRνκΈ°
#2. μνλ κ² (μ΄λ¦)μ΄ μμΌλ©΄ μΉ΄λ©λΌ μ’
λ£νκΈ°
k = cv2.waitKey(30)
if k == 49:
break
elif k == 50:
# μ΄μ§νλ₯Ό νκΈ° μν΄μ νλ°±μΌλ‘ λ³ννκΈ°
gray = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)
# μ μν μ΄μ§ν νκΈ°
binary = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 9,5)
import numpy as np
kernel = np.ones((3,3),np.uint8)
# νλΆλΆ ν½μ°½ > κ²μ λ
Έμ΄μ¦ λΆλΆ μ€μ΄κΈ°>κ²μ κΈμ¨ κ΅΅κΈ°κ° μ€μ΄
dilation = cv2.dilate(binary, kernel, iterations = 1)
erosion = cv2.erode(dilation, kernel, iterations = 2)
text = pytesseract.image_to_string(binary, lang ='kor')
print(text)
# μνλ κ°(μ΄λ¦)μ΄ μμΌλ©΄ μΉ΄λ©λΌ μ’
λ£νκΈ°
if 'ν°ν°' in text:
break
cv2.imshow('img',img)
video.release()
cv2.destroyAllWindows()