質問編集履歴
1
「試したこと」を追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -106,6 +106,105 @@
|
|
106
106
|
|
107
107
|
print(result)
|
108
108
|
|
109
|
+
```
|
109
110
|
|
111
|
+
### 試したこと
|
110
112
|
|
113
|
+
下のコードのように
|
114
|
+
pdfをページごとに分割した後に画像化をすると上手くいくので、おそらくJPEG化あたりで何かを間違えているのがと思いますが、原因が分からず困っている状態です。
|
115
|
+
|
116
|
+
```python
|
117
|
+
# プログラム|ライブラリ設定
|
118
|
+
import PyPDF2
|
119
|
+
import pathlib
|
120
|
+
import os
|
121
|
+
|
122
|
+
from pdf2image import convert_from_path
|
123
|
+
from PIL import Image
|
124
|
+
import sys
|
125
|
+
from pyocr import pyocr
|
126
|
+
from pyocr import builders
|
127
|
+
import cv2
|
128
|
+
import numpy as np
|
129
|
+
import pandas as pd
|
130
|
+
|
131
|
+
file = './9510.pdf'
|
132
|
+
scan = file.replace('.pdf','')
|
133
|
+
|
134
|
+
# インストール済みのTesseractのパスを通す
|
135
|
+
path_tesseract = r"C:\Users\AppData\Local\Programs\Tesseract-OCR"
|
136
|
+
if path_tesseract not in os.environ["PATH"].split(os.pathsep):
|
137
|
+
os.environ["PATH"] += os.pathsep + path_tesseract
|
138
|
+
|
139
|
+
# OCRエンジンの取得
|
140
|
+
tools = pyocr.get_available_tools()
|
141
|
+
#print(tools)
|
142
|
+
tool = tools[0]
|
143
|
+
|
144
|
+
# プログラム2|フォルダ内のPDFを全て取得
|
145
|
+
curdir = os.getcwd()
|
146
|
+
|
147
|
+
# プログラム4|分割したPDFを保管するためのフォルダ作成
|
148
|
+
path = os.path.join(curdir, scan)
|
149
|
+
if not os.path.isdir(path):
|
150
|
+
os.makedirs(path)
|
151
|
+
|
152
|
+
# プログラム5|PDFを分割
|
153
|
+
pdf = PyPDF2.PdfFileReader(file)
|
154
|
+
for page in range(pdf.numPages):
|
155
|
+
print(page)
|
156
|
+
newpdf = PyPDF2.PdfFileWriter()
|
157
|
+
newpdf.addPage(pdf.getPage(page))
|
158
|
+
|
159
|
+
# PDFをページごとに分割
|
160
|
+
pageNo = format(page, '0>3')
|
161
|
+
splitpdf = os.path.join(path, f'{pageNo}.pdf')
|
162
|
+
with open(splitpdf, 'wb') as f:
|
163
|
+
newpdf.write(f)
|
164
|
+
|
165
|
+
# 3.pdfからjpgへの変換
|
166
|
+
img = convert_from_path(splitpdf)
|
167
|
+
filename = splitpdf.replace(".pdf",".jpg")
|
168
|
+
#print(img)
|
169
|
+
for images in img:
|
170
|
+
images.save(filename)
|
171
|
+
|
172
|
+
|
173
|
+
filename0 = splitpdf.replace(".pdf","")
|
174
|
+
#画像を切り取る
|
175
|
+
imp_crop = Image.open(filename).crop((900,200,1600,400))
|
176
|
+
imp_crop.save(filename)
|
177
|
+
|
178
|
+
#画像の下線を消す
|
179
|
+
img00 = cv2.imread(filename)
|
180
|
+
##画像をグレースケールに変換
|
181
|
+
gray = cv2.cvtColor(img00, cv2.COLOR_BGR2GRAY)
|
182
|
+
#cv2.imwrite(filename0+"_gray.jpg", gray)
|
183
|
+
|
184
|
+
##画像を白黒反転する(下線を検出しやすくするため)
|
185
|
+
gray2 = cv2.bitwise_not(gray)
|
186
|
+
#cv2.imwrite(filename0+"_blackandwhite.jpg", gray2)
|
187
|
+
|
188
|
+
##下線を検出
|
189
|
+
lines = cv2.HoughLinesP(gray2, rho=1, theta=np.pi/180, threshold=200, minLineLength=400, maxLineGap=2)
|
190
|
+
print(lines)
|
191
|
+
|
192
|
+
##線を消す(白線を引く)
|
193
|
+
for line in lines:
|
194
|
+
x1, y1, x2, y2 = line[0]
|
195
|
+
# 線を消す(白で線を引く)
|
196
|
+
no_lines_img = cv2.line(img00, (x1,y1), (x2,y2), (255,255,255), 3)
|
197
|
+
cv2.imwrite(filename0+"_noline.jpg", no_lines_img)
|
198
|
+
os.remove(filename)
|
199
|
+
|
200
|
+
# 3.原稿画像の読み込み
|
201
|
+
|
202
|
+
img_org = Image.open(filename0+"_noline.jpg")
|
203
|
+
|
204
|
+
# 4.OCR実行
|
205
|
+
builder = builders.TextBuilder(tesseract_layout=6)
|
206
|
+
result = tool.image_to_string(img_org, lang="eng", builder=builder)
|
207
|
+
|
208
|
+
print(result)
|
209
|
+
|
111
210
|
```
|