Skip to content

Commit

Permalink
Add mock bird identification API to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
eleurent committed Jan 13, 2024
1 parent 7e686fe commit e4f957b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
45 changes: 45 additions & 0 deletions backend/nature_go/observation/identification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import PIL.ExifTags
import datetime

from observation.models import Species

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -50,6 +52,49 @@ def plantnet_identify_mock():
return json.load(open('observation/mock_plantnet_response.json'))


def bird_identify_mock(image_path: str):
"""Mock API to identify a bird
Args:
image_path (str): path to an image file
Returns:
dict: mock API response
"""
del image_path
bird_species = Species.objects.filter(type=Species.BIRD_TYPE).all()

def format_species(species):
return {
"score": 0.0,
"species": {
"scientificNameWithoutAuthor": species.scientificNameWithoutAuthor,
"scientificNameAuthorship": species.scientificNameAuthorship,
"genus": {
"scientificNameWithoutAuthor": species.genus,
"scientificNameAuthorship": "",
"scientificName": species.genus
},
"family": {
"scientificNameWithoutAuthor": species.family,
"scientificNameAuthorship": "",
"scientificName": species.family
},
"commonNames": species.commonNames,
"scientificName": species.scientificNameWithoutAuthor + " " + species.scientificNameAuthorship,
},
"images": [],
"gbif": {
"id": species.gbif_id
},
"powo": {
"id": species.powo_id
}
}

return {"results": list(map(format_species, bird_species))}


def read_exif(image_path):
location = {}
datetime_ = ''
Expand Down
5 changes: 3 additions & 2 deletions backend/nature_go/observation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ def create(self, request, *args, **kwargs):
image_path=observation.image.path, organ=observation.organ)
observation.save()
elif observation.type == Species.BIRD_TYPE:
if not (observation.species and observation.species.type == Species.BIRD_TYPE):
raise serializers.ValidationError('For bird observations, a bird species must be provided')
observation.identification_response = identification.bird_identify_mock(
image_path=observation.image.path)
observation.save()

serializer = ObservationSerializer(instance=observation)
return Response(serializer.data)
Expand Down

0 comments on commit e4f957b

Please sign in to comment.