Opencv 그림의 OCR 식별 실전 예시
1. 그림 변환
0, 모듈 가져오기
관련 함수를 가져오고 오류가 발생하면 pip install 함수 이름을 직접 가져옵니다.
import numpy as np
import argparse
import cv2
매개변수 초기화
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image to be scanned")
args = vars(ap.parse_args())
Parameters:--image images\page.jpg
1. resize 함수 다시 쓰기
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation=inter)
return resized
2. 사전 처리
그림을 읽고 크기를 재설정하고 축소 배수를 계산합니다.그레이스케일링, 고스 필터링 및 Canny 윤곽 추출
image = cv2.imread(args["image"])
ratio = image.shape[0] / 500.0
orig = image.copy()
image = resize(orig, height = 500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)
3. 테두리 검출
윤곽선을 검사하고 정렬하며 윤곽선을 훑어보십시오.
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]#
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]# 5
#
for c in cnts:
#
peri = cv2.arcLength(c, True)# ,C ,True
#(C ,epslion line segment , )
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# 4
if len(approx) == 4:
screenCnt = approx
break
4. 원근 변환
근사 윤곽 그리기, 투시 변환, 이치 처리
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)#
#
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(warped, 100, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('scan.jpg', ref)
2. OCR 식별
0, tesseract-ocr 설치
링크:다운로드
환경 변수, 시스템 변수의 Path에 설치 경로를 추가합니다. 예를 들어 E:\Program Files(x86)\Tesseract-OCR
tesseract -v# ,
tesseract XXX.png result#
pip install pytesseract#
python 설치 경로에 있는 python 파일을 엽니다. 예를 들어 C:\ProgramData\Anaconda3\Lib\site-packages\pytesseract\pytesseract.pytesseract_cmd를 절대 경로로 수정하면 됩니다. 예를 들어:tesseract_cmd = ‘C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
1. 모듈 가져오기
from PIL import Image
import pytesseract
import cv2
import os
2. 사전 처리
그림 읽기, 그레이스케일링, 필터링
image = cv2.imread('scan.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 3)
3. 결과 출력
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
print(text)
os.remove(filename)
Opencv 이미지의 OCR 식별에 대한 실전 예시를 소개합니다. Opencv 이미지와 관련된 OCR 식별 내용은 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보십시오. 앞으로 많은 응원 부탁드립니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
손이 서투른 엔지니어라도 iPad Pro + Apple Pencil이라면 그림이나 그림을 잘 그릴 수 있다! 정말 신 디바이스이므로 사 밖에 없으면 수백만회라도 외치고 싶다!손이 서투른 엔지니어라도 iPad Pro + Apple Pencil이라면 그림이나 그림을 잘 그릴 수 있다! 정말 신 디바이스이므로 사 밖에 없으면 수백만회라도 외치고 싶다! 자신의 글자가 굉장히 잘 된 것 같은 감...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.