Example #1
0
 def __init__(self, row: Dict):
     self.ingredient = Ingredient(row[INGREDIENT_NAME_ALIAS],
                                  row[INGREDIENT_ID_ALIAS])
     self.menu_item_name = row[MENU_ITEM_NAME_ALIAS]
     self.scan = Scan(row[MENU_ITEM_ID_ALIAS], row["image_id"], row["time"],
                      row["scan_id"], row["user_id"])
     detections = [Detection(**d) for d in jp.decode(row["detections"])]
     self.detected_ingredients = DetectedIngredient(self.scan.id,
                                                    self.ingredient.id,
                                                    detections)
Example #2
0
    def run_detection(self, scan_request: ScanRequest,
                      scan_id: int) -> List[DetectedIngredient]:
        image = np.array(scan_request.image)

        depth_map = np.array(scan_request.depth_map)
        segmented_circles: List[
            SegmentedCircle] = self.circle_detector.get_segmented_circles(
                image)

        detected_ingredients: Dict[Ingredient,
                                   List[Detection]] = defaultdict(list)
        for segmented_circle in segmented_circles:
            segmented_circle.draw(image)
            # radius_pixels = segmented_circle.circle.r
            # mm_per_pixel = (PLATE_DIAMETER_MM / 2) / radius_pixels
            # max_depth_in_circle = segmented_circle.get_max_value_in_circle(depth_map)
            # print("Max depth in circle was %d" % max_depth_in_circle)
            # depth_map = max_depth_in_circle - depth_map
            for segment in segmented_circle.segments:
                ingredient = self.ingredient_detector.label(
                    segment.get_segment_of(image))
                if ingredient is None:
                    continue
                mass = calculate_mass(depth_map, segment, mm_per_pixel)
                print(mass)
                detection: Detection = Detection(segment.x1, segment.y1, mass)
                detected_ingredients[ingredient].append(detection)

        results = [
            DetectedIngredient(scan_id, k.id, v)
            for (k, v) in detected_ingredients.items()
        ]
        print("Found {} detected ingredients: {}".format(
            len(results), [r.ingredient_id for r in results]))
        return results
Example #3
0
 def get_detected_ingredients(self,
                              ids: List[int] = None
                              ) -> List[DetectedIngredient]:
     if ids is not None:
         rows = self.get(
             clause="scan_id in {}".format(self.list_to_in_param(ids)))
     else:
         rows = self.get()
     return [DetectedIngredient(**r) for r in rows]
class MasterQueryResult:

    def __init__(self, row: Dict):
        self.ingredient = Ingredient(row[INGREDIENT_NAME_ALIAS], row[INGREDIENT_ID_ALIAS])
        self.menu_item_name = row[MENU_ITEM_NAME_ALIAS]
        self.scan = Scan(row[MENU_ITEM_ID_ALIAS], row["image_id"], row["time"], row["scan_id"], row["user_id"])
        detections = [Detection(**d) for d in jp.decode(row["detections"])]
        self.detected_ingredients = DetectedIngredient(self.scan.id, self.ingredient.id, detections)

    def get_as_dict(self):
        as_dict = dict(vars(self))
        as_dict['detected_ingredients'] = self.detected_ingredients.get_as_dict()
        as_dict['ingredient'] = self.ingredient.get_as_dict()
        as_dict['scan'] = self.scan.get_as_dict()
        return as_dict

    def get_as_json(self):
        return json.dumps(self.get_as_dict())
Example #5
0
class MasterQueryResult:
    def __init__(self, row: Dict):
        self.ingredient = Ingredient(row[INGREDIENT_NAME_ALIAS],
                                     row[INGREDIENT_ID_ALIAS])
        self.menu_item_name = row[MENU_ITEM_NAME_ALIAS]
        self.scan = Scan(row[MENU_ITEM_ID_ALIAS], row["image_id"], row["time"],
                         row["scan_id"], row["user_id"])
        detections = [Detection(**d) for d in jp.decode(row["detections"])]
        self.detected_ingredients = DetectedIngredient(self.scan.id,
                                                       self.ingredient.id,
                                                       detections)

    def get_as_dict(self):
        as_dict = dict(vars(self))
        as_dict[
            'detected_ingredients'] = self.detected_ingredients.get_as_dict()
        as_dict['ingredient'] = self.ingredient.get_as_dict()
        as_dict['scan'] = self.scan.get_as_dict()
        return as_dict

    def get_as_json(self):
        return json.dumps(self.get_as_dict())
