def categorize(barcode: str, deepest_only: bool, blacklist: bool): from robotoff.ml.category.neural.model import LocalModel, \ filter_blacklisted_categories from robotoff import settings from robotoff.utils import get_logger get_logger() model = LocalModel(settings.CATEGORY_CLF_MODEL_PATH) predicted = model.predict_from_barcode(barcode, deepest_only=deepest_only) if predicted: if blacklist: predicted = filter_blacklisted_categories(predicted) for cat, confidence in predicted: print("{}: {}".format(cat, confidence))
def on_get(self, req, resp): barcode = req.get_param("barcode", required=True) deepest_only = req.get_param_as_bool("deepest_only", default=False) blacklist = req.get_param_as_bool("blacklist", default=False) model = ModelRegistry.get() predicted = model.predict_from_barcode(barcode, deepest_only=deepest_only) if predicted: if blacklist: predicted = filter_blacklisted_categories(predicted) predicted = [{ "category": category, "confidence": confidence, } for category, confidence in predicted] predicted = predicted or [] resp.media = {"categories": predicted}