File size: 6,616 Bytes
c770c67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from client import get_textract_client
from io import BytesIO

def extract_data_from_image(image_input):
    # Initialize Textract client
    client = get_textract_client()
    
    # Handle both file paths and BytesIO objects
    if isinstance(image_input, (str, bytes)):
        # If it's a file path or bytes
        if isinstance(image_input, str):
            with open(image_input, 'rb') as document:
                image_bytes = document.read()
        else:
            image_bytes = image_input
    elif isinstance(image_input, BytesIO):
        # If it's BytesIO
        image_bytes = image_input.getvalue()
    else:
        raise ValueError("Invalid input type. Expected str, bytes, or BytesIO")
    
    # Process the image document
    response = client.analyze_document(
        Document={'Bytes': image_bytes},
        FeatureTypes=["TABLES", "FORMS"]  # Added FORMS to extract key-value pairs
    )
    
    # Process Textract response
    def process_blocks(blocks):
        text_lines = []
        tables = []
        key_values = []
        
        # First, create a dictionary of all blocks for lookup
        block_map = {block['Id']: block for block in blocks}
        
        for block in blocks:
            block_type = block['BlockType']
            
            if block_type == "LINE":
                text_lines.append(block['Text'])
                
            elif block_type == "TABLE":
                table_cells = {}
                rows = []
                
                # Get cells for this table
                if 'Relationships' in block and any(rel['Type'] == 'CHILD' for rel in block['Relationships']):
                    for rel in block['Relationships']:
                        if rel['Type'] == 'CHILD':
                            for cell_id in rel['Ids']:
                                cell_block = block_map[cell_id]
                                if cell_block['BlockType'] == 'CELL':
                                    row_index = cell_block['RowIndex']
                                    col_index = cell_block['ColumnIndex']
                                    
                                    # Get cell content
                                    cell_text = ""
                                    if 'Relationships' in cell_block and any(rel['Type'] == 'CHILD' for rel in cell_block['Relationships']):
                                        child_ids = next(rel['Ids'] for rel in cell_block['Relationships'] if rel['Type'] == 'CHILD')
                                        for child_id in child_ids:
                                            child_block = block_map[child_id]
                                            if child_block['BlockType'] in ['WORD', 'LINE']:
                                                cell_text += child_block['Text'] + " "
                                    
                                    if not cell_text and 'Text' in cell_block:
                                        cell_text = cell_block['Text']
                                        
                                    cell_text = cell_text.strip()
                                    
                                    # Create row if not exists
                                    if row_index not in table_cells:
                                        table_cells[row_index] = {}
                                    
                                    # Add cell to row
                                    table_cells[row_index][col_index] = cell_text
                
                # Format table as tab-separated rows
                for row_idx in sorted(table_cells.keys()):
                    row_data = table_cells[row_idx]
                    row_text = "\t".join([row_data.get(col_idx, "") for col_idx in sorted(row_data.keys())])
                    rows.append(row_text)
                
                if rows:
                    tables.append("\n".join(rows))
            
            elif block_type == "KEY_VALUE_SET":
                if 'EntityTypes' in block and 'KEY' in block['EntityTypes']:
                    key_block = block
                    key_text = ""
                    value_text = ""
                    
                    # Get key text
                    if 'Relationships' in key_block:
                        for relationship in key_block['Relationships']:
                            if relationship['Type'] == 'CHILD':
                                for child_id in relationship['Ids']:
                                    child_block = block_map[child_id]
                                    if child_block['BlockType'] == 'WORD':
                                        key_text += child_block['Text'] + " "
                    
                    # Get value block
                    if 'Relationships' in key_block:
                        for relationship in key_block['Relationships']:
                            if relationship['Type'] == 'VALUE':
                                for value_id in relationship['Ids']:
                                    value_block = block_map[value_id]
                                    
                                    # Get value text
                                    if 'Relationships' in value_block:
                                        for value_relationship in value_block['Relationships']:
                                            if value_relationship['Type'] == 'CHILD':
                                                for value_child_id in value_relationship['Ids']:
                                                    value_child_block = block_map[value_child_id]
                                                    if value_child_block['BlockType'] == 'WORD':
                                                        value_text += value_child_block['Text'] + " "
                    
                    if key_text and value_text:
                        key_values.append(f"{key_text.strip()}: {value_text.strip()}")
        
        return {
            'text': '\n'.join(text_lines),
            'tables': tables,
            'key_values': key_values
        }

    processed_data = process_blocks(response['Blocks'])
    formatted_output = (
        f"Text Content:\n{processed_data['text']}\n\n"
        f"Key-Value Pairs:\n{chr(10).join(processed_data['key_values'])}\n\n"
        f"Tables:\n" + "\n\n".join(processed_data['tables'])
    )
    return formatted_output