Merge in RR/image-prediction from RED-6084-adhoc-scanned-pages-filtering-refactoring to master
Squashed commit of the following:
commit bd6d83e7363b1c1993babcceb434110a6312c645
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Thu Feb 9 16:08:25 2023 +0100
Tweak logging
commit 55bdd48d2a3462a8b4a6b7194c4a46b21d74c455
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Thu Feb 9 15:47:31 2023 +0100
Update dependencies
commit 970275b25708c05e4fbe78b52aa70d791d5ff17a
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Thu Feb 9 15:35:37 2023 +0100
Refactoring
Make alpha channel check monadic to streamline error handling
commit e99e97e23fd8ce16f9a421d3e5442fccacf71ead
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Tue Feb 7 14:32:29 2023 +0100
Refactoring
- Rename
- Refactor image extraction functions
commit 76b1b0ca2401495ec03ba2b6483091b52732eb81
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Tue Feb 7 11:55:30 2023 +0100
Refactoring
commit cb1c461049d7c43ec340302f466447da9f95a499
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Tue Feb 7 11:44:01 2023 +0100
Refactoring
commit 092069221a85ac7ac19bf838dcbc7ab1fde1e12b
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Tue Feb 7 10:18:53 2023 +0100
Add to-do
commit 3cea4dad2d9703b8c79ddeb740b66a3b8255bb2a
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Tue Feb 7 10:11:35 2023 +0100
Refactoring
- Rename
- Add typehints everywhere
commit 865e0819a14c420bc2edff454d41092c11c019a4
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 19:38:57 2023 +0100
Add type explanation
commit 01d3d5d33f1ccb05aea1cec1d1577572b1a4deaa
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 19:37:49 2023 +0100
Formatting
commit dffe1c18fc3a322a6b08890d4438844e8122faaf
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 19:34:13 2023 +0100
[WIP] Either refactoring
Add alternative formulation for monadic chain
commit 066cf17add404a313520cd794c06e3264cf971c9
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 18:40:30 2023 +0100
[WIP] Either refactoring
commit f53f0fea298cdab88deb090af328b34d37e0198e
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 18:18:34 2023 +0100
[WIP] Either refactoring
Propagate error and metadata
commit 274a5f56d4fcb9c67fac5cf43e9412ec1ab5179e
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 17:51:35 2023 +0100
[WIP] Either refactoring
Fix test assertion
commit 3235a857f6e418e50484cbfff152b0f63efb2f53
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 16:57:31 2023 +0100
[WIP] Either-refactoring
Replace Maybe with Either to allow passing on error information or
metadata which otherwise get sucked up by Nothing.
commit 89989543d87490f8b20a0a76055605d34345e8f4
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 16:12:40 2023 +0100
[WIP] Monadic refactoring
Integrate image validation step into monadic chain.
At the moment we lost the error information through this. Refactoring to
Either monad can bring it back.
commit 022bd4856a51aa085df5fe983fd77b99b53d594c
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 15:16:41 2023 +0100
[WIP] Monadic refactoring
commit ca3898cb539607c8c3dd01c57e60211a5fea8a7d
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 15:10:34 2023 +0100
[WIP] Monadic refactoring
commit d8f37bed5cbd6bdd2a0b52bae46fcdbb50f9dff2
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 15:09:51 2023 +0100
[WIP] Monadic refactoring
commit 906fee0e5df051f38076aa1d2725e52a182ade13
Author: Matthias Bisping <matthias.bisping@axbit.com>
Date: Mon Feb 6 15:03:35 2023 +0100
[WIP] Monadic refactoring
... and 35 more commits
155 lines
5.2 KiB
Python
155 lines
5.2 KiB
Python
import json
|
|
import math
|
|
import os
|
|
from functools import lru_cache
|
|
from operator import itemgetter
|
|
|
|
from funcy import first
|
|
|
|
from image_prediction.config import CONFIG
|
|
from image_prediction.exceptions import ParsingError
|
|
from image_prediction.transformer.transformer import Transformer
|
|
from image_prediction.utils import get_logger
|
|
|
|
logger = get_logger()
|
|
|
|
|
|
class ResponseTransformer(Transformer):
|
|
def transform(self, data):
|
|
logger.debug("ResponseTransformer.transform")
|
|
return build_image_info(data)
|
|
|
|
|
|
def build_image_info(data: dict) -> dict:
|
|
page_width, page_height, x1, x2, y1, y2, width, height, alpha = itemgetter(
|
|
"page_width", "page_height", "x1", "x2", "y1", "y2", "width", "height", "alpha"
|
|
)(data)
|
|
|
|
classification = data["classification"]
|
|
label = classification["label"]
|
|
representation = data["representation"]
|
|
|
|
geometric_quotient = round(compute_geometric_quotient(page_width, page_height, x2, x1, y2, y1), 4)
|
|
|
|
min_image_to_page_quotient_breached = bool(
|
|
geometric_quotient < get_class_specific_min_image_to_page_quotient(label)
|
|
)
|
|
max_image_to_page_quotient_breached = bool(
|
|
geometric_quotient > get_class_specific_max_image_to_page_quotient(label)
|
|
)
|
|
|
|
min_image_width_to_height_quotient_breached = bool(
|
|
width / height < get_class_specific_min_image_width_to_height_quotient(label)
|
|
)
|
|
max_image_width_to_height_quotient_breached = bool(
|
|
width / height > get_class_specific_max_image_width_to_height_quotient(label)
|
|
)
|
|
|
|
min_confidence_breached = bool(
|
|
max(classification["probabilities"].values()) < get_class_specific_min_classification_confidence(label)
|
|
)
|
|
|
|
image_info = {
|
|
"classification": classification,
|
|
"representation": representation,
|
|
"position": {"x1": x1, "x2": x2, "y1": y1, "y2": y2, "pageNumber": data["page_idx"] + 1},
|
|
"geometry": {"width": width, "height": height},
|
|
"alpha": alpha,
|
|
"filters": {
|
|
"geometry": {
|
|
"imageSize": {
|
|
"quotient": geometric_quotient,
|
|
"tooLarge": max_image_to_page_quotient_breached,
|
|
"tooSmall": min_image_to_page_quotient_breached,
|
|
},
|
|
"imageFormat": {
|
|
"quotient": round(width / height, 4),
|
|
"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
|
|
|
|
|
|
def compute_geometric_quotient(page_width, page_height, x2, x1, y2, y1):
|
|
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
|
|
|
|
|
|
def get_class_specific_min_image_to_page_quotient(label, table=None):
|
|
return get_class_specific_value(
|
|
"REL_IMAGE_SIZE", label, "min", CONFIG.filters.image_to_page_quotient.min, table=table
|
|
)
|
|
|
|
|
|
def get_class_specific_max_image_to_page_quotient(label, table=None):
|
|
return get_class_specific_value(
|
|
"REL_IMAGE_SIZE", label, "max", CONFIG.filters.image_to_page_quotient.max, table=table
|
|
)
|
|
|
|
|
|
def get_class_specific_min_image_width_to_height_quotient(label, table=None):
|
|
return get_class_specific_value(
|
|
"IMAGE_FORMAT", label, "min", CONFIG.filters.image_width_to_height_quotient.min, table=table
|
|
)
|
|
|
|
|
|
def get_class_specific_max_image_width_to_height_quotient(label, table=None):
|
|
return get_class_specific_value(
|
|
"IMAGE_FORMAT", label, "max", CONFIG.filters.image_width_to_height_quotient.max, table=table
|
|
)
|
|
|
|
|
|
def get_class_specific_min_classification_confidence(label, table=None):
|
|
return get_class_specific_value("CONFIDENCE", label, "min", CONFIG.filters.min_confidence, table=table)
|
|
|
|
|
|
def get_class_specific_value(prefix, label, bound, fallback_value, table=None):
|
|
def fallback():
|
|
return fallback_value
|
|
|
|
def success():
|
|
threshold_map = parse_env_var(prefix, table=table) or {}
|
|
value = threshold_map.get(label, {}).get(bound)
|
|
if value:
|
|
logger.debug(f"Using class '{label}' specific {bound} {prefix.lower().replace('_', '-')} value.")
|
|
return value
|
|
|
|
assert bound in ["min", "max"]
|
|
|
|
return success() or fallback()
|
|
|
|
|
|
@lru_cache(maxsize=None)
|
|
def parse_env_var(prefix, table=None):
|
|
table = table or os.environ
|
|
head = first(filter(lambda s: s == prefix, table))
|
|
if head:
|
|
try:
|
|
return parse_env_var_value(table[head])
|
|
except ParsingError as err:
|
|
logger.warning(err)
|
|
else:
|
|
return None
|
|
|
|
|
|
def parse_env_var_value(env_var_value):
|
|
try:
|
|
return json.loads(env_var_value)
|
|
except Exception as err:
|
|
raise ParsingError(f"Failed to parse {env_var_value}") from err
|