import cv2
from PIL import Image, ImageOps
import numpy as np
from tensorflow.keras.models import load_model
import streamlit as st
from lime import lime_image
import matplotlib.pyplot as plt
import streamlit.components.v1 as components
from tensorflow.keras.preprocessing import image as IM
from skimage.segmentation import mark_boundaries
model_path = "C:/Users/akhil/Desktop/MalariaDetectionWebapp/malaria_calssifier_model.h5"
model = load_model(model_path)
def import_and_predict(image_data, model):
size = (50, 50)
image = ImageOps.fit(image_data, size, Image.ANTIALIAS)
image = np.asarray(image)
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img_resize = (
cv2.resize(img, dsize=(50, 50), interpolation=cv2.INTER_CUBIC)
) / 255.0
img_reshape = img_resize[np.newaxis, ...]
prediction = model.predict(img_reshape)
return prediction
def explain_prediction(image_data, model):
image = IM.load_img(image_data, target_size=(50, 50))
image = IM.img_to_array(image)
img = np.expand_dims(image, axis=0)
explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(
img[0].astype("double"),
model.predict,
top_labels=2,
hide_color=0,
num_samples=1000,
)
temp_1, mask_1 = explanation.get_image_and_mask(
explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=True
)
temp_2, mask_2 = explanation.get_image_and_mask(
explanation.top_labels[0], positive_only=False, num_features=10, hide_rest=False
)
return temp_1, mask_1, temp_2, mask_2
st.write(
"""
# Malaria Detection Webapp
"""
)
st.write(
"This is a simple Malaria classification web app to predict Parasitisized or Unifected Malaria cell"
)
file = st.file_uploader("Please upload an image file", type=["jpg", "png"])
#
if file is None:
st.text("You haven't uploaded an image file")
else:
image = Image.open(file)
st.image(image, use_column_width=True)
prediction = import_and_predict(image, model)
# temp_1, mask_1, temp_1, mask_1 = explain_prediction(image, model)
# st.image(mark_boundaries(temp_1, mask_1))
if np.argmax(prediction) == 0:
st.write("The Image is Infected !")
else:
st.write("The Image is Not Infected !")
这是我的代码(粗略)。我无法创建假设如果我上传图像的部分,我会将该图像视为输出。
不完全像这张图片,但如果我上传一张合适类别的图片,预测应该是下面这张图片的输出。
请如果有人有任何线索,请告诉我!