File size: 9,341 Bytes
e9c4101
 
 
 
 
 
 
 
 
6ea0852
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9c4101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea0852
8652429
e9c4101
 
 
 
 
 
 
6ea0852
e9c4101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea0852
e9c4101
 
 
6ea0852
 
e9c4101
 
 
8652429
 
e9c4101
 
 
 
 
8652429
e9c4101
8652429
6ea0852
8652429
 
 
 
 
 
 
 
6ea0852
8652429
6ea0852
 
 
 
 
8652429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea0852
e9c4101
8652429
 
e9c4101
6ea0852
8652429
e9c4101
6ea0852
 
 
 
e9c4101
6ea0852
 
 
 
 
 
e9c4101
6ea0852
 
 
 
 
e9c4101
8652429
6ea0852
 
e9c4101
8652429
 
 
 
 
 
 
 
 
 
 
 
 
6ea0852
8652429
e9c4101
 
6ea0852
 
 
 
 
e9c4101
6ea0852
 
e9c4101
8652429
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
import boto3
from PIL import Image
import io
import json
import pikepdf
# Example: converting this single page to an image
from pdf2image import convert_from_bytes
from tools.custom_image_analyser_engine import OCRResult, CustomImageRecognizerResult

def extract_textract_metadata(response):
    """Extracts metadata from an AWS Textract response."""

    print("Document metadata:", response['DocumentMetadata'])

    request_id = response['ResponseMetadata']['RequestId']
    pages = response['DocumentMetadata']['Pages']
    #number_of_pages = response['DocumentMetadata']['NumberOfPages']

    return str({
        'RequestId': request_id,
        'Pages': pages
        #,
        #'NumberOfPages': number_of_pages
    })

def analyse_page_with_textract(pdf_page_bytes, json_file_path):
    '''
    Analyse page with AWS Textract
    '''
    try:
        client = boto3.client('textract')
    except:
        print("Cannot connect to AWS Textract")
        return "", "", ""

    print("Analysing page with AWS Textract")
    
    # Convert the image to bytes using an in-memory buffer
    #image_buffer = io.BytesIO()
    #image.save(image_buffer, format='PNG')  # Save as PNG, or adjust format if needed
    #image_bytes = image_buffer.getvalue()

    #response = client.detect_document_text(Document={'Bytes': image_bytes})
    response = client.analyze_document(Document={'Bytes': pdf_page_bytes}, FeatureTypes=["SIGNATURES"])

    text_blocks = response['Blocks']
    request_metadata = extract_textract_metadata(response) # Metadata comes out as a string

    # Write the response to a JSON file
    with open(json_file_path, 'w') as json_file:
        json.dump(response, json_file, indent=4)  # indent=4 makes the JSON file pretty-printed

    print("Response has been written to output:", json_file_path)       
            
    return text_blocks, request_metadata


def convert_pike_pdf_page_to_bytes(pdf, page_num):
    # Create a new empty PDF
    new_pdf = pikepdf.Pdf.new()

    # Specify the page number you want to extract (0-based index)
    page_num = 0  # Example: first page

    # Extract the specific page and add it to the new PDF
    new_pdf.pages.append(pdf.pages[page_num])

    # Save the new PDF to a bytes buffer
    buffer = io.BytesIO()
    new_pdf.save(buffer)

    # Get the PDF bytes
    pdf_bytes = buffer.getvalue()

    # Now you can use the `pdf_bytes` to convert it to an image or further process
    buffer.close()

    #images = convert_from_bytes(pdf_bytes)
    #image = images[0]

    return pdf_bytes


