Skip to content

Commit

Permalink
Merge pull request #111 from DevMed22/Back-end
Browse files Browse the repository at this point in the history
MCI Model API
  • Loading branch information
tabana1 authored Jun 17, 2023
2 parents 359be76 + 437af27 commit d14f137
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 1 deletion.
Empty file added backend/prediction/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions backend/prediction/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions backend/prediction/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PredictionConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'prediction'
Empty file.
3 changes: 3 additions & 0 deletions backend/prediction/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
5 changes: 5 additions & 0 deletions backend/prediction/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# serializers.py
from rest_framework import serializers

class ImageUploadSerializer(serializers.Serializer):
image = serializers.ImageField()
3 changes: 3 additions & 0 deletions backend/prediction/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions backend/prediction/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# urls.py
from django.urls import path
from .views import PredictionView

urlpatterns = [
path('api/predict/', PredictionView.as_view(), name='predict'),
]
37 changes: 37 additions & 0 deletions backend/prediction/views.py
Original file line number Diff line number Diff line change
@@ -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})
2 changes: 1 addition & 1 deletion backend/root/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down

0 comments on commit d14f137

Please sign in to comment.