Skip to content

Commit

Permalink
return photo embedding in some responses
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Dec 12, 2024
1 parent f816373 commit 3923de7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/inat_inferrer.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,12 @@ async def embedding_for_photo(self, url, session):
cache_path = await self.download_photo_async(url, session)
if cache_path is None:
return
image = InatInferrer.prepare_image_for_inference(cache_path)
return self.signature_for_image(cache_path)
except urllib.error.HTTPError:
return

def signature_for_image(self, image_path):
image = InatInferrer.prepare_image_for_inference(image_path)
return self.vision_inferrer.signature_for_image(image).tolist()

async def download_photo_async(self, url, session):
Expand Down
8 changes: 6 additions & 2 deletions lib/inat_vision_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,19 @@ def score_image(self, form, file_path, lat, lng, iconic_taxon_id, geomodel):
aggregated_scores, self.inferrer
)
return InatVisionAPIResponses.aggregated_object_response(
leaf_scores, aggregated_scores, self.inferrer
leaf_scores, aggregated_scores, self.inferrer,
embedding=self.inferrer.signature_for_image(file_path)
)

# legacy dict response
if geomodel != "true":
return InatVisionAPIResponses.legacy_dictionary_response(leaf_scores, self.inferrer)

if form.format.data == "object":
return InatVisionAPIResponses.object_response(leaf_scores, self.inferrer)
return InatVisionAPIResponses.object_response(
leaf_scores, self.inferrer,
embedding=self.inferrer.signature_for_image(file_path)
)

return InatVisionAPIResponses.array_response(leaf_scores, self.inferrer)

Expand Down
10 changes: 6 additions & 4 deletions lib/inat_vision_api_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def array_response(leaf_scores, inferrer):
return InatVisionAPIResponses.array_response_columns(leaf_scores).to_dict(orient="records")

@staticmethod
def object_response(leaf_scores, inferrer):
def object_response(leaf_scores, inferrer, embedding):
leaf_scores = InatVisionAPIResponses.limit_leaf_scores_for_response(leaf_scores)
leaf_scores = InatVisionAPIResponses.update_leaf_scores_scaling(leaf_scores)
results = InatVisionAPIResponses.array_response_columns(
Expand All @@ -41,7 +41,8 @@ def object_response(leaf_scores, inferrer):

return {
"common_ancestor": common_ancestor,
"results": results
"results": results,
"embedding": embedding
}

@staticmethod
Expand Down Expand Up @@ -73,7 +74,7 @@ def aggregated_tree_response(aggregated_scores, inferrer):
return "<pre>" + "<br/>".join(printable_tree) + "</pre>"

@staticmethod
def aggregated_object_response(leaf_scores, aggregated_scores, inferrer):
def aggregated_object_response(leaf_scores, aggregated_scores, inferrer, embedding):
top_leaf_combined_score = aggregated_scores.query(
"leaf_class_id.notnull()"
).sort_values(
Expand Down Expand Up @@ -118,7 +119,8 @@ def aggregated_object_response(leaf_scores, aggregated_scores, inferrer):

return {
"common_ancestor": common_ancestor,
"results": final_results.to_dict(orient="records")
"results": final_results.to_dict(orient="records"),
"embedding": embedding
}

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ aiohttp==3.11.2;python_version>="3.11"
aiohttp==3.10.11;python_version=="3.8"
flake8==7.0.0
flake8-quotes==3.4.0
Flask==3.0.2
Flask[async]==3.0.2
Flask-WTF==1.2.1
h3==3.7.7
h3pandas==0.2.6
Expand Down

0 comments on commit 3923de7

Please sign in to comment.