From 3923de72f27965df79ca38fadebbe06bf4a97a25 Mon Sep 17 00:00:00 2001 From: Patrick Leary Date: Thu, 12 Dec 2024 11:29:33 -0500 Subject: [PATCH] return photo embedding in some responses --- lib/inat_inferrer.py | 5 ++++- lib/inat_vision_api.py | 8 ++++++-- lib/inat_vision_api_responses.py | 10 ++++++---- requirements.txt | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/inat_inferrer.py b/lib/inat_inferrer.py index 28f0cd6..ec1a690 100644 --- a/lib/inat_inferrer.py +++ b/lib/inat_inferrer.py @@ -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): diff --git a/lib/inat_vision_api.py b/lib/inat_vision_api.py index a13b607..8150149 100644 --- a/lib/inat_vision_api.py +++ b/lib/inat_vision_api.py @@ -154,7 +154,8 @@ 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 @@ -162,7 +163,10 @@ def score_image(self, form, file_path, lat, lng, iconic_taxon_id, geomodel): 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) diff --git a/lib/inat_vision_api_responses.py b/lib/inat_vision_api_responses.py index 5cf292c..e80c9ea 100644 --- a/lib/inat_vision_api_responses.py +++ b/lib/inat_vision_api_responses.py @@ -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( @@ -41,7 +41,8 @@ def object_response(leaf_scores, inferrer): return { "common_ancestor": common_ancestor, - "results": results + "results": results, + "embedding": embedding } @staticmethod @@ -73,7 +74,7 @@ def aggregated_tree_response(aggregated_scores, inferrer): return "
" + "
".join(printable_tree) + "
" @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( @@ -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 diff --git a/requirements.txt b/requirements.txt index f0894ce..a66056b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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