|
import streamlit as st |
|
import tensorflow as tf |
|
import numpy as np |
|
from PIL import Image |
|
import cv2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
labels = ['Actinic Keratoses', 'Basal Cell Carcinoma', 'Benign Keratosis-like Lesions', 'Dermatofibroma', 'Melanoma', 'Melanocytic Nevi', 'Vascular Lesions'] |
|
|
|
model = tf.keras.models.load_model('front_model_resnet.h5') |
|
classify_model=tf.lite.Interpreter(model_path="InceptionResNetV2Skripsi.tflite") |
|
classify_model.allocate_tensors() |
|
|
|
input_details = classify_model.get_input_details() |
|
output_details = classify_model.get_output_details() |
|
|
|
def detect_skin(image): |
|
|
|
ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb) |
|
|
|
|
|
lower_skin = np.array([0, 133, 77], dtype=np.uint8) |
|
upper_skin = np.array([255, 173, 127], dtype=np.uint8) |
|
mask = cv2.inRange(ycrcb, lower_skin, upper_skin) |
|
|
|
|
|
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) |
|
mask = cv2.erode(mask, kernel, iterations=2) |
|
mask = cv2.dilate(mask, kernel, iterations=2) |
|
|
|
|
|
num_skin_pixels = cv2.countNonZero(mask) |
|
|
|
|
|
ratio = num_skin_pixels / (image.shape[0] * image.shape[1]) |
|
|
|
return ratio |
|
|
|
def resize_image(image): |
|
|
|
resized_image = tf.image.resize(image, [150, 150]) |
|
return resized_image.numpy() |
|
|
|
def classify_image1(image): |
|
|
|
resized_image = resize_image(image) |
|
input_data = np.expand_dims(resized_image, axis=0).astype(np.float32) |
|
classify_model.set_tensor(input_details[0]['index'], input_data) |
|
|
|
|
|
with st.spinner('Classifying...'): |
|
classify_model.invoke() |
|
|
|
|
|
output_data = classify_model.get_tensor(output_details[0]['index']) |
|
return output_data[0] |
|
def classify_image(img, model): |
|
image=img |
|
img = img.resize((224, 224)) |
|
img_array = np.array(img) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
prediction = model.predict(img_array) |
|
if prediction[0][0] > 0.5: |
|
st.write("The image is classified as class Cancer") |
|
|
|
|
|
|
|
|
|
probs = classify_image1(image) |
|
|
|
|
|
top_3_indices = np.argsort(probs)[::-1][:3] |
|
st.write("Top 3 predictions:") |
|
for i in range(3): |
|
st.write("%d. %s (%.2f%%)" % (i + 1, labels[top_3_indices[i]], probs[top_3_indices[i]] * 100)) |
|
ind=probs.argmax() |
|
st.write("The Most possible label Will be:",labels[ind]) |
|
else: |
|
st.write("The image is classified as class non cancer") |
|
|
|
|
|
|
|
model = tf.keras.models.load_model('front_model_resnet.h5') |
|
classify_model=tf.lite.Interpreter(model_path="InceptionResNetV2Skripsi.tflite") |
|
classify_model.allocate_tensors() |
|
|
|
|
|
|
|
st.title("Skin Cancer Detection") |
|
st.sidebar.title('Input Image') |
|
st.sidebar.markdown('Upload an image of a skin lesion to make a prediction.') |
|
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png","HEIC"]) |
|
if uploaded_file is not None: |
|
image = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1) |
|
image = cv2.resize(image, (500, 500)) |
|
|
|
|
|
|
|
ratio = detect_skin(image) |
|
|
|
|
|
|
|
st.write(f"Ratio of skin pixels to total pixels: {ratio:.2f}") |
|
if ratio > 0.4: |
|
st.write("The image contains skin.") |
|
image = Image.open(uploaded_file) |
|
st.image(image, width=300) |
|
st.write("") |
|
st.write("Classifying...") |
|
label = classify_image(image, model) |
|
else: |
|
st.write("The image does not contain skin.") |
|
|
|
|