Upload 4 files
Browse files- InceptionResNetV2Skripsi.tflite +3 -0
- another.py +125 -0
- front_model_resnet.h5 +3 -0
- requirements.txt +7 -0
InceptionResNetV2Skripsi.tflite
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2ce9b335d23bb9811adffe070040def6a0b8feecff69e345c0d1c16364804bfb
|
3 |
+
size 56233680
|
another.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
# st.markdown(
|
8 |
+
# """
|
9 |
+
# <style>
|
10 |
+
# .stApp {
|
11 |
+
# background-image:url( "https://cdn.discordapp.com/attachments/1086260179139579955/1099734972971102298/img.png");
|
12 |
+
# background-size: cover;
|
13 |
+
# }
|
14 |
+
# </style>
|
15 |
+
# """,
|
16 |
+
# unsafe_allow_html=True
|
17 |
+
# )
|
18 |
+
|
19 |
+
labels = ['Actinic Keratoses', 'Basal Cell Carcinoma', 'Benign Keratosis-like Lesions', 'Dermatofibroma', 'Melanoma', 'Melanocytic Nevi', 'Vascular Lesions']
|
20 |
+
|
21 |
+
model = tf.keras.models.load_model('front_model_resnet.h5')
|
22 |
+
classify_model=tf.lite.Interpreter(model_path="InceptionResNetV2Skripsi.tflite")
|
23 |
+
classify_model.allocate_tensors()
|
24 |
+
|
25 |
+
input_details = classify_model.get_input_details()
|
26 |
+
output_details = classify_model.get_output_details()
|
27 |
+
|
28 |
+
def detect_skin(image):
|
29 |
+
# Convert the image to YCrCb color space
|
30 |
+
ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
|
31 |
+
|
32 |
+
# Apply skin color detection algorithm
|
33 |
+
lower_skin = np.array([0, 133, 77], dtype=np.uint8)
|
34 |
+
upper_skin = np.array([255, 173, 127], dtype=np.uint8)
|
35 |
+
mask = cv2.inRange(ycrcb, lower_skin, upper_skin)
|
36 |
+
|
37 |
+
# Apply morphological transformations to remove noise
|
38 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
|
39 |
+
mask = cv2.erode(mask, kernel, iterations=2)
|
40 |
+
mask = cv2.dilate(mask, kernel, iterations=2)
|
41 |
+
|
42 |
+
# Count the number of skin pixels
|
43 |
+
num_skin_pixels = cv2.countNonZero(mask)
|
44 |
+
|
45 |
+
# Calculate the ratio of skin pixels to total pixels
|
46 |
+
ratio = num_skin_pixels / (image.shape[0] * image.shape[1])
|
47 |
+
|
48 |
+
return ratio
|
49 |
+
|
50 |
+
def resize_image(image):
|
51 |
+
# Resize the image to 150x150 pixels
|
52 |
+
resized_image = tf.image.resize(image, [150, 150])
|
53 |
+
return resized_image.numpy()
|
54 |
+
|
55 |
+
def classify_image1(image):
|
56 |
+
# Pre-process the input image
|
57 |
+
resized_image = resize_image(image)
|
58 |
+
input_data = np.expand_dims(resized_image, axis=0).astype(np.float32)
|
59 |
+
classify_model.set_tensor(input_details[0]['index'], input_data)
|
60 |
+
|
61 |
+
# Run inference
|
62 |
+
with st.spinner('Classifying...'):
|
63 |
+
classify_model.invoke()
|
64 |
+
|
65 |
+
# Get the output probabilities
|
66 |
+
output_data = classify_model.get_tensor(output_details[0]['index'])
|
67 |
+
return output_data[0]
|
68 |
+
def classify_image(img, model):
|
69 |
+
image=img
|
70 |
+
img = img.resize((224, 224)) # Resize the image to match the model input size
|
71 |
+
img_array = np.array(img)
|
72 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
73 |
+
prediction = model.predict(img_array)
|
74 |
+
if prediction[0][0] > 0.5:
|
75 |
+
st.write("The image is classified as class Cancer")
|
76 |
+
# image = np.array(Image.open(image))
|
77 |
+
# st.image(image, width=150)
|
78 |
+
|
79 |
+
# Run inference on the input image
|
80 |
+
probs = classify_image1(image)
|
81 |
+
|
82 |
+
# # Display the top 3 predictions
|
83 |
+
top_3_indices = np.argsort(probs)[::-1][:3]
|
84 |
+
st.write("Top 3 predictions:")
|
85 |
+
for i in range(3):
|
86 |
+
st.write("%d. %s (%.2f%%)" % (i + 1, labels[top_3_indices[i]], probs[top_3_indices[i]] * 100))
|
87 |
+
ind=probs.argmax()
|
88 |
+
st.write("The Most possible label Will be:",labels[ind])
|
89 |
+
else:
|
90 |
+
st.write("The image is classified as class non cancer")
|
91 |
+
|
92 |
+
|
93 |
+
# Load the pre-trained model
|
94 |
+
model = tf.keras.models.load_model('front_model_resnet.h5')
|
95 |
+
classify_model=tf.lite.Interpreter(model_path="InceptionResNetV2Skripsi.tflite")
|
96 |
+
classify_model.allocate_tensors()
|
97 |
+
|
98 |
+
|
99 |
+
# Define the Streamlit app
|
100 |
+
st.title("Skin Cancer Detection")
|
101 |
+
st.sidebar.title('Input Image')
|
102 |
+
st.sidebar.markdown('Upload an image of a skin lesion to make a prediction.')
|
103 |
+
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png","HEIC"])
|
104 |
+
if uploaded_file is not None:
|
105 |
+
image = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
|
106 |
+
image = cv2.resize(image, (500, 500))
|
107 |
+
# image = cv2.resize(image, (224, 224))
|
108 |
+
|
109 |
+
# Detect skin in the image
|
110 |
+
ratio = detect_skin(image)
|
111 |
+
|
112 |
+
# Display the result
|
113 |
+
# st.image(image, caption="Uploaded Image", use_column_width=True)
|
114 |
+
st.write(f"Ratio of skin pixels to total pixels: {ratio:.2f}")
|
115 |
+
if ratio > 0.4:
|
116 |
+
st.write("The image contains skin.")
|
117 |
+
image = Image.open(uploaded_file)
|
118 |
+
st.image(image, width=300)
|
119 |
+
st.write("")
|
120 |
+
st.write("Classifying...")
|
121 |
+
label = classify_image(image, model)
|
122 |
+
else:
|
123 |
+
st.write("The image does not contain skin.")
|
124 |
+
|
125 |
+
|
front_model_resnet.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:60b5551c9ffeb968697abcd30c115b78de96747c34ded16bb86d82848b001ce7
|
3 |
+
size 711461336
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
numpy
|
3 |
+
panda
|
4 |
+
tensorflow
|
5 |
+
keras
|
6 |
+
matplotlib
|
7 |
+
|