diff --git a/app/main/controller/image_ocr_controller.py b/app/main/controller/image_ocr_controller.py index 83f0cfdf..63d6e97a 100644 --- a/app/main/controller/image_ocr_controller.py +++ b/app/main/controller/image_ocr_controller.py @@ -18,6 +18,32 @@ def _after_log(retry_state): CLIENT = get_credentialed_google_client(vision.ImageAnnotatorClient) @api.route('/') class ImageOcrResource(Resource): + @staticmethod + def polygon_area(vertices): + area = 0 + for i in range(len(vertices)): + x1, y1 = vertices[i] + x2, y2 = vertices[(i + 1) % len(vertices)] + area += (x1 * y2 - x2 * y1) + return abs(area) / 2 + + @staticmethod + def calculate_text_percentage(response): + bounds = [] + for page in response.full_text_annotation.pages: + for block in page.blocks: + bounds.append(block.bounding_box) + total_text_area = 0 + for annotation in bounds: + vertices = [(v.x, v.y) for v in annotation.vertices] + area = ImageOcrResource.polygon_area(vertices) + total_text_area += area + # response object contains the whole image width and height in response.full_text_annotation.pages[0] + # as we are sending images, response.full_text_annotation.pages is always 1 page only + image_area = response.full_text_annotation.pages[0].width * response.full_text_annotation.pages[0].height + text_percentage = (total_text_area / image_area) * 100 + return text_percentage + @api.response(200, 'text successfully extracted.') @api.doc('Perform text extraction from an image') @api.doc(params={'url': 'url of image to extract text from'}) @@ -37,8 +63,13 @@ def post(self): if not texts: return - app.logger.info( - f"[Alegre OCR] [image_uri {image.source.image_uri}] Image OCR response package looks like {convert_text_annotation_to_json(texts[0])}") + #### calculate bounding boxes areas. + try: + text_percentage = ImageOcrResource.calculate_text_percentage(response) + app.logger.info( + f"[Alegre OCR] [image_uri {image.source.image_uri}] [percentage of image area covered by text {text_percentage}%] Image OCR response package looks like {convert_text_annotation_to_json(texts[0])}") + except Exception as caught_exception: + app.logger.error(f"[image_uri {image.source.image_uri}] Error calculating percentage of image area covered by text. Error was {caught_exception}. Image OCR response package looks like {convert_text_annotation_to_json(texts[0])}") return { 'text': texts[0].description