0

I am building an app which lets the user to upload an image to the server and the server will return the most similar image in the database. I implemented a basic algorithm to do this, but I can not figure out how to actually let the user upload the image to the server. I am using the DjangoRestFramework.

I now have implemented a feature to upload an image to the database by having ViewSet with CreateModelMixin implemented. However I want to let the user upload an image, run my algorithm and then return and ID of most similar image. What function/viewset should I look into? I am beginner in REST

1 Answers1

0

I would approach this problem by allowing the serializer to do the heavy lifting in this case and keeping the view slim.

class ImageUploadSerializer(serializers.Serializer):
     """
     This serializer will accept the uploaded image, 
     run the custom algorithm and return the queryset
     of the similar images.
     """
     [#read more about image field in docs][1]
     image = serializers.ImageField(write_only=True)
     similar_image = serializers.ImageField(read_only=True)

     def get_similar_image(self, obj):
         """
         Sends the image to the custom alogrithm
         and returns the most similar image
         """
         
         # your function to return the similar image  
         return most_similiar_image(self.validated_data.get("image"))

    class Meta:
        fields = ("image", "similar_image")
VJ Magar
  • 974
  • 7
  • 14