FastAPI-Img2CSV / extract.py
utkarsh134's picture
Upload 7 files
c770c67 verified
Raw History Blame Contribute Delete
6.62 kB
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