|
''' |
|
MIT license https://opensource.org/licenses/MIT Copyright 2024 Infosys Ltd |
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
''' |
|
|
|
|
|
import json |
|
import io, base64 |
|
import random |
|
from PIL import Image |
|
import requests |
|
import pandas as pd |
|
from privacy.service.easy import EasyOCR |
|
from privacy.service.azureComputerVision import ComputerVision |
|
|
|
from privacy.mappers.mappers import * |
|
import secrets |
|
import os |
|
|
|
import httpx |
|
|
|
|
|
from privacy.util.encrypt import EncryptImage |
|
|
|
from typing import List |
|
from privacy.constants.local_constants import (DELTED_SUCCESS_MESSAGE) |
|
from privacy.exception.exception import PrivacyNameNotEmptyError, PrivacyException, PrivacyNotFoundError |
|
from presidio_analyzer import Pattern, PatternRecognizer, AnalyzerEngine, RecognizerRegistry,predefined_recognizers |
|
from presidio_anonymizer import AnonymizerEngine, DeanonymizeEngine |
|
from presidio_anonymizer.entities import (RecognizerResult, |
|
OperatorResult, |
|
OperatorConfig) |
|
from privacy.config.logger import CustomLogger |
|
from presidio_image_redactor import ImageRedactorEngine,ImageAnalyzerEngine,ImagePiiVerifyEngine |
|
from fastapi import FastAPI, UploadFile |
|
from fastapi.responses import FileResponse |
|
from PIL import Image |
|
import base64 |
|
import io |
|
import os |
|
|
|
from zipfile import ZipFile,is_zipfile |
|
from dotenv import load_dotenv |
|
import tempfile |
|
import glob |
|
from pathlib import Path |
|
import matplotlib.pyplot as plt |
|
import pydicom |
|
from presidio_image_redactor import DicomImageRedactorEngine |
|
from presidio_analyzer.nlp_engine import NlpEngineProvider |
|
from privacy.util.conf.conf import ConfModle |
|
|
|
|
|
|
|
|
|
|
|
from privacy.config.logger import request_id_var |
|
load_dotenv() |
|
import numpy as np |
|
import cv2 |
|
import re |
|
from faker import Faker |
|
fake = Faker() |
|
|
|
from privacy.util.fakerEntities import FakeData |
|
|
|
|
|
log = CustomLogger() |
|
import time |
|
|
|
|
|
|
|
import cv2 |
|
import pytesseract |
|
from scipy import ndimage |
|
from PIL import Image as im |
|
from privacy.util.special_recognizers.DataListRecognizer import DataListRecognizer |
|
|
|
from privacy.service.__init__ import * |
|
from privacy.service.api_req import ApiCall |
|
|
|
from xeger import Xeger |
|
x = Xeger() |
|
|
|
import contextvars |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageRotation: |
|
def float_convertor(x): |
|
if x.isdigit(): |
|
out= float(x) |
|
else: |
|
out= x |
|
return out |
|
def getAngle(image): |
|
k = pytesseract.image_to_osd(image) |
|
out = {i.split(":")[0]: ImageRotation.float_convertor(i.split(":")[-1].strip()) for i in k.rstrip().split("\n")} |
|
return out["Rotate"] |
|
def rotateImage(image,preAngle=0): |
|
angle=0 |
|
|
|
if(preAngle==0): |
|
angle=ImageRotation.getAngle(image) |
|
|
|
if(preAngle==angle): |
|
return (image,angle) |
|
img_rotated = ndimage.rotate(image, preAngle-angle) |
|
image = im.fromarray(img_rotated) |
|
return (image,angle) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PrivacyService: |
|
|
|
|
|
def analyze(payload: PIIAnalyzeRequest) -> PIIAnalyzeResponse: |
|
error_dict[request_id_var.get()]=[] |
|
log.debug("Entering in analyze function") |
|
|
|
log.debug(f"payload: {payload}") |
|
try: |
|
if(payload.exclusionList == None): |
|
exclusionList=[] |
|
else: |
|
exclusionList=payload.exclusionList |
|
|
|
if(payload.portfolio== None): |
|
results = PrivacyService.__analyze(text=payload.inputText,exclusion=exclusionList) |
|
else: |
|
results = PrivacyService.__analyze(text=payload.inputText,accName=payload,exclusion=exclusionList) |
|
if results == None: |
|
return None |
|
if( results== 404): |
|
return results |
|
list_PIIEntity = [] |
|
results=sorted(results, key=lambda i: i.start) |
|
|
|
for result in results: |
|
log.debug(f"result: {result}") |
|
obj_PIIEntity = PIIEntity(type=result.entity_type, |
|
beginOffset=result.start, |
|
endOffset=result.end, |
|
score=result.score, |
|
responseText=payload.inputText[result.start:result.end]) |
|
log.debug(f"obj_PIIEntity: {obj_PIIEntity}") |
|
list_PIIEntity.append(obj_PIIEntity) |
|
del obj_PIIEntity |
|
|
|
log.debug(f"list_PIIEntity: {list_PIIEntity}") |
|
objPIIAnalyzeResponse = PIIAnalyzeResponse |
|
objPIIAnalyzeResponse.PIIEntities = list_PIIEntity |
|
|
|
log.debug("Returning from analyze function") |
|
return objPIIAnalyzeResponse |
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"textAnalyzeMainFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
|
|
raise Exception(e) |
|
|
|
|
|
def __analyze(text: str,accName:any=None,exclusion:any=None): |
|
result=[] |
|
|
|
|
|
try: |
|
if(accName==None): |
|
|
|
|
|
result = analyzer.analyze(text=text,language="en",allow_list=exclusion,return_decision_process = True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
preEntity=[] |
|
|
|
|
|
dataistEnt = [] |
|
|
|
response_value=ApiCall.request(accName) |
|
|
|
if(response_value==None): |
|
return None |
|
if(response_value==404): |
|
|
|
return response_value |
|
|
|
entityType,datalist,preEntity=response_value |
|
|
|
for d in range(len(datalist)): |
|
record=ApiCall.getRecord(entityType[d]) |
|
record=AttributeDict(record) |
|
log.debug("Record====="+str(record)) |
|
|
|
|
|
|
|
if(record.RecogType=="Data"): |
|
|
|
dataRecog=(DataListRecognizer(terms=datalist[d],entitie=[entityType[d]])) |
|
registry.add_recognizer(dataRecog) |
|
|
|
|
|
|
|
|
|
update_session_dict(entityType[d], datalist[d]) |
|
data = {entityType[d]: datalist[d]} |
|
dataistEnt.append(data) |
|
|
|
elif(record.RecogType=="Pattern" and record.isPreDefined=="No"): |
|
contextObj=record.Context.split(',') |
|
pattern="|".join(datalist[d]) |
|
|
|
log.debug("pattern="+str(pattern)) |
|
patternObj = Pattern(name=entityType[d], |
|
regex=pattern, |
|
score=record.Score) |
|
patternRecog = PatternRecognizer(supported_entity=entityType[d], |
|
patterns=[patternObj],context=contextObj) |
|
registry.add_recognizer(patternRecog) |
|
|
|
|
|
|
|
results = analyzer.analyze(text=text, language="en",entities=entityType+preEntity,allow_list=exclusion,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"]) |
|
|
|
result.extend(results) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug(f"results: {results}") |
|
log.debug(f"type results: {type(results)}") |
|
|
|
|
|
|
|
|
|
|
|
log.debug("result="+str(result)) |
|
|
|
return result |
|
except Exception as e: |
|
log.error(str(e)) |
|
|
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
|
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"textAnalyzeFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
def anonymize(payload: PIIAnonymizeRequest): |
|
error_dict[request_id_var.get()]=[] |
|
log.debug("Entering in anonymize function") |
|
try: |
|
Data.encrypted_text.clear() |
|
|
|
|
|
if(payload.exclusionList == None): |
|
exclusionList=[] |
|
else: |
|
exclusionList=payload.exclusionList |
|
if(payload.portfolio== None): |
|
results = PrivacyService.__analyze(text=payload.inputText,exclusion=exclusionList) |
|
else: |
|
results = PrivacyService.__analyze(text=payload.inputText,accName=payload,exclusion=exclusionList) |
|
|
|
ent_pattern=[] |
|
if results == None: |
|
return None |
|
|
|
if(results==404): |
|
|
|
return results |
|
dict_operators = {} |
|
if payload.fakeData == True: |
|
|
|
fake_dict_operator = FakeDataGenerate.fakeDataGeneration(results,payload.inputText) |
|
dict_operators.update(fake_dict_operator) |
|
|
|
|
|
encryptionList=[] |
|
if(payload.portfolio!= None ): |
|
|
|
encryptionList=admin_par[request_id_var.get()]["encryptionList"] |
|
|
|
if encryptionList is not None and len(encryptionList) >0 : |
|
for entity in encryptionList: |
|
print("---------------------------") |
|
dict_operators.update({entity: OperatorConfig("hash", {"hash-type": 'md5'})}) |
|
|
|
|
|
|
|
|
|
anonymize_text = anonymizer.anonymize(text=payload.inputText, |
|
operators=dict_operators, |
|
analyzer_results=results) |
|
|
|
|
|
log.debug(f"anonymize_text: {anonymize_text}") |
|
log.debug(f"anonymize_text_item"+ str(anonymize_text.items)) |
|
|
|
obj_PIIAnonymizeResponse = PIIAnonymizeResponse |
|
obj_PIIAnonymizeResponse.anonymizedText = anonymize_text.text |
|
log.debug("Returning from anonymize function") |
|
|
|
return obj_PIIAnonymizeResponse |
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"textAnonimyzeFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
def encrypt(payload: PIIAnonymizeRequest): |
|
log.debug("Entering in encrypt function") |
|
|
|
try: |
|
Data.encrypted_text.clear() |
|
|
|
if(payload.exclusionList == None): |
|
exclusionList=[] |
|
else: |
|
exclusionList=payload.exclusionList |
|
if(payload.portfolio== None): |
|
results = PrivacyService.__analyze(text=payload.inputText,exclusion=exclusionList) |
|
else: |
|
results = PrivacyService.__analyze(text=payload.inputText,accName=payload,exclusion=exclusionList) |
|
|
|
if results == None: |
|
return None |
|
dict_operators = {} |
|
|
|
data = "WmZq4t7w!z%C&F)J" |
|
|
|
for i in results: |
|
dict_operators.update({i.entity_type : OperatorConfig("encrypt", {"key": data})}) |
|
|
|
anonymize_text = anonymizer.anonymize(text=payload.inputText, |
|
operators=dict_operators, |
|
analyzer_results=results) |
|
|
|
log.debug(f"anonymize_text: {anonymize_text}") |
|
log.debug(f"anonymize_text_item"+ str(anonymize_text.items)) |
|
|
|
obj_PIIEncryptResponse = PIIEncryptResponse |
|
obj_PIIEncryptResponse.text = anonymize_text.text |
|
obj_PIIEncryptResponse.items= anonymize_text.items |
|
log.debug("Returning from encrypt function") |
|
|
|
return obj_PIIEncryptResponse |
|
|
|
|
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"textAnonimyzeFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
def decryption(payload: PIIDecryptRequest): |
|
log.debug("Entering in decrypt function") |
|
|
|
print("payload=====",payload) |
|
try: |
|
anonymized_text = payload.text |
|
anonymized_entities = payload.items |
|
|
|
data = "WmZq4t7w!z%C&F)J" |
|
list_ent= [] |
|
for item in anonymized_entities: |
|
list_ent.append(OperatorResult(start=item.start, |
|
end=item.end , |
|
entity_type= item.entity_type, |
|
text= item.text, |
|
operator= item.operator,)) |
|
anonymized_entities=list_ent |
|
|
|
deanonymized_result = deanonymizer.deanonymize(text=anonymized_text, |
|
entities=anonymized_entities, |
|
operators={"DEFAULT": OperatorConfig("decrypt", {"key": data})},) |
|
|
|
obj_PIIDecryptResponse = PIIDecryptResponse |
|
obj_PIIDecryptResponse.decryptedText = deanonymized_result.text |
|
log.debug("Returning from anonymize function") |
|
|
|
return obj_PIIDecryptResponse |
|
|
|
|
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"textAnonimyzeFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
|
|
def image_analyze(payload): |
|
error_dict[request_id_var.get()]=[] |
|
try: |
|
log.debug("Entering in image_analyze function") |
|
payload=AttributeDict(payload) |
|
image = Image.open(payload.image.file) |
|
|
|
|
|
angle=0 |
|
if(payload.rotationFlag): |
|
image,angle=ImageRotation.rotateImage(image) |
|
|
|
ocr=None |
|
global imageAnalyzerEngine |
|
if(payload.easyocr=="EasyOcr"): |
|
ocr=EasyOCR() |
|
EasyOCR.setMag(payload.mag_ratio) |
|
tt=time.time() |
|
imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr) |
|
|
|
if(payload.easyocr=="ComputerVision"): |
|
ocr=ComputerVision() |
|
|
|
|
|
imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr) |
|
|
|
|
|
log.debug("payload="+str(payload)) |
|
if(payload.exclusion == None): |
|
exclusionList=[] |
|
else: |
|
exclusionList=payload.exclusion.split(",") |
|
if(payload.portfolio== None): |
|
results = imageAnalyzerEngine.analyze(image, allow_list=exclusionList) |
|
else: |
|
result=[] |
|
preEntity=[] |
|
response_value=ApiCall.request(payload) |
|
if(response_value==None): |
|
return None |
|
if(response_value==404): |
|
|
|
return response_value |
|
entityType,datalist,preEntity=response_value |
|
|
|
|
|
for d in range(len(datalist)): |
|
record=ApiCall.getRecord(entityType[d]) |
|
record=AttributeDict(record) |
|
|
|
|
|
if(record.RecogType=="Data"): |
|
dataRecog=(DataListRecognizer(terms=datalist[d],entitie=[entityType[d]])) |
|
registry.add_recognizer(dataRecog) |
|
|
|
|
|
|
|
elif(record.RecogType=="Pattern" and record.isPreDefined=="No"): |
|
contextObj=record.Context.split(',') |
|
pattern="|".join(datalist[d]) |
|
log.debug("pattern="+str(pattern)) |
|
patternObj = Pattern(name=entityType[d], |
|
regex=pattern, |
|
score=record.Score) |
|
patternRecog = PatternRecognizer(supported_entity=entityType[d], |
|
patterns=[patternObj],context=contextObj) |
|
registry.add_recognizer(patternRecog) |
|
|
|
|
|
|
|
results = imageAnalyzerEngine.analyze(image,entities=entityType+preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"]) |
|
result.extend(results) |
|
|
|
|
|
|
|
|
|
|
|
results=result |
|
|
|
|
|
|
|
list_PIIEntity = [] |
|
for result in results: |
|
log.debug(f"result: {result}") |
|
obj_PIIEntity = PIIEntity(type=result.entity_type, |
|
beginOffset=result.start, |
|
endOffset=result.end, |
|
score=result.score) |
|
log.debug(f"obj_PIIEntity: {obj_PIIEntity}") |
|
list_PIIEntity.append(obj_PIIEntity) |
|
del obj_PIIEntity |
|
|
|
log.debug(f"list_PIIEntity: {list_PIIEntity}") |
|
objPIIAnalyzeResponse = PIIAnalyzeResponse |
|
objPIIAnalyzeResponse.PIIEntities = list_PIIEntity |
|
|
|
log.debug("Returning from image_analyze function") |
|
|
|
return objPIIAnalyzeResponse |
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"imageAnalyzeFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
|
|
|
|
def temp(payload): |
|
engine = ImageAnalyzerEngine() |
|
|
|
image = Image.open(payload.file) |
|
results = engine.analyze(image) |
|
|
|
list_PIIEntity = [] |
|
for result in results: |
|
log.debug(f"result: {result}") |
|
list_PIIEntity.append(result.entity_type) |
|
|
|
|
|
|
|
|
|
return list_PIIEntity |
|
|
|
|
|
|
|
def image_anonymize(payload): |
|
log.debug("Entering in image_anonymize function") |
|
error_dict[request_id_var.get()]=[] |
|
try: |
|
payload=AttributeDict(payload) |
|
|
|
ocr=None |
|
global imageRedactorEngine |
|
if(payload.easyocr=="EasyOcr"): |
|
ocr=EasyOCR() |
|
EasyOCR.setMag(payload.mag_ratio) |
|
|
|
imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr) |
|
imageRedactorEngine = ImageRedactorEngine(image_analyzer_engine=imageAnalyzerEngine) |
|
if(payload.easyocr=="ComputerVision"): |
|
ocr=ComputerVision() |
|
|
|
|
|
imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr) |
|
imageRedactorEngine = ImageRedactorEngine(image_analyzer_engine=imageAnalyzerEngine) |
|
|
|
payload=AttributeDict(payload) |
|
image = Image.open(payload.image.file) |
|
|
|
|
|
angle=0 |
|
if(payload.rotationFlag): |
|
image,angle=ImageRotation.rotateImage(image) |
|
|
|
|
|
|
|
if(payload.exclusion == None): |
|
exclusionList=[] |
|
else: |
|
exclusionList=payload.exclusion.split(",") |
|
if(payload.portfolio== None): |
|
redacted_image = imageRedactorEngine.redact(image, (255, 192, 203), allow_list=exclusionList) |
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
else: |
|
result=[] |
|
preEntity=[] |
|
response_value=ApiCall.request(payload) |
|
if(response_value==None): |
|
return None |
|
if(response_value==404): |
|
|
|
return response_value |
|
entityType,datalist,preEntity=response_value |
|
|
|
for d in range(len(datalist)): |
|
record=ApiCall.getRecord(entityType[d]) |
|
record=AttributeDict(record) |
|
|
|
|
|
if(record.RecogType=="Data"): |
|
dataRecog=(DataListRecognizer(terms=datalist[d],entitie=[entityType[d]])) |
|
registry.add_recognizer(dataRecog) |
|
|
|
|
|
|
|
|
|
|
|
elif(record.RecogType=="Pattern" and record.isPreDefined=="No"): |
|
contextObj=record.Context.split(',') |
|
pattern="|".join(datalist[d]) |
|
log.debug("pattern="+str(pattern)) |
|
patternObj = Pattern(name=entityType[d], |
|
regex=pattern, |
|
score=record.Score) |
|
patternRecog = PatternRecognizer(supported_entity=entityType[d], |
|
patterns=[patternObj],context=contextObj) |
|
registry.add_recognizer(patternRecog) |
|
|
|
|
|
redacted_image = imageRedactorEngine.redact(image, (255, 192, 203),entities=entityType+preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"]) |
|
|
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
|
|
image=redacted_image |
|
|
|
|
|
|
|
|
|
|
|
|
|
if(angle!=0 and payload.rotationFlag==True): |
|
redacted_image,angle=ImageRotation.rotateImage(redacted_image,angle) |
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
|
|
|
|
|
|
|
|
processed_image_bytes = processed_image_stream.getvalue() |
|
base64_encoded_image=base64.b64encode(processed_image_bytes) |
|
|
|
saveImage.saveImg(base64_encoded_image) |
|
log.debug("Returning from image_anonymize function") |
|
|
|
return base64_encoded_image |
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"imageAnonimyzeFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
async def image_masking(main_image,template_image): |
|
template_gray = cv2.cvtColor(template_image, cv2.COLOR_BGR2GRAY) |
|
|
|
_, template_mask = cv2.threshold(template_gray, 1, 255, cv2.THRESH_BINARY) |
|
|
|
|
|
result = cv2.matchTemplate(main_image, template_image, cv2.TM_CCOEFF_NORMED) |
|
_, max_val, _, max_loc = cv2.minMaxLoc(result) |
|
|
|
|
|
template_height, template_width = template_image.shape[:2] |
|
|
|
|
|
mask = np.zeros(main_image.shape[:2], dtype=np.uint8) |
|
|
|
|
|
mask[max_loc[1]:max_loc[1] + template_height, max_loc[0]:max_loc[0] + template_width] = 255 |
|
|
|
|
|
result_with_mask = cv2.bitwise_and(main_image, main_image, mask=cv2.bitwise_not(mask)) |
|
|
|
return result_with_mask |
|
|
|
def zipimage_anonymize(payload): |
|
result=[] |
|
in_memory_file=io.BytesIO(payload.file.read()) |
|
|
|
engine = ImageRedactorEngine() |
|
log.debug("=="+str(is_zipfile(payload.file))) |
|
|
|
with ZipFile(in_memory_file, 'r') as zObject: |
|
for file_name in zObject.namelist(): |
|
|
|
log.debug(zObject.namelist()) |
|
log.debug("=="+str(type(zObject))) |
|
file_data=zObject.read(file_name) |
|
image=Image.open(io.BytesIO(file_data)) |
|
redacted_image = engine.redact(image, (255, 192, 203)) |
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
processed_image_bytes = processed_image_stream.getvalue() |
|
base64_encoded_image=base64.b64encode(processed_image_bytes) |
|
result.append(base64_encoded_image) |
|
return result |
|
|
|
def image_verify(payload): |
|
error_dict[request_id_var.get()]=[] |
|
log.debug("Entering in image_verify function") |
|
try: |
|
|
|
|
|
|
|
|
|
global imagePiiVerifyEngine |
|
payload=AttributeDict(payload) |
|
image = Image.open(payload.image.file) |
|
|
|
if(payload.exclusion == None): |
|
exclusionList=[] |
|
else: |
|
exclusionList=payload.exclusion.split(",") |
|
|
|
if(payload.portfolio== None): |
|
verify_image = imagePiiVerifyEngine.verify(image, allow_list=exclusionList) |
|
processed_image_stream = io.BytesIO() |
|
verify_image.save(processed_image_stream, format='PNG') |
|
|
|
else: |
|
result=[] |
|
preEntity=[] |
|
response_value=ApiCall.request(payload) |
|
if(response_value==None): |
|
return None |
|
if(response_value==404): |
|
|
|
return response_value |
|
entityType,datalist,preEntity=response_value |
|
|
|
|
|
for d in range(len(datalist)): |
|
record=ApiCall.getRecord(entityType[d]) |
|
record=AttributeDict(record) |
|
|
|
if(record.RecogType=="Data"): |
|
dataRecog=(DataListRecognizer(terms=datalist[d],entitie=[entityType[d]])) |
|
registry.add_recognizer(dataRecog) |
|
elif(record.RecogType=="Pattern" and record.isPreDefined=="No"): |
|
contextObj=record.Context.split(',') |
|
pattern="|".join(datalist[d]) |
|
log.debug("pattern="+str(pattern)) |
|
patternObj = Pattern(name=entityType[d], |
|
regex=pattern, |
|
score=record.Score) |
|
patternRecog = PatternRecognizer(supported_entity=entityType[d], |
|
patterns=[patternObj],context=contextObj) |
|
registry.add_recognizer(patternRecog) |
|
verify_image = imagePiiVerifyEngine.verify(image,entities=entityType+preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"]) |
|
|
|
processed_image_stream = io.BytesIO() |
|
verify_image.save(processed_image_stream, format='PNG') |
|
|
|
image=verify_image |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
processed_image_bytes = processed_image_stream.getvalue() |
|
base64_encoded_image=base64.b64encode(processed_image_bytes) |
|
saveImage.saveImg(base64_encoded_image) |
|
log.debug("Returning from image_verify function") |
|
|
|
return base64_encoded_image |
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"imageVeryFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
def imageEncryption(payload): |
|
error_dict[request_id_var.get()]=[] |
|
log.debug("Entering in imageEncryption function") |
|
try: |
|
payload=AttributeDict(payload) |
|
EncryptImage.entity.clear() |
|
|
|
|
|
ocr=None |
|
global encryptImageEngin |
|
if(payload.easyocr=="EasyOcr"): |
|
ocr=EasyOCR() |
|
EasyOCR.setMag(payload.mag_ratio) |
|
imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr) |
|
encryptImageEngin=EncryptImage(image_analyzer_engine=imageAnalyzerEngine) |
|
if(payload.easyocr=="ComputerVision"): |
|
ocr=ComputerVision() |
|
|
|
|
|
imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr) |
|
encryptImageEngin=EncryptImage(image_analyzer_engine=imageAnalyzerEngine) |
|
|
|
|
|
payload=AttributeDict(payload) |
|
image = Image.open(payload.image.file) |
|
angle=0 |
|
if(payload.rotationFlag): |
|
image,angle=ImageRotation.rotateImage(image) |
|
|
|
|
|
encryptMapper=[] |
|
if(payload.exclusion == None): |
|
exclusionList=[] |
|
else: |
|
exclusionList=payload.exclusion.split(",") |
|
encryptImageEngin.getText(image) |
|
if(payload.portfolio== None): |
|
|
|
redacted_image = encryptImageEngin.imageAnonimyze(image, (255, 192, 203), allow_list=exclusionList) |
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
else: |
|
result=[] |
|
preEntity=[] |
|
response_value=ApiCall.request(payload) |
|
|
|
if(response_value==None): |
|
return None |
|
if(response_value==404): |
|
|
|
return response_value |
|
encryptionList=admin_par[request_id_var.get()]["encryptionList"] |
|
entityType,datalist,preEntity=response_value |
|
|
|
for d in range(len(datalist)): |
|
record=ApiCall.getRecord(entityType[d]) |
|
record=AttributeDict(record) |
|
|
|
|
|
if(record.RecogType=="Data"): |
|
dataRecog=(DataListRecognizer(terms=datalist[d],entitie=[entityType[d]])) |
|
registry.add_recognizer(dataRecog) |
|
|
|
|
|
|
|
|
|
|
|
elif(record.RecogType=="Pattern" and record.isPreDefined=="No"): |
|
contextObj=record.Context.split(',') |
|
pattern="|".join(datalist[d]) |
|
log.debug("pattern="+str(pattern)) |
|
patternObj = Pattern(name=entityType[d], |
|
regex=pattern, |
|
score=record.Score) |
|
patternRecog = PatternRecognizer(supported_entity=entityType[d], |
|
patterns=[patternObj],context=contextObj) |
|
registry.add_recognizer(patternRecog) |
|
|
|
|
|
redacted_image = encryptImageEngin.imageAnonimyze(image, (255, 192, 203),encryptionList=encryptionList,entities=entityType+preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"]) |
|
|
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
|
|
image=redacted_image |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EncryptImage.dis() |
|
res=encryptImageEngin.encrypt(redacted_image,encryptionList=encryptionList) |
|
redacted_image=res[0] |
|
encryptMapper=res[1] |
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
|
|
if(angle!=0 and payload.rotationFlag==True): |
|
redacted_image,angle=ImageRotation.rotateImage(redacted_image,angle) |
|
processed_image_stream = io.BytesIO() |
|
redacted_image.save(processed_image_stream, format='PNG') |
|
|
|
|
|
|
|
processed_image_bytes = processed_image_stream.getvalue() |
|
base64_encoded_image=base64.b64encode(processed_image_bytes) |
|
|
|
saveImage.saveImg(base64_encoded_image) |
|
obj={"map":encryptMapper,"img":base64_encoded_image} |
|
log.debug("Returning from imageEncryption function") |
|
|
|
return obj |
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"imageHashifyFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
def privacyShield(payload: PIIPrivacyShieldRequest) -> PIIPrivacyShieldResponse: |
|
log.debug("Entering in privacyShield function") |
|
log.debug(f"payload: {payload}") |
|
|
|
res = [] |
|
totEnt=[] |
|
enres=[] |
|
query={} |
|
|
|
log.debug("response="+str(res)) |
|
if(payload.portfolio== None): |
|
response_value=ApiCall.request(payload) |
|
if(response_value==None): |
|
return None |
|
if(response_value==404): |
|
|
|
|
|
return response_value |
|
entityType,datalist,preEntity=response_value |
|
|
|
results = PrivacyService.__analyze(text=payload.inputText) |
|
|
|
record=[ele for ele in admin_par[request_id_var.get()]["records"] if ele["isPreDefined"]=="Yes"] |
|
|
|
for i in record: |
|
i=AttributeDict(i) |
|
totEnt.append(i.RecogName) |
|
pass |
|
else: |
|
|
|
response_value=ApiCall.request(payload) |
|
if(response_value==None): |
|
return None |
|
if(response_value==404): |
|
|
|
return response_value |
|
entityType,datalist,preEntity=response_value |
|
|
|
to=[] |
|
|
|
|
|
entityType.extend(preEntity) |
|
|
|
totEnt=entityType |
|
|
|
results = PrivacyService.__analyze(text=payload.inputText,accName=payload) |
|
|
|
|
|
value=payload.inputText |
|
list_PIIEntity = [] |
|
results=sorted(results, key=lambda i: i.start) |
|
for result in results: |
|
log.debug(f"result: {result}") |
|
enres.append({"type":result.entity_type,"start":result.start,"end":result.end,"value":value[result.start:result.end]}) |
|
|
|
|
|
|
|
log.debug(f"obj_PIIEntity: {enres}") |
|
|
|
|
|
|
|
if(len(enres)==0): |
|
temp= "Passed" |
|
else: |
|
temp="Failed" |
|
|
|
objent = PrivacyShield( |
|
entitiesRecognised=enres, |
|
entitiesConfigured= totEnt, |
|
result=temp |
|
) |
|
list_PIIEntity.append(objent) |
|
log.debug(f"list_PIIEntity: {list_PIIEntity}") |
|
|
|
objPIIAnalyzeResponse = PIIPrivacyShieldResponse |
|
objPIIAnalyzeResponse.privacyCheck = list_PIIEntity |
|
|
|
|
|
log.debug("objPIIAnalyzeResponse="+str(objPIIAnalyzeResponse.privacyCheck)) |
|
log.debug("Returning from privacyShield function") |
|
return objPIIAnalyzeResponse |
|
|
|
|
|
class FakeDataGenerate: |
|
def fakeDataGeneration(results,inputText): |
|
fakeData_Dict = {} |
|
for i in results: |
|
if hasattr(FakeData, i.entity_type): |
|
ent = getattr(FakeData, i.entity_type) |
|
fakeData_Dict.update({i.entity_type: OperatorConfig("replace", {"new_value": ent()})}) |
|
elif i.entity_type in get_session_dict(): |
|
entValue =get_session_dict()[i.entity_type] |
|
|
|
log.debug("Value of entValue 342========"+str(entValue)) |
|
text = str(inputText[i.start:i.end]) |
|
print("text===",text) |
|
random_data="" |
|
while True: |
|
if text in entValue: |
|
random_data = secrets.choice(entValue) |
|
print("RNDAOM dATA 348===",random_data) |
|
if random_data.lower() != str(inputText[i.start:i.end]).lower() : |
|
fakeData_Dict.update({i.entity_type: OperatorConfig("replace", {"new_value": random_data})}) |
|
entValue.remove(random_data) |
|
break |
|
else: |
|
break |
|
print("Value of entValue 344========",text) |
|
|
|
|
|
|
|
|
|
fakeData_Dict.update({i.entity_type: OperatorConfig("replace", {"new_value": random_data})}) |
|
|
|
|
|
else: |
|
|
|
decision_process = i.analysis_explanation |
|
|
|
if(decision_process==None): |
|
continue |
|
pattern = decision_process.pattern |
|
|
|
t=x.xeger(pattern) |
|
p=r'[\x00-\x1F\x7F-\xFF]' |
|
|
|
t1=re.sub(p, ' ', t) |
|
|
|
fakeData_Dict.update({i.entity_type: OperatorConfig("replace", {"new_value": t1})}) |
|
|
|
|
|
|
|
return fakeData_Dict |
|
|
|
class DICOM: |
|
def dcmToPng(dcmObj): |
|
plt.clf() |
|
plt.imshow(dcmObj.pixel_array,cmap=plt.cm.bone) |
|
plt.axis('off') |
|
buffer=io.BytesIO() |
|
plt.savefig(buffer,format='png', bbox_inches='tight', pad_inches=0) |
|
buffer.seek(0) |
|
return base64.b64encode(buffer.getvalue()) |
|
|
|
|
|
|
|
|
|
def readDicom(payload): |
|
error_dict[request_id_var.get()]=[] |
|
log.debug("Entering in readDicom function") |
|
try: |
|
|
|
|
|
EncryptImage.entity.clear() |
|
|
|
|
|
DicomEngine = DicomImageRedactorEngine() |
|
dicom_instance = pydicom.dcmread(payload.file) |
|
|
|
redacted_dicom_instance = DicomEngine.redact(dicom_instance, fill="contrast") |
|
original=DICOM.dcmToPng(dicom_instance) |
|
redacted=DICOM.dcmToPng(redacted_dicom_instance) |
|
|
|
obj={"original":original,"anonymize":redacted} |
|
log.debug("Returning from readDicom function") |
|
return obj |
|
|
|
except Exception as e: |
|
log.error(str(e)) |
|
log.error("Line No:"+str(e.__traceback__.tb_lineno)) |
|
log.error(str(e.__traceback__.tb_frame)) |
|
error_dict[request_id_var.get()].append({"UUID":request_id_var.get(),"function":"readDICOMFunction","msg":str(e.__class__.__name__),"description":str(e)+"Line No:"+str(e.__traceback__.tb_lineno)}) |
|
raise Exception(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
class saveImage: |
|
def saveImg(img_data): |
|
|
|
|
|
with open("imageToSave.png", "wb") as fh: |
|
fh.write(base64.decodebytes(img_data)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|