Skip to content

Commit

Permalink
alternate representative taxon photo lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Jan 10, 2025
1 parent f217b11 commit a7093d1
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions lib/controllers/v1/computervision_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,47 +287,44 @@ const ComputervisionController = class ComputervisionController {
return;
}

const representativeTaxonPhotos = [];
const promiseProducer = ( ) => {
if ( _.isEmpty( resultsToLookup ) ) {
return null;
}
const resultToLookup = resultsToLookup.shift( );
return ( async ( ) => {
const embeddingsResponse = await esClient.search( "taxon_photos", {
body: {
knn: {
field: "embedding",
query_vector: embedding,
k: 10,
num_candidates: 10,
filter: {
term: {
ancestor_ids: resultToLookup.taxon.id
}
}
},
size: 1,
_source: [
"id",
"taxon_id",
"photo_id",
"ancestor_ids"
]
const embeddingsResponse = await esClient.search( "taxon_photos", {
body: {
knn: {
field: "embedding",
query_vector: embedding,
k: 100,
num_candidates: 100,
filter: {
terms: {
ancestor_ids: _.map( resultsToLookup, result => result.taxon.id )
}
}
} );
const firstHitSource = embeddingsResponse.hits.hits[0]?._source;
if ( !firstHitSource ) {
},
size: 100,
_source: [
"id",
"taxon_id",
"photo_id",
"ancestor_ids"
]
}
} );
const embeddingsHits = _.map( embeddingsResponse.hits.hits, "_source" );
const representativeTaxonPhotos = [];
_.each( results, result => {
if ( result && result.taxon && result.taxon.default_photo ) {
const firstMatch = _.find( embeddingsHits, h => (
h?.photo_id && _.includes( h.ancestor_ids, result.taxon.id )
) );
if ( !firstMatch ) {
return;
}
representativeTaxonPhotos.push( {
taxon: resultToLookup.taxon,
photo_id: firstHitSource.photo_id
taxon: result.taxon,
photo_id: firstMatch.photo_id
} );
} )( );
};
const pool = new PromisePool( promiseProducer, 2 );
await pool.start( );
}
} );

await ObservationPreload.assignObservationPhotoPhotos( representativeTaxonPhotos );
if ( req.inat.isInRepresentativePhotosTestGroup ) {
Expand Down

0 comments on commit a7093d1

Please sign in to comment.