File size: 6,260 Bytes
f496f54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
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.
'''

# from privacy.util.encrypt import EncryptImage

from presidio_image_redactor import ImageRedactorEngine,ImageAnalyzerEngine,ImagePiiVerifyEngine,OCR
# from privacy.service import easyocr_analyzer       
from dotenv import load_dotenv
from privacy.config.logger import CustomLogger
# pytesseract.pytesseract.tesseract_cmd = r"C:\Users\amitumamaheshwar.h\AppData\Local\Programs\Tesseract-OCR"
import time
load_dotenv()
import os
import sys
import requests

from PIL import Image
from io import BytesIO

log = CustomLogger()

# log = CustomLogger()
# output_type = easyocr.Reader(['en'],model_storage_directory=r"privacy/util/model",download_enabled=False)
        
class AttributeDict(dict):
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__
class Data:
    encrypted_text=[]
    
apikey=os.getenv("API_KEY")
apiendpint=os.getenv("API_ENDPOINT")
# image_path=r"C:\WORK\GIT\responsible-ai-admin\responsible-ai-admin\src\rai_admin\temp\MicrosoftTeams-image (4).png"
# # Read the image into a byte array
# image_data = open(image_path, "rb").read()
# Set Content-Type to octet-stream
headers = {'Ocp-Apim-Subscription-Key': apikey, 'Content-Type': 'application/octet-stream'}
params = {'language': 'en', 'detectOrientation': 'true','features':['read']}
class ComputerVision(OCR):

    def process(a):
        return a

            
        
        
    # def perform_ocr(self, image: object, **kwargs) -> dict:
    #     # output_type = easyocr.Reader(['en'])
    #     s=time.time()
    #     # response(code)
        
    #     textmap={"text":[],"left":[],"top":[],"width":[],"height":[],"conf":[]}
    #     buffer = BytesIO()

    #     # Save the image to the in-memory buffer in PNG format
    #     image.save(buffer, "PNG")

    #     # Get the binary data from the buffer (seek to the beginning)
    #     binary_data = buffer.getvalue()
    #     image_data = binary_data
    #     # put the byte array into your post request
    #     response = requests.post(apiendpint, headers=headers, params=params, data = image_data)

    #     response.raise_for_status()

    #     analysis = response.json()
    #     line_infos = [region["lines"] for region in analysis["regions"]]
    #     word_infos = []
    #     for line in line_infos:
    #         for word_metadata in line:
    #             for word_info in word_metadata["words"]:
    #                 word_infos.append(word_info)

    #     for val in word_infos:
    #         boxval=val["boundingBox"].split(',')
    #         textmap["text"].append(val["text"])
    #         textmap["left"].append(int(boxval[0]))
    #         textmap["top"].append(int(boxval[1]))
    #         textmap["width"].append(int(boxval[2]))
    #         textmap["height"].append(int(boxval[3]))
    #         # textmap["conf"].append(val["conf"])
    #         # text.append(val["text"])
    #         # left.append(min(val['coordinates'][0][0],val["coordinates"][3][0]))
    #         # top.append(min(val['coordinates'][0][1],val["coordinates"][1][1]))
    #         # width.append(abs(val['coordinates'][1][0]-val["coordinates"][0][0]))
    #         # height.append(abs(val['coordinates'][3][1]-val["coordinates"][0][1]))
    #         # conf.append(val["conf"])
    #     print(textmap)
    #     log.warn("time======="+str(time.time()-s))
    #     return textmap
            

    def perform_ocr(self, image: object, **kwargs) -> dict:
        # output_type = easyocr.Reader(['en'])
        s=time.time()
        # response(code)
        
        textmap={"text":[],"left":[],"top":[],"width":[],"height":[],"conf":[]}
        buffer = BytesIO()

        # Save the image to the in-memory buffer in PNG format
        image.save(buffer, "PNG")

        # Get the binary data from the buffer (seek to the beginning)
        binary_data = buffer.getvalue()
        image_data = binary_data
        # put the byte array into your post request
        response = requests.post(apiendpint, headers=headers, params=params, data = image_data)

        response.raise_for_status()

        analysis = response.json()
        print(analysis)
        line_infos = [region["lines"] for region in analysis["readResult"]["blocks"]]
        textmap={"text":[],"left":[],"top":[],"width":[],"height":[],"conf":[]}
        for line in line_infos:
            for word_metadata in line:
                for val in word_metadata["words"]:
                    # word_infos.append(val)
                    textmap["text"].append(val["text"])
                    textmap["left"].append(min(val['boundingPolygon'][0]['x'],val["boundingPolygon"][3]['x']))
                    textmap["top"].append(min(val['boundingPolygon'][0]['y'],val["boundingPolygon"][1]['y']))
                    textmap["width"].append(abs(val['boundingPolygon'][1]['x']-val["boundingPolygon"][0]['x']))
                    textmap["height"].append(abs(val['boundingPolygon'][3]['y']-val["boundingPolygon"][0]['y']))
                    textmap["conf"].append(val["confidence"])
        print(textmap)
        log.warn("time======="+str(time.time()-s))
        return textmap