mirror of
https://github.com/data-privacy-stack/presidio.git
synced 2026-09-30 18:08:36 -05:00
4.7 MiB
4.7 MiB
In [ ]:
!pip install presidio_analyzer presidio_anonymizer presidio_image_redactor
!python -m spacy download en_core_web_lgIn [1]:
from PIL import Image
import pydicom
from presidio_image_redactor import ImageAnalyzerEngine, ImagePiiVerifyEngine, DicomImagePiiVerifyEngine
import matplotlib.pyplot as pltIn [2]:
# Image analyzer engine
image_analyzer_engine = ImageAnalyzerEngine()
# Verification engines
verify_engine = ImagePiiVerifyEngine() # standard images
dicom_verify_engine = DicomImagePiiVerifyEngine() # DICOM images
padding_width = 3In [3]:
image = Image.open("../../image-redactor/ocr_text.png")
display(image)In [4]:
verify_image = verify_engine.verify(image, display_image=True)In [5]:
instance = pydicom.dcmread("./sample_data/0_ORIGINAL.dcm")
plt.imshow(instance.pixel_array, cmap="gray")Out [5]:
<matplotlib.image.AxesImage at 0x1f2a91512b0>
In [6]:
dicom_image, dicom_ocr_bboxes, dicom_analyzer_bboxes = dicom_verify_engine.verify_dicom_instance(
instance,
padding_width=padding_width,
display_image=True
)In [7]:
len(dicom_ocr_bboxes)Out [7]:
9
In [8]:
len(dicom_analyzer_bboxes)Out [8]:
4
In [9]:
type(dicom_analyzer_bboxes)Out [9]:
list
In [10]:
type(dicom_analyzer_bboxes[0])Out [10]:
dict
In [11]:
dicom_analyzer_bboxes[0]Out [11]:
{'entity_type': 'PERSON',
'score': 0.85,
'left': 3,
'top': 3,
'width': 241,
'height': 37,
'is_PII': True}In [12]:
# Example provided bounding box list
given_bboxes = [
{
'entity_type': 'PERSON',
'left': 3,
'top': 3,
'width': 241,
'height': 37,
'is_PII': True
},
{
'entity_type': 'PERSON',
'left': 179,
'top': 150,
'width': 300,
'height': 74,
'is_PII': False
}
]In [13]:
# Let's plot with the given bounding boxes
test_image = image_analyzer_engine.add_custom_bboxes(image, given_bboxes, show_text_annotation=True)