From e1df4d1b97663ab0fb4f0176028925dcd831a22e Mon Sep 17 00:00:00 2001 From: Patrick Leary Date: Tue, 24 Dec 2024 11:17:04 -0500 Subject: [PATCH] representative photos require parameter, included as separate attribute --- .../v1/computervision_controller.js | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/controllers/v1/computervision_controller.js b/lib/controllers/v1/computervision_controller.js index 4a61cb1e..2ab9fa1a 100644 --- a/lib/controllers/v1/computervision_controller.js +++ b/lib/controllers/v1/computervision_controller.js @@ -268,8 +268,8 @@ const ComputervisionController = class ComputervisionController { return; } const taxonIDsToLookup = _.map( _.filter( - results, result => result.rank_level < 30 - ), "taxon_id" ); + results, result => result?.taxon?.rank_level < 30 + ), result => result?.taxon?.id ); if ( _.isEmpty( taxonIDsToLookup ) ) { return; } @@ -285,10 +285,10 @@ const ComputervisionController = class ComputervisionController { knn: { field: "embedding", query_vector: embedding, - k: 10, - num_candidates: 500 + k: 500, + num_candidates: 5000 }, - size: 10000, + size: 500, _source: [ "id", "taxon_id", @@ -300,7 +300,7 @@ const ComputervisionController = class ComputervisionController { const embeddingsHits = _.map( embeddingsResponse.hits.hits, "_source" ); // TODO: remove this once the ancestor_ids data in the index has been fixed _.each( embeddingsHits, hit => { - hit.ancestor_ids = _.remove( hit.ancestor_ids, ancestorID => ancestorID === hit.id ); + hit.ancestor_ids = _.filter( hit.ancestor_ids, ancestorID => ancestorID !== hit.id ); hit.ancestor_ids.push( hit.taxon_id ); } ); const representativeTaxonPhotos = []; @@ -321,9 +321,7 @@ const ComputervisionController = class ComputervisionController { await ObservationPreload.assignObservationPhotoPhotos( representativeTaxonPhotos ); _.each( representativeTaxonPhotos, taxonPhoto => { if ( taxonPhoto?.photo?.url ) { - taxonPhoto.taxon.default_photo.url = taxonPhoto.photo.url.replace( "medium", "square" ); - taxonPhoto.taxon.default_photo.medium_url = taxonPhoto.photo.url; - taxonPhoto.taxon.default_photo.square_url = taxonPhoto.photo.url.replace( "medium", "square" ); + taxonPhoto.taxon.representative_photo = taxonPhoto.photo; } } ); } @@ -386,10 +384,14 @@ const ComputervisionController = class ComputervisionController { } ); } - await ComputervisionController.addRepresentativePhotos( - withTaxa, - visionApiResponse.embedding - ); + if ( req.userSession?.isAdmin && ( + req.body.include_representative_photos || req.query.include_representative_photos + ) ) { + await ComputervisionController.addRepresentativePhotos( + withTaxa, + visionApiResponse.embedding + ); + } // remove attributes of common_ancestor that should not be in the response if ( response.common_ancestor ) {