Matthias Bisping a9d60654f5 Pull request #3: Refactoring
Merge in RR/image-prediction from refactoring to master

Squashed commit of the following:

commit fc4e2efac113f2e307fdbc091e0a4f4e3e5729d3
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 14:21:05 2022 +0100

    applied black

commit 3baabf5bc0b04347af85dafbb056f134258d9715
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 14:20:30 2022 +0100

    added banner

commit 30e871cfdc79d0ff2e0c26d1b858e55ab1b0453f
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 14:02:26 2022 +0100

    rename logger

commit d76fefd3ff0c4425defca4db218ce4a84c6053f3
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 14:00:39 2022 +0100

    logger refactoring

commit 0e004cbd21ab00b8804901952405fa870bf48e9c
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 14:00:08 2022 +0100

    logger refactoring

commit 49e113f8d85d7973b73f664779906a1347d1522d
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 13:25:08 2022 +0100

    refactoring

commit 7ec3d52e155cb83bed8804d2fee4f5bdf54fb59b
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 13:21:52 2022 +0100

    applied black

commit 06ea0be8aa9344e11b9d92fd526f2b73061bc736
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date:   Wed Mar 16 13:21:20 2022 +0100

    refactoring
2022-03-16 15:07:30 +01:00

71 lines
2.8 KiB
Python

"""Defines functions for constructing service responses."""
import math
from itertools import starmap
from operator import itemgetter
from image_prediction.config import CONFIG
def build_response(predictions: list, metadata: list) -> list:
return list(starmap(build_image_info, zip(predictions, metadata)))
def build_image_info(prediction: dict, metadata: dict) -> dict:
def compute_geometric_quotient():
page_area_sqrt = math.sqrt(abs(page_width * page_height))
image_area_sqrt = math.sqrt(abs(x2 - x1) * abs(y2 - y1))
return image_area_sqrt / page_area_sqrt
page_width, page_height, x1, x2, y1, y2, width, height = itemgetter(
"page_width", "page_height", "x1", "x2", "y1", "y2", "width", "height"
)(metadata)
quotient = compute_geometric_quotient()
min_image_to_page_quotient_breached = bool(quotient < CONFIG.filters.image_to_page_quotient.min)
max_image_to_page_quotient_breached = bool(quotient > CONFIG.filters.image_to_page_quotient.max)
min_image_width_to_height_quotient_breached = bool(
width / height < CONFIG.filters.image_width_to_height_quotient.min
)
max_image_width_to_height_quotient_breached = bool(
width / height > CONFIG.filters.image_width_to_height_quotient.max
)
min_confidence_breached = bool(max(prediction["probabilities"].values()) < CONFIG.filters.min_confidence)
prediction["label"] = prediction.pop("class") # "class" as field name causes problem for Java objectmapper
prediction["probabilities"] = {klass: round(prob, 6) for klass, prob in prediction["probabilities"].items()}
image_info = {
"classification": prediction,
"position": {"x1": x1, "x2": x2, "y1": y1, "y2": y2, "pageNumber": metadata["page_idx"] + 1},
"geometry": {"width": width, "height": height},
"filters": {
"geometry": {
"imageSize": {
"quotient": quotient,
"tooLarge": max_image_to_page_quotient_breached,
"tooSmall": min_image_to_page_quotient_breached,
},
"imageFormat": {
"quotient": width / height,
"tooTall": min_image_width_to_height_quotient_breached,
"tooWide": max_image_width_to_height_quotient_breached,
},
},
"probability": {"unconfident": min_confidence_breached},
"allPassed": not any(
[
max_image_to_page_quotient_breached,
min_image_to_page_quotient_breached,
min_image_width_to_height_quotient_breached,
max_image_width_to_height_quotient_breached,
min_confidence_breached,
]
),
},
}
return image_info