generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 3
/
01-pdf-ocr.py
428 lines (309 loc) · 11.6 KB
/
01-pdf-ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# Databricks notebook source
# MAGIC %md
# MAGIC You may find this series of notebooks at https://github.com/databricks-industry-solutions/ocr-phi-masking. For more information about this solution accelerator, visit https://www.databricks.com/solutions/accelerators/automated-phi-removal.
# COMMAND ----------
# MAGIC %md
# MAGIC # Spark OCR in Healthcare
# MAGIC
# MAGIC Spark OCR is a commercial extension of Spark NLP for optical character recognition from images, scanned PDF documents, Microsoft DOCX and DICOM files.
# MAGIC
# MAGIC In this notebook we will:
# MAGIC - Import clinical notes in pdf format and store in delta
# MAGIC - Convert pdfs to image and improve image quality
# MAGIC - Extract text from pdfs and store resulting text data in delta
# COMMAND ----------
# MAGIC %pip install transformers==4.22.1
# COMMAND ----------
#To prevent undesired info from the outputs
import logging
logger = spark._jvm.org.apache.log4j
logging.getLogger("py4j.java_gateway").setLevel(logging.ERROR)
# COMMAND ----------
import os
import json
import string
#import sys
#import base64
import numpy as np
import pandas as pd
import sparknlp
import sparknlp_jsl
from sparknlp.base import *
from sparknlp.util import *
from sparknlp.annotator import *
from sparknlp_jsl.base import *
from sparknlp_jsl.annotator import *
from sparknlp.pretrained import ResourceDownloader
import sparkocr
from sparkocr.transformers import *
from sparkocr.utils import *
from sparkocr.enums import *
from pyspark.sql import functions as F
from pyspark.ml import Pipeline, PipelineModel
from sparknlp.training import CoNLL
import matplotlib.pyplot as plt
pd.set_option('max_colwidth', 100)
pd.set_option('display.max_columns', 100)
pd.set_option('display.expand_frame_repr', False)
spark.sql("set spark.sql.legacy.allowUntypedScalaUDF=true")
print('sparknlp.version : ',sparknlp.version())
print('sparknlp_jsl.version : ',sparknlp_jsl.version())
print('sparkocr : ',sparkocr.version())
spark
# COMMAND ----------
# MAGIC %md
# MAGIC ## 0. Initial Configurations
# COMMAND ----------
# MAGIC %run ./03-config
# COMMAND ----------
solacc_settings=SolAccUtil('phi_ocr')
solacc_settings.print_paths()
# COMMAND ----------
# DBTITLE 1,download pdf files
remote_url='https://raw.githubusercontent.com/JohnSnowLabs/spark-nlp-workshop/master/data/ocr'
for i in range(0,3):
solacc_settings.load_remote_data(f'{remote_url}/MT_OCR_0{i}.pdf')
dbutils.fs.ls(solacc_settings.data_path)
# COMMAND ----------
pdfs_df = spark.read.format("binaryFile").load(f'{solacc_settings.data_path}/*.pdf').sort('path')
print("Number of files in the folder : ", pdfs_df.count())
# COMMAND ----------
# MAGIC %md
# MAGIC ## 0.1. Write pdf files to delta bronze layer
# COMMAND ----------
pdfs_bronze_df = pdfs_df.selectExpr('sha1(path) as id','*')
display(pdfs_bronze_df)
# COMMAND ----------
pdfs_bronze_df.write.mode('overwrite').save(f'{solacc_settings.delta_path}/bronze/notes_pdfs')
# COMMAND ----------
# MAGIC %md
# MAGIC ## 1. Parsing the Files through OCR (create bronze layer)
# COMMAND ----------
# MAGIC %md
# MAGIC - The pdf files can have more than one page. We will transform the document in to images per page. Than we can run OCR to get text.
# MAGIC - We are using `PdfToImage()` to render PDF to images and `ImageToText()` to runs OCR for each images.
# COMMAND ----------
pdf_df=spark.read.load(f'{solacc_settings.delta_path}/bronze/notes_pdfs')
# COMMAND ----------
# Transform PDF document to images per page
pdf_to_image = PdfToImage()\
.setInputCol("content")\
.setOutputCol("image")
# Run OCR
ocr = ImageToText()\
.setInputCol("image")\
.setOutputCol("text")\
.setConfidenceThreshold(65)\
.setIgnoreResolution(False)
ocr_pipeline = PipelineModel(stages=[
pdf_to_image,
ocr
])
# COMMAND ----------
# MAGIC %md
# MAGIC - Now, we can transform the `pdfs` with our pipeline.
# COMMAND ----------
ocr_result_df = ocr_pipeline.transform(pdfs_df)
# COMMAND ----------
# MAGIC %md
# MAGIC - After transforming we get following columns :
# MAGIC
# MAGIC - path
# MAGIC - modificationTime
# MAGIC - length
# MAGIC - image
# MAGIC - total_pages
# MAGIC - pagenum
# MAGIC - documentnum
# MAGIC - confidence
# MAGIC - exception
# MAGIC - text
# MAGIC - positions
# COMMAND ----------
display(
ocr_result_df.select('modificationTime', 'length', 'total_pages', 'pagenum', 'documentnum', 'confidence', 'exception')
)
# COMMAND ----------
display(ocr_result_df.select('path', 'image', 'text', 'positions'))
# COMMAND ----------
# MAGIC %md
# MAGIC - Now, we have our pdf files in text format and as image.
# MAGIC
# MAGIC - Let's see the images and the text.
# COMMAND ----------
import matplotlib.pyplot as plt
img = ocr_result_df.collect()[0].image
img_pil = to_pil_image(img, img.mode)
plt.figure(figsize=(24,16))
plt.imshow(img_pil, cmap='gray')
plt.show()
# COMMAND ----------
# MAGIC %md
# MAGIC - Let's see extracted text which is stored in `'text'` column as a list. Each line is is an item in this list, so we can join them and see the whole page.
# COMMAND ----------
print("\n".join([row.text for row in ocr_result_df.select("text").collect()[0:1]]))
# COMMAND ----------
# MAGIC %md
# MAGIC ### 1.1. Skew Correction
# MAGIC
# MAGIC In some images, there may be some skewness and this reduces accuracy of the extracted text. Spark OCR has `ImageSkewCorrector` which detects skew of the image and rotates it.
# COMMAND ----------
from pyspark.sql.types import *
new_schema = StructType([
StructField('origin',StringType(),'true'),
StructField('height',IntegerType(),'false'),
StructField('width',IntegerType(),'false'),
StructField('nChannels',IntegerType(),'false'),
StructField('mode',IntegerType(),'false'),
StructField('resolution',IntegerType(),'false'),
StructField('data',BinaryType(),'true')
])
new_img_schema = StructType([StructField('image',new_schema,'true')])
# COMMAND ----------
# Image skew corrector
pdf_to_image = PdfToImage()\
.setInputCol("content")\
.setOutputCol("image")
skew_corrector = ImageSkewCorrector()\
.setInputCol("image")\
.setOutputCol("corrected_image")\
.setAutomaticSkewCorrection(True)
ocr = ImageToText()\
.setInputCol("corrected_image")\
.setOutputCol("text")\
.setConfidenceThreshold(65)\
.setIgnoreResolution(False)
ocr_skew_corrector = PipelineModel(stages=[
pdf_to_image,
skew_corrector,
ocr
])
# COMMAND ----------
ocr_skew_corrected_result_df = ocr_skew_corrector.transform(pdf_df).cache()
# COMMAND ----------
# MAGIC %md
# MAGIC Let's see the results after the skew correction.
# COMMAND ----------
# DBTITLE 1,Original Images
ocr_result_df.select('path', 'confidence').filter("path rlike 'MT_OCR_01.pdf'").limit(1).display()
# COMMAND ----------
# DBTITLE 1,Skew Corrected Images
ocr_skew_corrected_result_df.select('path', 'confidence').filter("path rlike 'MT_OCR_01.pdf'").limit(1).display()
# COMMAND ----------
# MAGIC %md
# MAGIC After skew correction, confidence is increased from %48.3 to % %66.5. Let's display the corrected image and the original image side by side.
# COMMAND ----------
img_orig = ocr_skew_corrected_result_df.select("image").collect()[1].image
img_corrected = ocr_skew_corrected_result_df.select("corrected_image").collect()[1].corrected_image
img_pil_orig = to_pil_image(img_orig, img_orig.mode)
img_pil_corrected = to_pil_image(img_corrected, img_corrected.mode)
plt.figure(figsize=(24,16))
plt.subplot(1, 2, 1)
plt.imshow(img_pil_orig, cmap='gray')
plt.title('Original')
plt.subplot(1, 2, 2)
plt.imshow(img_pil_corrected, cmap='gray')
plt.title("Skew Corrected")
plt.show()
# COMMAND ----------
# MAGIC %md
# MAGIC ### 1.2. Image Processing
# MAGIC
# MAGIC * After reading pdf files, we can process on images to increase the confidence.
# MAGIC
# MAGIC * By **`ImageAdaptiveThresholding`**, we can compute a threshold mask image based on local pixel neighborhood and apply it to image.
# MAGIC
# MAGIC * Another method which we can add to pipeline is applying morphological operations. We can use **`ImageMorphologyOperation`** which support:
# MAGIC - Erosion
# MAGIC - Dilation
# MAGIC - Opening
# MAGIC - Closing
# MAGIC
# MAGIC * To remove remove background objects **`ImageRemoveObjects`** can be used.
# MAGIC
# MAGIC * We will add **`ImageLayoutAnalyzer`** to pipeline, to analyze the image and determine the regions of text.
# COMMAND ----------
from sparkocr.enums import *
# Read binary as image
pdf_to_image = PdfToImage()\
.setInputCol("content")\
.setOutputCol("image")\
.setResolution(400)
# Correcting the skewness
skew_corrector = ImageSkewCorrector()\
.setInputCol("image")\
.setOutputCol("skew_corrected_image")\
.setAutomaticSkewCorrection(True)
# Binarize using adaptive thresholding
binarizer = ImageAdaptiveThresholding()\
.setInputCol("skew_corrected_image")\
.setOutputCol("binarized_image")\
.setBlockSize(91)\
.setOffset(50)
# Apply morphology opening
opening = ImageMorphologyOperation()\
.setKernelShape(KernelShape.SQUARE)\
.setOperation(MorphologyOperationType.OPENING)\
.setKernelSize(3)\
.setInputCol("binarized_image")\
.setOutputCol("opening_image")
# Remove small objects
remove_objects = ImageRemoveObjects()\
.setInputCol("opening_image")\
.setOutputCol("corrected_image")\
.setMinSizeObject(130)
ocr_corrected = ImageToText()\
.setInputCol("corrected_image")\
.setOutputCol("corrected_text")\
.setPageIteratorLevel(PageIteratorLevel.SYMBOL) \
.setPageSegMode(PageSegmentationMode.SPARSE_TEXT) \
.setConfidenceThreshold(65)
# OCR pipeline
image_pipeline = PipelineModel(stages=[
pdf_to_image,
skew_corrector,
binarizer,
opening,
remove_objects,
ocr_corrected
])
# COMMAND ----------
pdf_processed_df = image_pipeline.transform(pdf_df).cache()
# COMMAND ----------
# MAGIC %md
# MAGIC Let's see the original image and corrected image.
# COMMAND ----------
img_orig = pdf_processed_df.select("image").collect()[1].image
img_corrected = pdf_processed_df.select("corrected_image").collect()[1].corrected_image
img_pil_orig = to_pil_image(img_orig, img_orig.mode)
img_pil_corrected = to_pil_image(img_corrected, img_corrected.mode)
plt.figure(figsize=(24,16))
plt.subplot(1, 2, 1)
plt.imshow(img_pil_orig, cmap='gray')
plt.title('Original')
plt.subplot(1, 2, 2)
plt.imshow(img_pil_corrected, cmap='gray')
plt.title("Skew Corrected")
plt.show()
# COMMAND ----------
# MAGIC %md
# MAGIC After processing, we have cleaner image. And confidence is increased to %97
# COMMAND ----------
print("Original Images")
display(ocr_result_df.filter("path rlike 'MT_OCR_01.pdf'").select('confidence'))
print("Skew Corrected Images")
display(ocr_skew_corrected_result_df.filter("path rlike 'MT_OCR_01.pdf'").select('confidence'))
print("Corrected Images")
display(pdf_processed_df.filter("path rlike 'MT_OCR_01.pdf'").select('confidence'))
# COMMAND ----------
# MAGIC %md
# MAGIC ## Write to Silver
# MAGIC Now after applying image processing and correction to the notes and extracting corrected text, we write the final result in deltalake's silver layer to
# COMMAND ----------
pdf_processed_silver_df=pdf_processed_df.select('id','path','length','total_pages','documentnum','pagenum','corrected_image','confidence','corrected_text')
pdf_processed_silver_df.display()
# COMMAND ----------
pdf_processed_silver_df.write.mode('overwrite').save(f'{solacc_settings.delta_path}/silver/processed_pdf')
# COMMAND ----------
# MAGIC %md
# MAGIC In the next notebook, we read extracted text from clinical notes, extract PHI entities and obfuscate those entities.