-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from DevMed22/Back-end
MCI Model API
- Loading branch information
Showing
10 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters