Spaces:
Sleeping
Sleeping
Commit
·
86dfc98
1
Parent(s):
3767c4e
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import joblib
|
5 |
+
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
# Load the trained KNN model
|
9 |
+
model_file_path = 'skin_cancer_model.pkl'
|
10 |
+
model = joblib.load(model_file_path)
|
11 |
+
|
12 |
+
# Function to preprocess the image
|
13 |
+
def preprocess_image(image):
|
14 |
+
# Resize the image to a fixed size (e.g., 128x128)
|
15 |
+
size = (224, 224)
|
16 |
+
image = image.resize(size)
|
17 |
+
# Convert the image to a numpy array
|
18 |
+
image_array = np.array(image)
|
19 |
+
# Flatten the image array
|
20 |
+
image_flattened = image_array.flatten()
|
21 |
+
return image_flattened
|
22 |
+
|
23 |
+
# Define a video transformer class to process video frames
|
24 |
+
class VideoTransformer(VideoTransformerBase):
|
25 |
+
def transform(self, frame):
|
26 |
+
img = frame.to_ndarray(format="bgr24")
|
27 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
28 |
+
|
29 |
+
# Preprocess image for the model (resize to match model input size)
|
30 |
+
img_resized = cv2.resize(img, (224, 224)) # Adjust size as per your model input
|
31 |
+
img_resized = img_resized.astype("float32") / 255.0
|
32 |
+
img_resized = np.expand_dims(img_resized, axis=0) # Add batch dimension
|
33 |
+
|
34 |
+
# Make prediction
|
35 |
+
prediction = model.predict(img_resized)
|
36 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
37 |
+
|
38 |
+
# Display predicted class
|
39 |
+
#cv2.putText(img, f"Predicted Class: {predicted_class}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
40 |
+
st.write(f"Skin Cancer Prediction: {predicted_class}")
|
41 |
+
|
42 |
+
return img
|
43 |
+
|
44 |
+
def main():
|
45 |
+
st.title("Skin Diagnosis")
|
46 |
+
|
47 |
+
st.write("Please note that this app is not 100% accurate. If your result happens to be malignant, please contact a medical professional for further instructions.")
|
48 |
+
|
49 |
+
# Use the webrtc_streamer to capture video from the camera
|
50 |
+
webrtc_streamer(key="example", video_processor_factory=VideoTransformer)
|
51 |
+
|
52 |
+
st.write("**Upload an image to detect if it's benign or malignant:**")
|
53 |
+
|
54 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
55 |
+
|
56 |
+
if uploaded_file is not None:
|
57 |
+
image = Image.open(uploaded_file)
|
58 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
59 |
+
|
60 |
+
# Preprocess the image
|
61 |
+
processed_image = preprocess_image(image)
|
62 |
+
|
63 |
+
# Make prediction
|
64 |
+
prediction = model.predict([processed_image])[0]
|
65 |
+
class_names = {0: 'Benign', 1: 'Malignant'}
|
66 |
+
result = class_names[prediction]
|
67 |
+
|
68 |
+
st.write(f"Skin Cancer Prediction: {result}")
|
69 |
+
st.write("**Please click the button if you have had a large amount of sun exposure:**")
|
70 |
+
result = st.button("Yes")
|
71 |
+
if result:
|
72 |
+
st.write("Please note that high levels of sun exposure can lead to a higher risk of skin cancer.")
|
73 |
+
|
74 |
+
if __name__ == '__main__':
|
75 |
+
main()
|