Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from tensorflow.keras.callbacks import EarlyStopping
|
|
10 |
from sklearn.utils.class_weight import compute_class_weight
|
11 |
from sklearn.model_selection import train_test_split
|
12 |
from tensorflow.image import resize
|
|
|
13 |
|
14 |
from tensorflow.keras.utils import to_categorical
|
15 |
import matplotlib.pyplot as plt
|
@@ -39,106 +40,68 @@ models = {
|
|
39 |
model_name = st.selectbox("Choose a model", list(models.keys()))
|
40 |
model = models[model_name]
|
41 |
|
42 |
-
# Upload
|
43 |
-
|
44 |
-
file =
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
if file is not None:
|
55 |
-
|
56 |
-
#
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
# Get prediction
|
60 |
-
pred = model.predict(img_reshaped)
|
61 |
-
label = np.argmax(pred)
|
62 |
-
|
63 |
-
# label_map = {4: ('nv', ' melanocytic nevi'),
|
64 |
-
# 6: ('mel', 'melanoma'),
|
65 |
-
# 2: ('bkl', 'benign keratosis-like lesions'),
|
66 |
-
# 1: ('bcc' , ' basal cell carcinoma'),
|
67 |
-
# 5: ('vasc', 'pyogenic granulomas and hemorrhage'),
|
68 |
-
# 0: ('akiec', 'Actinic keratoses and intraepithelial carcinomae'),
|
69 |
-
# 3: ('df', 'dermatofibroma')}
|
70 |
-
|
71 |
-
# if label in label_map:
|
72 |
-
# label_name = label_map[label][0]
|
73 |
-
# full_name = label_map[label][1]
|
74 |
|
75 |
# Display image and result
|
76 |
col1, col2 = st.columns(2)
|
77 |
with col1:
|
78 |
st.header("Input Image")
|
79 |
-
|
80 |
with col2:
|
81 |
st.header("Prediction")
|
82 |
-
st.write(
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
# import streamlit as st
|
87 |
-
# import predict_model # our prediction model
|
88 |
-
|
89 |
-
# # Label maps
|
90 |
-
# label_map = {0: ('akiec', 'Actinic keratoses'),
|
91 |
-
# 1: ('bcc', 'basal cell carcinoma'),
|
92 |
-
# # Rest of label map
|
93 |
-
# }
|
94 |
-
|
95 |
-
# # Get prediction
|
96 |
-
# img = st.file_uploader("Upload image")
|
97 |
-
# if img:
|
98 |
-
# pred_id = predict_model.get_prediction(img)
|
99 |
-
|
100 |
-
# # Display prediction
|
101 |
-
# if pred_id in label_map:
|
102 |
-
# label_name = label_map[pred_id][0]
|
103 |
-
# full_name = label_map[pred_id][1]
|
104 |
-
|
105 |
-
# st.success(f"Predicted Label: {label_name} - {full_name}")
|
106 |
-
# else:
|
107 |
-
# st.warning("Unknown label predicted")
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
# data_dir = 'hmnist_28_28_RGB.csv'
|
112 |
-
# data = pd.read_csv(data_dir)
|
113 |
-
# data.head()
|
114 |
-
|
115 |
-
# Label = data["label"]
|
116 |
-
# Data = data.drop(columns=["label"])
|
117 |
-
|
118 |
-
# data["label"].value_counts()
|
119 |
-
|
120 |
-
# classes = {4: ('nv', ' melanocytic nevi'),
|
121 |
-
# 6: ('mel', 'melanoma'),
|
122 |
-
# 2 :('bkl', 'benign keratosis-like lesions'),
|
123 |
-
# 1:('bcc' , ' basal cell carcinoma'),
|
124 |
-
# 5: ('vasc', ' pyogenic granulomas and hemorrhage'),
|
125 |
-
# 0: ('akiec', 'Actinic keratoses and intraepithelial carcinomae'),
|
126 |
-
# 3: ('df', 'dermatofibroma')}
|
127 |
-
|
128 |
-
# from tensorflow.image import resize
|
129 |
-
|
130 |
-
# #preprocess data
|
131 |
-
# Label = data["label"]
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
# Label = to_categorical(Label, num_classes=7) # Assuming 7 classes
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
|
140 |
-
# # Later in Streamlit...
|
141 |
|
142 |
|
143 |
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
from sklearn.utils.class_weight import compute_class_weight
|
11 |
from sklearn.model_selection import train_test_split
|
12 |
from tensorflow.image import resize
|
13 |
+
from google.colab.patches import cv2_imshow
|
14 |
|
15 |
from tensorflow.keras.utils import to_categorical
|
16 |
import matplotlib.pyplot as plt
|
|
|
40 |
model_name = st.selectbox("Choose a model", list(models.keys()))
|
41 |
model = models[model_name]
|
42 |
|
43 |
+
# Upload Image
|
44 |
+
|
45 |
+
file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
46 |
+
# file ='hmnist_28_28_RGB.csv'
|
47 |
+
|
48 |
+
classes = {4: ('nv', ' melanocytic nevi'), 6: ('mel', 'melanoma'),
|
49 |
+
2 :('bkl', 'benign keratosis-like lesions'), 1:('bcc' , ' basal cell carcinoma'),
|
50 |
+
5: ('vasc', ' pyogenic granulomas and hemorrhage'),
|
51 |
+
0: ('akiec', 'Actinic keratoses and intraepithelial carcinomae'),
|
52 |
+
3: ('df', 'dermatofibroma')}
|
53 |
+
|
54 |
+
|
55 |
+
if file is not None:
|
56 |
+
img = cv2.imread(file)
|
57 |
+
# cv2_imshow(img)
|
58 |
+
img1 = cv2.resize(img, (28, 28))
|
59 |
+
result = model.predict(img1.reshape(1, 28, 28, 3))
|
60 |
+
max_prob = max(result[0])
|
61 |
+
class_ind = list(result[0]).index(max_prob)
|
62 |
+
class_name = classes[class_ind]
|
63 |
+
# print(class_name)
|
64 |
+
# count+=1
|
65 |
+
# if count>10:
|
66 |
+
# break
|
67 |
+
|
68 |
+
|
69 |
+
# df = pd.read_csv(file)
|
70 |
+
# # Get first row
|
71 |
+
# img_reshaped = image_resize(df)
|
72 |
|
73 |
+
# # Get prediction
|
74 |
+
# pred = model.predict(img_reshaped)
|
75 |
+
# label = np.argmax(pred)
|
76 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# Display image and result
|
79 |
col1, col2 = st.columns(2)
|
80 |
with col1:
|
81 |
st.header("Input Image")
|
82 |
+
st.image(img)
|
83 |
with col2:
|
84 |
st.header("Prediction")
|
85 |
+
st.write(class_name)
|
86 |
+
st.metric("Category:", class_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
|
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
+
# from google.colab.patches import cv2_imshow
|
93 |
+
# srcdir = '/kaggle/input/skin-cancer-mnist-ham10000/HAM10000_images_part_1'
|
94 |
+
# count=0
|
95 |
+
# for temp in os.listdir(srcdir):
|
96 |
+
# img = cv2.imread(os.path.join(srcdir, temp))
|
97 |
+
# cv2.imwrite(temp, img)
|
98 |
+
# cv2_imshow(img)
|
99 |
+
# img = cv2.resize(img, (28, 28))
|
100 |
+
# result = model.predict(img.reshape(1, 28, 28, 3))
|
101 |
+
# max_prob = max(result[0])
|
102 |
+
# class_ind = list(result[0]).index(max_prob)
|
103 |
+
# class_name = classes[class_ind]
|
104 |
+
# print(class_name)
|
105 |
+
# count+=1
|
106 |
+
# if count>10:
|
107 |
+
# break
|