/
skincancer.py
133 lines (101 loc) · 4.69 KB
/
skincancer.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import streamlit as st
import numpy as np
import pandas as pd
#import keras
#from tensorflow.keras.utils.np_utils import to_categorical
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPool2D
from tensorflow.keras import backend as K
import time
import io
from PIL import Image
from pathlib import Path
import urllib.request
import tensorflow
#from tensorflow.python.keras.utils import to_categorical
st.markdown("<h1 style='text-align: center; color: white;'>Artificial Intelligence Augmented Skin Imaging using Computer Vision and Neural Networks</h1>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center; color: white;'> By Vinita Silaparasetty</h3>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center; color: white;'> Msc Data Science at Newcastle University</h3>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: left; color: white;'> Upload Image of Problem Area</h3>", unsafe_allow_html=True)
st.sidebar.header("Patient Details")
patient_id = st.sidebar.text_input( "Patient ID:", '#')
st.sidebar.text("Title of Patient:")
choice = st.sidebar.radio("Select the title of the patient:",
options=['Mr', 'Mrs', 'Ms', 'Miss', 'Master'])
patient_surname = st.sidebar.text_input("Patient Surname:")
patient_first_name = st.sidebar.text_input("Patient First Name:")
patient_middle_name = st.sidebar.text_input("Patient Middle Name:")
patient_age = st.sidebar.date_input("Select Date of Birth:",value=None)
if st.sidebar.button("Submit"):
st.sidebar.success("Submission Successful")
else:
st.sidebar.error("Kindly fill in patient details and click 'Submit'")
def data_gen(x):
img = np.asarray(Image.open(x).resize((28, 28)))
x_test = np.asarray(img.tolist())
x_test_mean = np.mean(x_test)
x_test_std = np.std(x_test)
x_test = (x_test - x_test_mean) / x_test_std
x_validate = x_test.reshape(1, 28, 28, 3)
return x_validate
def data_gen_(img):
img = img.reshape(28, 28)
x_test = np.asarray(img.tolist())
x_test_mean = np.mean(x_test)
x_test_std = np.std(x_test)
x_test = (x_test - x_test_mean) / x_test_std
x_validate = x_test.reshape(1, 28, 28, 3)
return x_validate
def load_models():
with st.spinner("Downloading model... this may take awhile! \n Don't stop it!"):
#url = 'https://github.com/VinitaSilaparasetty/dissertation/releases/download/maiden/skincancer_98.h5'
#filename = url.split('/')[-1]
#urllib.request.urlretrieve(url, filename)
save_dest = Path('models')
save_dest.mkdir(exist_ok=True)
f_checkpoint = Path('models/skincancer_98.h5')
if not f_checkpoint.exists():
download_file_from_google_drive("https://github.com/VinitaSilaparasetty/dissertation/releases/download/maiden/skincancer_98.h5", f_checkpoint)
model = tensorflow.keras.models.load_model("skincancer_98.h5")
return model
def predict(x_test, model):
Y_pred = model.predict(x_test)
ynew = model.predict_proba(x_test)
K.clear_session()
ynew = np.round(ynew, 2)
ynew = ynew*100
y_new = ynew[0].tolist()
Y_pred_classes = np.argmax(Y_pred, axis=1)
K.clear_session()
return y_new, Y_pred_classes
file_path = st.file_uploader('Upload an image', type=['png', 'jpg','jpeg'])
if file_path is not None:
x_test = data_gen(file_path)
image = Image.open(file_path)
img_array = np.array(image)
st.header("Image Preview")
st.success('Upload Successful')
st.image(img_array,use_column_width=True)
model = load_models()
st.header("Diagnosis")
with st.spinner('Analyzing Image...'):
time.sleep(5)
y_new, Y_pred_classes = predict(x_test, model)
if Y_pred_classes==0:
st.success('Patient has Actinic Keratoses')
elif Y_pred_classes==1:
st.success('Patient has Basal Cell Carcinoma')
elif Y_pred_classes==2:
st.success('Patient has Benign Keratosis-like Lesions')
elif Y_pred_classes==3:
st.success('Patient has Dermatofibroma')
elif Y_pred_classes==4:
st.success('Patient has Melanocytic Nevi')
elif Y_pred_classes==5:
st.success('Patient has Melanoma')
elif Y_pred_classes==6:
st.success('Patient has Vascular Lesions')
else:
st.error("Kindly try another image.")
else:
st.info('Kindly Upload an Image')