def json_to_ocrresult(json_data, page_width, page_height):
    '''
    Convert the json response from textract to the OCRResult format used elsewhere in the code. Looks for lines, words, and signatures. Handwriting and signatures are set aside especially for later in case the user wants to override the default behaviour and redact all handwriting/signatures.
    '''
    all_ocr_results = []
    signature_or_handwriting_recogniser_results = []
    signature_recogniser_results = []
    handwriting_recogniser_results = []
    signatures = []
    handwriting = []

    combined_results = {}

    for text_block in json_data:

        is_signature = False
        is_handwriting = False

        

        if (text_block['BlockType'] == 'LINE') | (text_block['BlockType'] == 'SIGNATURE'): # (text_block['BlockType'] == 'WORD') |

            if text_block['BlockType'] == 'LINE':
                # Extract text and bounding box for the line
                line_text = text_block.get('Text', '')
                line_bbox = text_block["Geometry"]["BoundingBox"]
                line_left = int(line_bbox["Left"] * page_width)
                line_top = int(line_bbox["Top"] * page_height)
                line_right = int((line_bbox["Left"] + line_bbox["Width"]) * page_width)
                line_bottom = int((line_bbox["Top"] + line_bbox["Height"]) * page_height)

                words = []
                if 'Relationships' in text_block:
                    for relationship in text_block['Relationships']:
                        if relationship['Type'] == 'CHILD':
                            for child_id in relationship['Ids']:
                                child_block = next((block for block in json_data if block['Id'] == child_id), None)
                                if child_block and child_block['BlockType'] == 'WORD':
                                    word_text = child_block.get('Text', '')
                                    word_bbox = child_block["Geometry"]["BoundingBox"]
                                    confidence = child_block.get('Confidence','')
                                    word_left = int(word_bbox["Left"] * page_width)
                                    word_top = int(word_bbox["Top"] * page_height)
                                    word_right = int((word_bbox["Left"] + word_bbox["Width"]) * page_width)
                                    word_bottom = int((word_bbox["Top"] + word_bbox["Height"]) * page_height)

                                    # Extract BoundingBox details
                                    width = word_bbox["Width"]
                                    height = word_bbox["Height"]

                                    # Convert proportional coordinates to absolute coordinates
                                    width_abs = int(width * page_width)
                                    height_abs = int(height * page_height)
                                    
                                    words.append({
                                        'text': word_text,
                                        'bounding_box': (word_left, word_top, word_right, word_bottom)
                                    })
                                    # Check for handwriting
                                    text_type = child_block.get("TextType", '')

                                    if text_type == "HANDWRITING":
                                        is_handwriting = True
                                        entity_name = "HANDWRITING"
                                        word_end = len(entity_name)
                                        recogniser_result = CustomImageRecognizerResult(entity_type=entity_name, text= word_text, score= confidence, start=0, end=word_end, left=word_left, top=word_top, width=width_abs, height=height_abs)
                                        handwriting.append(recogniser_result)                    
                                        print("Handwriting found:", handwriting[-1]) 

                combined_results[line_text] = {
                    'bounding_box': (line_left, line_top, line_right, line_bottom),
                    'words': words
                }

                

                # If handwriting or signature, add to bounding box               

            elif (text_block['BlockType'] == 'SIGNATURE'):
                line_text = "SIGNATURE"

                is_signature = True
                entity_name = "SIGNATURE"
                confidence = text_block['Confidence']
                word_end = len(entity_name)

                # Extract BoundingBox details
                bbox = text_block["Geometry"]["BoundingBox"]
                left = bbox["Left"]
                top = bbox["Top"]
                width = bbox["Width"]
                height = bbox["Height"]

                # Convert proportional coordinates to absolute coordinates
                left_abs = int(left * page_width)
                top_abs = int(top * page_height)
                width_abs = int(width * page_width)
                height_abs = int(height * page_height)

                recogniser_result = CustomImageRecognizerResult(entity_type=entity_name, text= line_text, score= confidence, start=0, end=word_end, left=left_abs, top=top_abs, width=width_abs, height=height_abs)
                signatures.append(recogniser_result)
                print("Signature found:", signatures[-1])

            # Extract BoundingBox details
            bbox = text_block["Geometry"]["BoundingBox"]
            left = bbox["Left"]
            top = bbox["Top"]
            width = bbox["Width"]
            height = bbox["Height"]

            # Convert proportional coordinates to absolute coordinates
            left_abs = int(left * page_width)
            top_abs = int(top * page_height)
            width_abs = int(width * page_width)
            height_abs = int(height * page_height)

            # Create OCRResult with absolute coordinates
            ocr_result = OCRResult(line_text, left_abs, top_abs, width_abs, height_abs)
            all_ocr_results.append(ocr_result)

            is_signature_or_handwriting = is_signature | is_handwriting

            # If it is signature or handwriting, will overwrite the default behaviour of the PII analyser
            if is_signature_or_handwriting:
                signature_or_handwriting_recogniser_results.append(recogniser_result)

                if is_signature: signature_recogniser_results.append(recogniser_result)
                if is_handwriting: handwriting_recogniser_results.append(recogniser_result)
    
    return all_ocr_results, signature_or_handwriting_recogniser_results, signature_recogniser_results, handwriting_recogniser_results, combined_results