File size: 1,643 Bytes
d6cb779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import os
import keras
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from keras.models import Sequential
from PIL import Image
from keras.layers import Conv2D, Flatten, Dense, Dropout, BatchNormalization, MaxPooling2D
from sklearn.preprocessing import OneHotEncoder
import pickle
import gradio as gr

def load_model():
    save_path = 'model.pkl'
    with open(save_path, 'rb') as file:
        model = pickle.load(file)
    return model

def predict_dementia(images, model):
    predictions = []
    for image in images:
        img = Image.fromarray(image.astype('uint8'))
        img = img.resize((128, 128))
        img = np.array(img)
        img = img.reshape(1, 128, 128, 3)
        
        prediction = model.predict(img)
        prediction_class = np.argmax(prediction)
        predictions.append(names(prediction_class))
    return predictions

def names(number):
    if number == 0:
        return 'Non Demented'
    elif number == 1:
        return 'Mild Dementia'
    elif number == 2:
        return 'Moderate Dementia'
    elif number == 3:
        return 'Very Mild Dementia'
    else:
        return 'Error in Prediction'

def main(images):
    model = load_model()
    predictions = predict_dementia(images, model)
    return predictions

iface = gr.Interface(fn=main,
                     inputs="image",
                     outputs="text",
                     title="Dementia Classification",
                     description="Classify dementia based on brain images",
                     examples=[["Non(1).jpg"],["Moderate.jpg"],["Mild.jpg"]])

iface.launch(debug =True)