File size: 30,305 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
'''
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 io, base64
from PIL import Image
from privacy.service.easy import EasyOCR
from privacy.service.azureComputerVision import ComputerVision
# from privacy.dao.TelemetryFlagDb import TelemetryFlag
from privacy.mappers.mappers import *
from privacy.util.encrypt import EncryptImage
from typing import List
from privacy.constants.local_constants import (DELTED_SUCCESS_MESSAGE)
from privacy.config.logger import CustomLogger
#import zipfile
from zipfile import ZipFile,is_zipfile
from dotenv import load_dotenv
from privacy.config.logger import request_id_var
load_dotenv()
import numpy as np
import cv2
# from privacy.util.flair_recognizer import FlairRecognizer 
log = CustomLogger()
import time
import pytesseract
from scipy import ndimage
from PIL import Image as im 
from privacy.util.special_recognizers.DataListRecognizer import DataListRecognizer
# global error_dict
from privacy.service.__init__ import *
from privacy.service.api_req import ApiCall

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
        # t=time.time()
        if(preAngle==0):
            angle=ImageRotation.getAngle(image)
        # print("angle:",time.time()-t)
        if(preAngle==angle):
            return (image,angle)
        img_rotated = ndimage.rotate(image, preAngle-angle)
        image = im.fromarray(img_rotated)
        return (image,angle)

class ImagePrivacy:
    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)
           
            # analyzer,registry=ConfModle.getAnalyzerEngin("en_core_web_lg")
            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)  
                # print(time.time()-tt)
            if(payload.easyocr=="ComputerVision"):
                ocr=ComputerVision()
                # EasyOCR.setMag(payload.mag_ratio)

                imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr)  
                # imageRedactorEngine = ImageRedactorEngine(image_analyzer_engine=imageAnalyzerEngine)
            
            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):
                    # print( response_value)
                    return response_value
                entityType,datalist,preEntity=response_value
                # entityType,datalist,preEntity=ApiCall.request(payload)
                # preEntity=["PERSON"]
                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)
                            # log.debug("++++++"+str(entityType[d]))
                            # results = engine.analyze(image,entities=[entityType[d]])
                            # result.extend(results)
                    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)
                        # log.debug("==========="+str(entityType[d]))                   
                        # results = engine.analyze(image,entities=[entityType[d]])
                        # result.extend(results)
                results = imageAnalyzerEngine.analyze(image,entities=entityType+preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"])
                result.extend(results)
                    # results = PrivacyService.__analyze(text=payload.inputText,accName=accMasterid.accMasterId)
                # if(len(preEntity)>0):               
                #         results = imageAnalyzerEngine.analyze(image,entities=preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"])
                #         preEntity.clear()
                #         result.extend(results)
                results=result                               

            #log.debug(f"results: {results}")

            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")
            # ApiCall.encryptionList.clear()
            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)
        #log.debug(f"results: {results}")
        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)
            # analyzer,registry=ConfModle.getAnalyzerEngin("en_core_web_lg")
            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()
                # EasyOCR.setMag(payload.mag_ratio)

                imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr)  
                imageRedactorEngine = ImageRedactorEngine(image_analyzer_engine=imageAnalyzerEngine)
            # engine = ImageRedactorEngine()
            payload=AttributeDict(payload)
            image = Image.open(payload.image.file)
          
            
            angle=0
            if(payload.rotationFlag):
                image,angle=ImageRotation.rotateImage(image)
         
            # registry.load_predefined_recognizers()
            # log.debug("payload.image.file====="+str(payload.image.file))
            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):
                    # print( response_value)
                    return response_value
                entityType,datalist,preEntity=response_value
                # entityType,datalist,preEntity=ApiCall.request(payload)
                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)
                            # log.debug("++++++"+str(entityType[d]))
                            # results = engine.analyze(image,entities=[entityType[d]])
                            # redacted_image = engine.redact(image, (255, 192, 203),entities=[entityType[d]])
                            # processed_image_stream = io.BytesIO()
                            # redacted_image.save(processed_image_stream, format='PNG')
                    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)
                        # log.debug("=="+str(entityType[d]))
                        # results = engine.analyze(image,entities=[entityType[d]])
                redacted_image = imageRedactorEngine.redact(image, (255, 192, 203),entities=entityType+preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"])
                        # log.debug("redacted_image=="+str(redacted_image))
                processed_image_stream = io.BytesIO()
                redacted_image.save(processed_image_stream, format='PNG')
                    # log.debug("redacted_image="+str(redacted_image))  
                image=redacted_image
                    # results = PrivacyService.__analyze(text=payload.inputText,accName=accMasterid.accMasterId)
                # if(len(preEntity)>0):
                #         redacted_image = imageRedactorEngine.redact(image, (255, 192, 203),entities=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')
                #         preEntity.clear()
            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')
            # redacted_image.show()
            # redacted_image = engine.redact(image, (255, 192, 203),entities=preEntity)
            # 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)
            saveImage.saveImg(base64_encoded_image)
            log.debug("Returning from image_anonymize function")
            # ApiCall.encryptionList.clear()
            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)
        # Threshold the template image to create a binary mask
        _, template_mask = cv2.threshold(template_gray, 1, 255, cv2.THRESH_BINARY)

        # Perform template matching
        result = cv2.matchTemplate(main_image, template_image, cv2.TM_CCOEFF_NORMED)
        _, max_val, _, max_loc = cv2.minMaxLoc(result)

        # Get the dimensions of the template image
        template_height, template_width = template_image.shape[:2]

        # Create a mask with the same size as the main image
        mask = np.zeros(main_image.shape[:2], dtype=np.uint8)

        # Set the region of interest (ROI) in the mask based on the template location
        mask[max_loc[1]:max_loc[1] + template_height, max_loc[0]:max_loc[0] + template_width] = 255

        # Apply the mask to the main image
        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:
                # analyzer,registry=ConfModle.getAnalyzerEngin("en_core_web_lg")
                # engine1 = ImageAnalyzerEngine(analyzer_engine=analyzer)
                # imagePiiVerifyEngine = ImagePiiVerifyEngine(image_analyzer_engine=imageAnalyzerEngine)
                # enginex=EncryptImage(image_analyzer_engine=engine1)
                global imagePiiVerifyEngine
                payload=AttributeDict(payload)
                image = Image.open(payload.image.file)
                # registry.load_predefined_recognizers()
                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):
                    # print( response_value)
                        return response_value
                    entityType,datalist,preEntity=response_value

                    # Al=ApiCall.encryptionList
                    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"])
                     #   verify_image = enginex.encrypt(image,encryptionList=Al,entities=[entityType[d]], allow_list=exclusionList)
                    processed_image_stream = io.BytesIO()
                    verify_image.save(processed_image_stream, format='PNG')
                    # log.debug("redacted_image="+str(redacted_image))  
                    image=verify_image
                        # results = PrivacyService.__analyze(text=payload.inputText,accName=accMasterid.accMasterId)
                    # if(len(preEntity)>0):
                    
                    #         verify_image = imagePiiVerifyEngine.verify(image,entities=preEntity, allow_list=exclusionList,score_threshold=admin_par[request_id_var.get()]["scoreTreshold"])
                    #      #    verify_image = enginex.encrypt(image,encryptionList=Al,entities=preEntity, allow_list=exclusionList)


                    #         processed_image_stream = io.BytesIO()
                    #         verify_image.save(processed_image_stream, format='PNG')
                    #         preEntity.clear()

                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")
                # ApiCall.encryptionList.clear()
                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()
                # analyzer,registry=ConfModle.getAnalyzerEngin("en_core_web_lg")

                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()
                    # EasyOCR.setMag(payload.mag_ratio)

                    imageAnalyzerEngine = ImageAnalyzerEngine(analyzer_engine=analyzer,ocr=ocr)  
                    encryptImageEngin=EncryptImage(image_analyzer_engine=imageAnalyzerEngine)
                # engine = ImageRedactorEngine(image_analyzer_engine=engine1)
                # engine = ImageRedactorEngine()
                payload=AttributeDict(payload)
                image = Image.open(payload.image.file)
                angle=0
                if(payload.rotationFlag):
                    image,angle=ImageRotation.rotateImage(image)
                # registry.load_predefined_recognizers()
                # log.debug("payload.image.file====="+str(payload.image.file))
                encryptMapper=[]
                if(payload.exclusion == None):
                    exclusionList=[]
                else:
                    exclusionList=payload.exclusion.split(",")
                encryptImageEngin.getText(image)
                if(payload.portfolio== None):
                    # redacted_image = engine.redact(image, (255, 192, 203), allow_list=exclusionList)
                    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)
                    # encryptionList=ApiCall.encryptionList
                    if(response_value==None):
                        return None
                    if(response_value==404):
                        # print( response_value)
                        return response_value
                    encryptionList=admin_par[request_id_var.get()]["encryptionList"]
                    entityType,datalist,preEntity=response_value
                    # entityType,datalist,preEntity=ApiCall.request(payload)
                    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)
                                # log.debug("++++++"+str(entityType[d]))
                                # results = engine.analyze(image,entities=[entityType[d]])
                                # redacted_image = engine.redact(image, (255, 192, 203),entities=[entityType[d]])
                                # processed_image_stream = io.BytesIO()
                                # redacted_image.save(processed_image_stream, format='PNG')
                        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)
                            # log.debug("=="+str(entityType[d]))    
                            # results = engine.analyze(image,entities=[entityType[d]])
                    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"])
                        # log.debug("redacted_image=="+str(redacted_image))
                    processed_image_stream = io.BytesIO()
                    redacted_image.save(processed_image_stream, format='PNG')
                    # log.debug("redacted_image="+str(redacted_image))  
                    image=redacted_image
                        # results = PrivacyService.__analyze(text=payload.inputText,accName=accMasterid.accMasterId)
                    # if(len(preEntity)>0):
                    #         redacted_image = encryptImageEngin.imageAnonimyze(image, (255, 192, 203),encryptionList=encryptionList,entities=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')
                    #         preEntity.clear()
                    
                    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')
                # redacted_image = engine.redact(image, (255, 192, 203),entities=preEntity)
                # 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)
                saveImage.saveImg(base64_encoded_image)
                obj={"map":encryptMapper,"img":base64_encoded_image}
                log.debug("Returning from imageEncryption function")
                # ApiCall.encryptionList.clear()
                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)
class saveImage:
    def saveImg(img_data):
        
    
        with open("imageToSave.png", "wb") as fh:
            fh.write(base64.decodebytes(img_data))