in src/co_op_translator/core/vision/image_translator.py [0:0]
def extract_line_bounding_boxes(self, image_path):
"""
Extract line bounding boxes from an image using Azure Analysis Client.
Args:
image_path (str): Path to the image file.
Returns:
list: List of dictionaries containing text, bounding box coordinates, and confidence scores.
Raises:
Exception: If the OCR operation did not succeed.
"""
image_analysis_client = self.get_image_analysis_client()
with open(image_path, "rb") as image_stream:
image_data = image_stream.read()
result = image_analysis_client.analyze(
image_data=image_data,
visual_features=[VisualFeatures.READ],
)
if result.read is not None and result.read.blocks:
line_bounding_boxes = []
for line in result.read.blocks[0].lines:
bounding_box = []
for point in line.bounding_polygon:
bounding_box.append(point.x)
bounding_box.append(point.y)
line_bounding_boxes.append(
{
"text": line.text,
"bounding_box": bounding_box,
"confidence": line.words[0].confidence if line.words else None,
}
)
logger.info(
f"Extracted {len(line_bounding_boxes)} bounding boxes from {image_path}"
)
return line_bounding_boxes
else:
raise Exception("No text was recognized in the image.")