Skip to content

Commit

Permalink
[fix] UnidentifiedImageError in image face detection google
Browse files Browse the repository at this point in the history
  • Loading branch information
floflokie committed Dec 11, 2023
1 parent c7d9a24 commit 466f0e1
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions edenai_apis/apis/google/google_image_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Sequence

import numpy as np
from PIL import Image as Img
from PIL import Image as Img, UnidentifiedImageError
from google.cloud import vision
from google.cloud.vision_v1.types.image_annotator import AnnotateImageResponse
from google.protobuf.json_format import MessageToDict
Expand Down Expand Up @@ -53,15 +53,18 @@ class GoogleImageApi(ImageInterface):
def _convert_likelihood(self, value: int) -> float:
values = [0, 0.2, 0.4, 0.6, 0.8, 1]
return values[value]

def image__explicit_content(
self, file: str, file_url: str = ""
) -> ResponseType[ExplicitContentDataClass]:
with open(file, "rb") as file_:
content = file_.read()
image = vision.Image(content=content)

payload = { "image": image }
response = handle_google_call(self.clients["image"].safe_search_detection, **payload)
payload = {"image": image}
response = handle_google_call(
self.clients["image"].safe_search_detection, **payload
)

# Convert response to dict
data = AnnotateImageResponse.to_dict(response)
Expand All @@ -73,23 +76,29 @@ def image__explicit_content(

items = []
for safe_search_annotation, likelihood in original_response.items():
classificator = CategoryType.choose_category_subcategory(safe_search_annotation.capitalize())
classificator = CategoryType.choose_category_subcategory(
safe_search_annotation.capitalize()
)
items.append(
ExplicitItem(
label=safe_search_annotation.capitalize(),
category=classificator["category"],
subcategory=classificator["subcategory"],
likelihood_score=self._convert_likelihood(likelihood),
likelihood=likelihood
likelihood=likelihood,
)
)

nsfw_likelihood = ExplicitContentDataClass.calculate_nsfw_likelihood(items)
nsfw_likelihood_score = ExplicitContentDataClass.calculate_nsfw_likelihood_score(items)
nsfw_likelihood_score = (
ExplicitContentDataClass.calculate_nsfw_likelihood_score(items)
)
return ResponseType(
original_response=original_response,
standardized_response=ExplicitContentDataClass(
items=items, nsfw_likelihood=nsfw_likelihood, nsfw_likelihood_score=nsfw_likelihood_score
items=items,
nsfw_likelihood=nsfw_likelihood,
nsfw_likelihood_score=nsfw_likelihood_score,
),
)

Expand All @@ -99,8 +108,10 @@ def image__object_detection(
file_ = open(file, "rb")
image = vision.Image(content=file_.read())

payload = { "image": image }
response = handle_google_call(self.clients["image"].object_localization, **payload)
payload = {"image": image}
response = handle_google_call(
self.clients["image"].object_localization, **payload
)
response = MessageToDict(response._pb)

file_.close()
Expand Down Expand Up @@ -139,13 +150,13 @@ def image__face_detection(
) -> ResponseType[FaceDetectionDataClass]:
with open(file, "rb") as file_:
file_content = file_.read()
img_size = Img.open(file).size
try:
img_size = Img.open(file).size
except UnidentifiedImageError:
raise ProviderException(message="Can not identify image file", code=400)
image = vision.Image(content=file_content)

payload = {
"image": image,
"max_results": 100
}

payload = {"image": image, "max_results": 100}
response = handle_google_call(self.clients["image"].face_detection, **payload)
original_result = MessageToDict(response._pb)

Expand Down Expand Up @@ -281,8 +292,10 @@ def image__landmark_detection(
with open(file, "rb") as file_:
content = file_.read()
image = vision.Image(content=content)
payload = { "image": image }
response = handle_google_call(self.clients["image"].landmark_detection, **payload)
payload = {"image": image}
response = handle_google_call(
self.clients["image"].landmark_detection, **payload
)
dict_response = vision.AnnotateImageResponse.to_dict(response)
landmarks = dict_response.get("landmark_annotations", [])

Expand Down Expand Up @@ -329,9 +342,9 @@ def image__logo_detection(
content = file_.read()
image = vision.Image(content=content)

payload = { "image": image }
payload = {"image": image}
response = handle_google_call(self.clients["image"].logo_detection, **payload)

response = MessageToDict(response._pb)

float_or_none = lambda val: float(val) if val else None
Expand Down

0 comments on commit 466f0e1

Please sign in to comment.