Example #6
0
    def run_detection(self, scan_request: ScanRequest, scan_id: int) -> List[DetectedIngredient]:
        image = np.array(scan_request.image)

        depth_map = np.array(scan_request.depth_map)
        segmented_circles: List[SegmentedCircle] = self.circle_detector.get_segmented_circles(image)
        if len(segmented_circles) > 1:
            print("WARN: Found more than one circle")

        detected_ingredients: Dict[Ingredient, List[Detection]] = defaultdict(list)
        for segmented_circle in segmented_circles:
            # segmented_circle.draw(image)

            radius_pixels = segmented_circle.circle.r
            mm_per_pixel = (PLATE_DIAMETER_MM / 2) / radius_pixels
            max_depth_in_circle = segmented_circle.get_max_value_in_circle(depth_map)
            if (max_depth_in_circle == -1):
                print("HAd no max depth, skipping")
                continue
            print("Max depth in circle was %{0.2f}",  max_depth_in_circle)
            print("mm per pixel was %d" % mm_per_pixel)

            # Mutating datastructures here
            # transformed_image = self.ingredient_detector.transform_image_for_classification(image)
            depth_map = max_depth_in_circle - depth_map

            for segment in segmented_circle.segments:
                segment_image = segment.get_segment_of(image)
                transformed_seg = self.ingredient_detector.transform_image_for_classification(segment_image)
                ingredient = self.ingredient_detector.label(transformed_seg)
                if ingredient is None or ingredient.id in IGNORE_IDS:
                    continue
                mass = calculate_mass(depth_map, segment, mm_per_pixel)
                # print(mass)
                detection: Detection = Detection(segment.x1, segment.y1, mass)
                detected_ingredients[ingredient].append(detection)

        results = [DetectedIngredient(scan_id, k.id, v) for (k, v) in detected_ingredients.items()]
        print("Found {} detected ingredients: {}".format(len(results), [INGREDIENTS[r.ingredient_id - 1] for r in results]))
        return results

class DetectedIngredientsDao(BaseDao):
    def __init__(self):
        super().__init__(TABLE, COLUMNS)

    def get_detected_ingredients(self, ids: List[int] = None) -> List[DetectedIngredient]:
        if ids is not None:
            rows = self.get(clause="scan_id in {}".format(self.list_to_in_param(ids)))
        else:
            rows = self.get()
        return [DetectedIngredient(**r) for r in rows]

    def insert_detected_ingredients(self, detected_ingredients: List[DetectedIngredient]) -> int:
        if len(detected_ingredients) == 0:
            print("No ingredients detected, skipping")
            return -1
        return self.insert([d.get_as_json() for d in detected_ingredients])


if __name__ == "__main__":
    did = DetectedIngredientsDao()
    d1 = Detection(0, 0, 100)
    d2 = Detection(40, 0, 50)
    detected_ingredient = DetectedIngredient(1, 1, [d1, d2])
    print(detected_ingredient.get_as_json())

    did.insert_detected_ingredients([detected_ingredient])
    did.insert_detected_ingredients([detected_ingredient])
    got = did.get_detected_ingredients()
    print(got)
Example #8
0
        super().__init__(TABLE, COLUMNS)

    def get_detected_ingredients(self,
                                 ids: List[int] = None
                                 ) -> List[DetectedIngredient]:
        if ids is not None:
            rows = self.get(
                clause="scan_id in {}".format(self.list_to_in_param(ids)))
        else:
            rows = self.get()
        return [DetectedIngredient(**r) for r in rows]

    def insert_detected_ingredients(
            self, detected_ingredients: List[DetectedIngredient]) -> int:
        if len(detected_ingredients) == 0:
            print("No ingredients detected, skipping")
            return -1
        return self.insert([d.get_as_json() for d in detected_ingredients])


if __name__ == "__main__":
    did = DetectedIngredientsDao()
    d1 = Detection(0, 0, 100)
    d2 = Detection(40, 0, 50)
    detected_ingredient = DetectedIngredient(1, 1, [d1, d2])
    print(detected_ingredient.get_as_json())

    did.insert_detected_ingredients([detected_ingredient])
    did.insert_detected_ingredients([detected_ingredient])
    got = did.get_detected_ingredients()
    print(got)
 def __init__(self, row: Dict):
     self.ingredient = Ingredient(row[INGREDIENT_NAME_ALIAS], row[INGREDIENT_ID_ALIAS])
     self.menu_item_name = row[MENU_ITEM_NAME_ALIAS]
     self.scan = Scan(row[MENU_ITEM_ID_ALIAS], row["image_id"], row["time"], row["scan_id"], row["user_id"])
     detections = [Detection(**d) for d in jp.decode(row["detections"])]
     self.detected_ingredients = DetectedIngredient(self.scan.id, self.ingredient.id, detections)