Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec2ab890b8 | ||
|
|
aaa02ea35e | ||
|
|
5d87066b40 | ||
|
|
23c61ef49e | ||
|
|
c1b9227035 | ||
|
|
ad9c5657fe | ||
|
|
c60e8cd678 | ||
|
|
101b71726c | ||
|
|
04aee4e627 | ||
|
|
4584e7ba66 | ||
|
|
5f99622646 |
@ -4,7 +4,7 @@ import os
|
|||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
|
|
||||||
from funcy import filter, juxt, first, rest, compose
|
from funcy import first
|
||||||
|
|
||||||
from image_prediction.config import CONFIG
|
from image_prediction.config import CONFIG
|
||||||
from image_prediction.exceptions import ParsingError
|
from image_prediction.exceptions import ParsingError
|
||||||
@ -20,45 +20,6 @@ class ResponseTransformer(Transformer):
|
|||||||
return build_image_info(data)
|
return build_image_info(data)
|
||||||
|
|
||||||
|
|
||||||
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():
|
|
||||||
logger.warning(f"Failed to resolve {bound} {prefix.lower().replace('_', '-')} value for class '{label}'.")
|
|
||||||
return fallback_value
|
|
||||||
|
|
||||||
assert bound in ["min", "max"]
|
|
||||||
|
|
||||||
threshold_map = parse_env_var(prefix, table=table) or {}
|
|
||||||
return threshold_map.get(label, {}).get(bound) or fallback()
|
|
||||||
|
|
||||||
|
|
||||||
def build_image_info(data: dict) -> dict:
|
def build_image_info(data: dict) -> dict:
|
||||||
def compute_geometric_quotient():
|
def compute_geometric_quotient():
|
||||||
page_area_sqrt = math.sqrt(abs(page_width * page_height))
|
page_area_sqrt = math.sqrt(abs(page_width * page_height))
|
||||||
@ -128,19 +89,61 @@ def build_image_info(data: dict) -> dict:
|
|||||||
return image_info
|
return image_info
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
@lru_cache(maxsize=None)
|
||||||
def parse_env_var(prefix, table=None):
|
def parse_env_var(prefix, table=None):
|
||||||
table = table or os.environ
|
table = table or os.environ
|
||||||
head, tail = juxt(first, compose(list, rest))(filter(prefix, table))
|
head = first(filter(lambda s: s == prefix, table))
|
||||||
if not head:
|
if head:
|
||||||
logger.warning(f"Found no environment variable with prefix '{prefix}'.")
|
|
||||||
elif tail:
|
|
||||||
logger.warning(f"Found multiple candidates for environment variable with prefix '{prefix}'.")
|
|
||||||
else:
|
|
||||||
try:
|
try:
|
||||||
return parse_env_var_value(table[head])
|
return parse_env_var_value(table[head])
|
||||||
except ParsingError as err:
|
except ParsingError as err:
|
||||||
logger.warning(err)
|
logger.warning(err)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def parse_env_var_value(env_var_value):
|
def parse_env_var_value(env_var_value):
|
||||||
|
|||||||
@ -21,8 +21,8 @@ def label():
|
|||||||
def page_quotient_threshold_map(label):
|
def page_quotient_threshold_map(label):
|
||||||
return frozendict(
|
return frozendict(
|
||||||
{
|
{
|
||||||
"REL_IMAGE_SIZE_MAP": json.dumps({label: {"min": 0.1, "max": 0.2}}),
|
"REL_IMAGE_SIZE": json.dumps({label: {"min": 0.1, "max": 0.2}}),
|
||||||
"IMAGE_FORMAT_MAP": json.dumps({label: {"min": 0.5, "max": 0.4}}),
|
"IMAGE_FORMAT": json.dumps({label: {"min": 0.5, "max": 0.4}}),
|
||||||
"CONFIDENCE": json.dumps({label: {"min": 0.8}}),
|
"CONFIDENCE": json.dumps({label: {"min": 0.8}}),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user