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)
def test_that_text_get_described(self): with mock.patch.object( Cognitive, "describe", return_value=DescriptionResult(description="A title", tags=["blue", "green", "white"]), ) as mock_method: image = ImageFactory(title=get_test_image_file().name, file=get_test_image_file()) image.refresh_from_db() mock_method.assert_called_with(image) self.assertEqual(image.title, "A title") self.assertEqual(image.tags.all().count(), 3) self.assertTrue(image.tags.filter(slug__in=["blue"]).exists())
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)
def test_that_text_gets_translated(self): with mock.patch.object( Cognitive, "describe", return_value=DescriptionResult(description="A title", tags=["blue", "green", "white"]), ): with mock.patch.object( GoogleTranslate, "translate", return_value=["En titel", "blå", "grön", "vit"], ): image = ImageFactory(title=get_test_image_file().name, file=get_test_image_file()) image.refresh_from_db() self.assertEqual(image.title, "En titel") self.assertEqual(image.tags.all().count(), 3) self.assertTrue(image.tags.filter(slug__in=["vit"]).exists())
def translate_description_result(result): provider = get_current_provider()() lang_and_country_code = get_language() lang_code = lang_and_country_code.split("-")[0] strings = [] if result.description: strings = [result.description] if result.tags: strings = [*strings, *result.tags] translated_strings = provider.translate(strings, target_language=lang_code) translated_description = (translated_strings[0] if result.description else result.description) translated_tags = (translated_strings[1:] if result.description else translated_strings) return DescriptionResult(description=translated_description, tags=translated_tags)
def test_that_description_is_set_when_tags_are_none(self): result = DescriptionResult("En bro", None) translated_result = translate_description_result(result) self.assertEqual(translated_result.description, "A bridge") self.assertEqual(translated_result.tags, [])
def test_that_description_is_excluded_when_empty(self): result = DescriptionResult("", ["cat", "house", "shoe"]) translated_result = translate_description_result(result) self.assertEqual(translated_result.description, "") self.assertEqual(translated_result.tags, ["katt", "hus", "sko"])
def test_translation_mapping(self): result = DescriptionResult("My title", ["cat", "house", "shoe"]) translated_result = translate_description_result(result) self.assertEqual(translated_result.description, "Min titel") self.assertEqual(translated_result.tags, ["katt", "hus", "sko"])