.NET의 이미지에서 텍스트를 추출하고 싶습니다 python
. 이를 위해 pytesseract
. 이미지에서 텍스트 추출을 시도했을 때 결과가 만족스럽지 않았습니다. 나는 또한 이것을 거쳐 아래 나열된 모든 기술을 구현했습니다. 그러나 그것은 잘 수행되지 않는 것 같습니다.
영상:
암호:
import pytesseract
import cv2
import numpy as np
img = cv2.imread('D:\\wordsimg.png')
img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1,1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.threshold(cv2.medianBlur(img, 3), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
txt = pytesseract.image_to_string(img ,lang = 'eng')
txt = txt[:-1]
txt = txt.replace('\n',' ')
print(txt)
산출:
t hose he large form might light another us should took mountai house n story important went own own thought girl over family look some much ask the under why miss point make mile grow do own school was
원치 않는 공간이 하나라도 많은 비용이들 수 있습니다. 결과가 100 % 정확하기를 바랍니다. 어떤 도움을 주시면 감사하겠습니다. 감사!
크기를 1.2에서 2로 변경하고 모든 전처리를 제거했습니다. psm 11 및 psm 12로 좋은 결과를 얻었습니다.
import pytesseract
import cv2
import numpy as np
img = cv2.imread('wavy.png')
# img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
img = cv2.resize(img, None, fx=2, fy=2)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1,1), np.uint8)
# img = cv2.dilate(img, kernel, iterations=1)
# img = cv2.erode(img, kernel, iterations=1)
# img = cv2.threshold(cv2.medianBlur(img, 3), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
cv2.imwrite('thresh.png', img)
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'
for psm in range(6,13+1):
config = '--oem 3 --psm %d' % psm
txt = pytesseract.image_to_string(img, config = config, lang='eng')
print('psm ', psm, ':',txt)
이 config = '--oem 3 --psm %d' % psm
줄은 문자열 보간 (%) 연산자 를 %d
사용하여 정수 (psm)로 바꿉니다. 정확히 무엇을하는지 모르겠지만 oem
사용하는 습관이 생겼습니다. psm
이 답변의 끝에서 더 자세히 알아보십시오 .
psm 11 : those he large form might light another us should name
took mountain story important went own own thought girl
over family look some much ask the under why miss point
make mile grow do own school was
psm 12 : those he large form might light another us should name
took mountain story important went own own thought girl
over family look some much ask the under why miss point
make mile grow do own school was
psm
페이지 분할 모드의 약자입니다. 다른 모드가 무엇인지 정확히 모르겠습니다. 설명에서 코드가 무엇인지 알 수 있습니다. 목록을 가져올 수 있습니다.tesseract --help-psm
Page segmentation modes:
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.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다