File size: 8,890 Bytes
54fa0c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
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 cv2
from PIL import Image, ImageChops
from presidio_image_redactor import ImageRedactorEngine
from presidio_image_redactor.image_analyzer_engine import ImageAnalyzerEngine
import matplotlib
import io
from numpy import asarray
from matplotlib import pyplot as plt
import string
import random
from typing import Optional
import time
from presidio_anonymizer import AnonymizerEngine
from presidio_image_redactor import ImageAnalyzerEngine, BboxProcessor
from PIL import Image, ImageDraw, ImageChops
from presidio_anonymizer.entities import (RecognizerResult,
    OperatorResult,
    OperatorConfig)
from typing import Union, Tuple, Optional

def fig2img(fig):
    """Convert a Matplotlib figure to a PIL Image and return it."""

    buf = io.BytesIO()
    fig.savefig(buf)
    buf.seek(0)
    img = Image.open(buf)
    return img



class Detect:
    def getFace(image:Image):
        # print(type(image))
        image= asarray(image)
        # print(image)
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
        # print(len(image.shape))
        # print(image.shape)
        if(len(image.shape)==3):
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        elif(len(image.shape)==2):
            gray = image
        else:
            gray = image
        # print(gray)

# Detect faces in the image
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
        # print("done")
        # print(faces)
# Extract and save each detected face
        for i, (x, y, w, h) in enumerate(faces):
            face = image[y:y+h, x:x+w]
            # print(x,y,w,h)
            
            # cv2.imwrite(f'C:\\WORK\\GIT\\responsible-ai-privacy\\responsible-ai-privacy\\src\\face_{i}.jpg', face)
            # print("returning")
            return(x,y,w,h)
        
class EncryptImage:
    text=""
    entity=[]
    def __init__(self, image_analyzer_engine: Optional[ImageAnalyzerEngine] = None):
        if not image_analyzer_engine:
            image_analyzer_engine = ImageAnalyzerEngine()
        self.image_analyzer_engine = image_analyzer_engine
        self.bbox_processor = BboxProcessor()
        # redactorEngine = ImageRedactorEngine(image_analyzer_engine=self.image_analyzer_engine)
    
    def getText(self,image:Image,ocr_kwargs:Optional[dict] = None,**analyzer_data):
        # "Code/Functions from image_analyzer_engine"
        print(type(image))
        perform_ocr_kwargs, ocr_threshold = self.image_analyzer_engine._parse_ocr_kwargs(ocr_kwargs)
        ocr_result = self.image_analyzer_engine.ocr.perform_ocr(image, **perform_ocr_kwargs)

        
        # Apply OCR confidence threshold if it is passed in
        if ocr_threshold:
            ocr_result = self.image_analyzer_engine.threshold_ocr_result(ocr_result, ocr_threshold)

        # Analyze text
        text = self.image_analyzer_engine.ocr.get_text_from_ocr_dict(ocr_result)
        EncryptImage.text=text
        
    def imageAnonimyze( self,
        image: Image,
        fill: Union[int, Tuple[int, int, int]] = (0, 0, 0),
        ocr_kwargs: Optional[dict] = None,
        encryptionList:Optional[list]=[],
        **text_analyzer_kwargs,):
        
        """"Code/Functions from Image Redactor"""
        image = ImageChops.duplicate(image)

        bboxes = self.image_analyzer_engine.analyze(
            image, ocr_kwargs, **text_analyzer_kwargs
        )
        
        # EncryptImage.entity.extend(bboxes)
        print("box==========",bboxes)
        draw = ImageDraw.Draw(image)
        
        for box in bboxes:
            if(box.entity_type in encryptionList):
                
                EncryptImage.entity.append(box)
            x0 = box.left
            y0 = box.top
            x1 = x0 + box.width
            y1 = y0 + box.height
            # print("=================",x0,y0)
            draw.rectangle([x0, y0, x1, y1], fill=fill)
        if("entities" in text_analyzer_kwargs):
            if("Face_Detect" in text_analyzer_kwargs["entities"]):
                res=Detect.getFace(image)
                if(res==None):
                    pass
                elif(len(res)==4):
                    x,y,w,h=res
                    draw.rectangle([x-(x*.05),y-(x*.05),x+w+(w*.25),y+h+(h*.25)], fill=fill)
        # x0,y0,x1,y1=(738, 1028, 217, 58)
        # draw.rectangle([x-(x*.05),y-(x*.05),x+w+(w*.25),y+h+(h*.25)], fill=fill)
                
      
        return image

        
        
    def dis():
        print("===========",EncryptImage.text)
        print("---------",EncryptImage.text.title())
        # print("==========",EncryptImage.entity)
        print("--------------------")
        
    def encrypt(self,image:Image,ocr_kwargs:Optional[dict] = None,encryptionList=[],**analyzer_data):
        """"Code Reference from Image Verify"""
        image = ImageChops.duplicate(image)
        image_x, image_y = image.size
        
        
        bboxes=EncryptImage.entity
        text=EncryptImage.text
        # print("boxA==",bboxes)
        dict_operators={}
        if encryptionList is not None and len(encryptionList) >0 :
            for entity in encryptionList:
                dict_operators.update({entity: OperatorConfig("hash", {"hash-type": 'md5'})})
        else:
            dict_operators = None
            
        # print("dic=",dict_operators)
        analyzer_result=bboxes
        # print(analyzer_result)
        anonymizeEngine=AnonymizerEngine()
        anonymizeResult=anonymizeEngine.anonymize(text=text,
                                        operators=dict_operators,
                                              analyzer_results=analyzer_result)
        # print(r.items)
        
        # print("==========",anonymizeResult.items)
        result=anonymizeResult.items
        result=sorted(result, key=lambda i: i.start)
        
        
        # print("RRRRRRRRR=========",len(result),len(bboxes))
        fig, ax = plt.subplots()
        ax.axis('off')
        image_r = 70
        fig.set_size_inches(image_x / image_r, image_y / image_r)
        res=[]
        drawen=[]
        excess=0
        if len(bboxes) == 0:
            return (image,res)
        else:
            for i in range(len(bboxes)):
                box=bboxes[i]
                N=box.end-box.start
                if((box.start,box.end)in drawen):
                    excess+=1
                    continue
                # print("i====######",i)
                drawen.append((box.start,box.end))
                if((i-excess)<len(result)):
                        # print(drawen,result[i-excess])
                        id=' '.join(random.choices(string.ascii_uppercase +
                             string.digits, k=N//2))
                        entity_type =id
                        res.append({"id":id,"text":result[i-excess].text,"entity_type":result[i-excess].entity_type,"start":box.start,"end":box.end})  
                x0 = box.left-15
                y0 = box.top
                x1 = x0 + box.width
                y1 = y0 + box.height
                # ax.annotate(
                #     entity_type,
                #     xy=(x1-((x1-x0)/2) , y1-((y1-y0)/2) ),
                #     xycoords="data",
                #     bbox=dict(boxstyle="round4,pad=.5", fc="0.9"),
                # )
                ax.text(x1-((x1-x0)/2), y1-((y1-y0)/2), entity_type, fontsize=12,bbox=dict(facecolor='white', alpha=0.5))
            # print("====",res)
            ax.imshow(image)
            im_from_fig = fig2img(fig)
            im_resized = im_from_fig.resize((image_x, image_y))
            im_resized = im_from_fig
            return (im_resized,res)