File size: 1,980 Bytes
fef3f6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7806a41
 
 
fef3f6f
 
 
7806a41
fef3f6f
 
 
 
 
 
 
 
 
4b8ce99
fef3f6f
af3816c
fef3f6f
 
 
 
 
 
 
 
 
 
7806a41
fef3f6f
 
af3816c
f436c44
7806a41
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
58
59
import sys
import os

import pandas as pd
import numpy as np
import seaborn as sns
import cv2

import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.imagenet_utils import preprocess_input

from sklearn.metrics import f1_score

import gradio as gr


os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
sns.set_style('darkgrid')
if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_colwidth', None)
print('Modules loaded')


def F1_score(y_true, y_pred):  
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    recall = true_positives / (possible_positives + K.epsilon())
    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
    return f1_val


model = load_model(r"Model/Model.h5", custom_objects={"F1_score": f1_score})

def med_image_recog(img_path):
    img = cv2.imread(img_path)
    img = cv2.resize(img, (250, 224))
    x = np.expand_dims(img, axis=0)
    x = preprocess_input(x)

    prediction = model.predict(x)

    classes = ['Tumor', 'Cyst', 'Normal', 'Stone']
    predicted_class = classes[np.argmax(prediction[0])]
    confidence = str(100 * (np.max(prediction[0])))
    return str(predicted_class + " detected with a confidence of " + confidence + "%")


app = gr.Interface(fn=med_image_recog, inputs=gr.Image(image_mode="L", type="filepath", label="Input Image"),
                   outputs=gr.Label(label="Model Prediction"), allow_flagging="never", examples=[r"demo/Cyst.jpg", r"demo/Normal.jpg", r"demo/Stone.jpg", r"demo/Tumor.jpg"], title="MedImageRecog - Sistema de Reconhecimento de Imagens Médicas")
app.launch()