diff --git a/backend/prediction/__init__.py b/backend/prediction/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/backend/prediction/admin.py b/backend/prediction/admin.py new file mode 100644 index 000000000..ea5d68b7c --- /dev/null +++ b/backend/prediction/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/backend/prediction/apps.py b/backend/prediction/apps.py new file mode 100644 index 000000000..407cb5331 --- /dev/null +++ b/backend/prediction/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class PredictionConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'prediction' diff --git a/backend/prediction/migrations/__init__.py b/backend/prediction/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/backend/prediction/models.py b/backend/prediction/models.py new file mode 100644 index 000000000..fd18c6eac --- /dev/null +++ b/backend/prediction/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/backend/prediction/serializers.py b/backend/prediction/serializers.py new file mode 100644 index 000000000..da3949e12 --- /dev/null +++ b/backend/prediction/serializers.py @@ -0,0 +1,5 @@ +# serializers.py +from rest_framework import serializers + +class ImageUploadSerializer(serializers.Serializer): + image = serializers.ImageField() \ No newline at end of file diff --git a/backend/prediction/tests.py b/backend/prediction/tests.py new file mode 100644 index 000000000..de8bdc00e --- /dev/null +++ b/backend/prediction/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/backend/prediction/urls.py b/backend/prediction/urls.py new file mode 100644 index 000000000..57c7d5e36 --- /dev/null +++ b/backend/prediction/urls.py @@ -0,0 +1,7 @@ +# urls.py +from django.urls import path +from .views import PredictionView + +urlpatterns = [ + path('api/predict/', PredictionView.as_view(), name='predict'), +] \ No newline at end of file diff --git a/backend/prediction/views.py b/backend/prediction/views.py new file mode 100644 index 000000000..7aeee0d11 --- /dev/null +++ b/backend/prediction/views.py @@ -0,0 +1,37 @@ +# views.py +from rest_framework.views import APIView +from rest_framework.response import Response +from PIL import Image +import io +import os +from django.conf import settings +from .serializers import ImageUploadSerializer +import tensorflow as tf +model_path = os.path.join(settings.BASE_DIR, 'MCI Model', 'mci-resnet.h5') + +class PredictionView(APIView): + def post(self, request): + # Load the serialized DL model from disk + model = tf.keras.models.load_model(model_path) + + # Process the API request + serializer = ImageUploadSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + image = serializer.validated_data['image'] + + # Pre-process the image + img = Image.open(image) + # Perform any necessary pre-processing on the image (e.g., resizing, normalization) + + # Convert the image to the input format expected by the DL model + img = img.resize((224, 224)) # Example: Resize the image to 224x224 pixels + img = img.convert('RGB') # Example: Convert to RGB color space + img = tf.keras.preprocessing.image.img_to_array(img) + img = tf.expand_dims(img, axis=0) + + # Pass the pre-processed image to the DL model for prediction + result = model.predict(img) + # Format the result as a response + + # Return the API response + return Response({'result': result}) diff --git a/backend/root/urls.py b/backend/root/urls.py index 286480bfb..2dc804d53 100644 --- a/backend/root/urls.py +++ b/backend/root/urls.py @@ -7,7 +7,7 @@ urlpatterns = [ path('admin/', admin.site.urls), path('api/v1/', include('api.urls')), - + path('prediction/api/v1/', include('prediction.urls')), path('', include('pages.urls')), path('accounts/', include('django.contrib.auth.urls')), path('accounts/', include('accounts.urls')),