def get_breeds()

in ai-ml/vision-api-serverless-app-tutorial/7_doggo_service/app.py [0:0]


def get_breeds(stream):
    bytesio = stream.read()
    image = vision.Image(content=bytesio)

    objects = client.object_localization(image=image).localized_object_annotations

    im = Image.open(io.BytesIO(bytesio))

    count = 0
    output = []
    for obj in objects:
        data = {}
        count += 1

        # split image
        box = [
            (vertex.x * im.width, vertex.y * im.height)
            for vertex in obj.bounding_poly.normalized_vertices
        ]
        item = im.crop((box[0][0], box[0][1], box[2][0], box[2][1]))

        # save cropped image to file, load into vision API
        item_io = io.BytesIO()
        item.save(item_io, format="png")

        item_bytes = item_io.getvalue()
        image = vision.Image(content=item_bytes)
        data["image"] = base64.b64encode(item_bytes).decode("utf-8")

        response = client.label_detection(image=image)

        labels = [label.description for label in response.label_annotations]
        descs = [label.description for label in response.label_annotations]
        mids = [label.mid for label in response.label_annotations]

        if "Dog" not in descs:
            continue

        # check MIDs
        response = kgapi.entities().search(ids=mids).execute()
        results = [resp["result"] for resp in response["itemListElement"]]
        breed = None
        for item in results:
            if "description" in item.keys() and item["description"] == "Dog breed":
                breed = item["name"]
                continue

        data["breed"] = breed if breed else None
        output.append(data)

    return output