Esempio n. 1
0
    def describe(self, image):
        if not image.is_stored_locally():
            image_data = get_image_data(image.file.url)
        else:
            image_data = get_local_image_data(image.file)

        description = None
        tags = []

        min_confidence = float(
            app_settings.ALT_GENERATOR_MIN_CONFIDENCE) / 100.0

        if image_data:
            image_data_base64 = base64.b64encode(image_data)

            max_results = app_settings.ALT_GENERATOR_MAX_TAGS

            request_body = {
                'requests': [{
                    'image': {
                        'content': image_data_base64.decode('UTF-8'),
                    },
                    'features': [{
                        'type': 'LABEL_DETECTION',
                    }]
                }]
            }

            if max_results != -1:
                request_body['requests'][0]['features'][0][
                    'maxResults'] = max_results  # NOQA

            service_request = self.service.images().annotate(body=request_body)

            response = service_request.execute()
            labels = response['responses'][0]['labelAnnotations']
            tags = [
                label['description'] for label in labels
                if label['score'] >= min_confidence
            ]

        return DescriptionResult(
            description=description,
            tags=tags,
        )
    def describe(self, image):
        if not image.is_stored_locally():
            image_data = get_image_data(image.file.url)
        else:
            image_data = get_local_image_data(image.file)

        description = None
        tags = []

        if image_data:
            response = self.client.detect_labels(
                Image={"Bytes": image_data},
                MinConfidence=app_settings.ALT_GENERATOR_MIN_CONFIDENCE,
            )
            if response["Labels"]:
                tags = [label["Name"] for label in response["Labels"]]

        return DescriptionResult(description=description, tags=tags)
    def describe(self, image):
        if not image.is_stored_locally():
            rendition = get_original_rendition(image)
            image_data = get_image_data(rendition.url)
        else:
            image_data = get_local_image_data(image.file)

        description = None
        tags = []

        min_confidence = float(
            app_settings.ALT_GENERATOR_MIN_CONFIDENCE) / 100.0

        if image_data:
            image_data_base64 = base64.b64encode(image_data)

            max_results = app_settings.ALT_GENERATOR_MAX_TAGS

            request_body = {
                "requests": [{
                    "image": {
                        "content": image_data_base64.decode("UTF-8")
                    },
                    "features": [{
                        "type": "LABEL_DETECTION"
                    }],
                }]
            }

            if max_results != -1:
                request_body["requests"][0]["features"][0][
                    "maxResults"] = max_results  # NOQA

            service_request = self.service.images().annotate(body=request_body)

            response = service_request.execute()
            labels = response["responses"][0]["labelAnnotations"]
            tags = [
                label["description"] for label in labels
                if label["score"] >= min_confidence
            ]

        return DescriptionResult(description=description, tags=tags)
Esempio n. 4
0
    def describe(self, image):
        if app_settings.ALT_GENERATOR_PREFER_UPLOAD:
            if not image.is_stored_locally():
                image_data = get_image_data(image.file.url)
                data = describe_by_data(image_data)
            else:
                image_data = get_local_image_data(image.file)
                data = describe_by_data(image_data)
        else:
            data = describe_by_url(image.file.url)

        description = None
        tags = []

        min_confidence = float(
            app_settings.ALT_GENERATOR_MIN_CONFIDENCE) / 100.0

        if not data:
            return DescriptionResult(
                description=description,
                tags=tags,
            )

        if 'description' in data and len(data['description']['captions']):
            captions = data['description']['captions']
            captions = [
                caption['text'] for caption in captions
                if caption['confidence'] >= min_confidence
            ]

            if len(captions):
                description = captions[0]

        try:
            tags = data['description']['tags']
        except:
            pass

        return DescriptionResult(
            description=description,
            tags=tags,
        )
    def describe(self, image):
        if app_settings.ALT_GENERATOR_PREFER_UPLOAD:
            if not image.is_stored_locally():
                rendition = get_original_rendition(image)
                image_data = get_image_data(rendition.url)
                data = describe_by_data(image_data)
            else:
                image_data = get_local_image_data(image.file)
                data = describe_by_data(image_data)
        else:
            rendition = get_original_rendition(image)
            data = describe_by_url(rendition.url)

        description = None
        tags = []

        min_confidence = float(
            app_settings.ALT_GENERATOR_MIN_CONFIDENCE) / 100.0

        if not data:
            return DescriptionResult(description=description, tags=tags)

        if "description" in data and len(data["description"]["captions"]):
            captions = data["description"]["captions"]
            captions = [
                caption["text"] for caption in captions
                if caption["confidence"] >= min_confidence
            ]

            if len(captions):
                description = captions[0]

        try:
            tags = data["description"]["tags"]
        except:  # NOQA
            pass

        return DescriptionResult(description=description, tags=tags)