Commit
·
8635d04
1
Parent(s):
5675222
app created
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from patchify import patchify
|
6 |
+
from huggingface_hub import from_pretrained_keras
|
7 |
+
|
8 |
+
m = from_pretrained_keras('ErnestBeckham/ViT-Lungs')
|
9 |
+
|
10 |
+
hp = {}
|
11 |
+
hp['class_names'] = ["lung_aca", "lung_n", "lung_scc"]
|
12 |
+
|
13 |
+
def main():
|
14 |
+
st.title("Multi-Cancer Classification")
|
15 |
+
|
16 |
+
# Upload image through drag and drop
|
17 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
18 |
+
|
19 |
+
if uploaded_file is not None:
|
20 |
+
# Convert the uploaded file to OpenCV format
|
21 |
+
image = convert_to_opencv(uploaded_file)
|
22 |
+
|
23 |
+
# Display the uploaded image
|
24 |
+
st.image(image, channels="BGR", caption="Uploaded Image", use_column_width=True)
|
25 |
+
|
26 |
+
# Display the image shape
|
27 |
+
image_class = predict_single_image(image, model, hp)
|
28 |
+
st.write(f"Image Class: {image_class}")
|
29 |
+
|
30 |
+
def convert_to_opencv(uploaded_file):
|
31 |
+
# Read the uploaded file using OpenCV
|
32 |
+
image_bytes = uploaded_file.read()
|
33 |
+
np_arr = np.frombuffer(image_bytes, np.uint8)
|
34 |
+
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
35 |
+
return image
|
36 |
+
|
37 |
+
def process_image_as_batch(path):
|
38 |
+
#read image
|
39 |
+
image = cv2.imread(path, cv2.IMREAD_COLOR)
|
40 |
+
#resize the image
|
41 |
+
image = cv2.resize(image, [512, 512])
|
42 |
+
#scale the image
|
43 |
+
image = image / 255.0
|
44 |
+
#change the data type of image
|
45 |
+
image = image.astype(np.float32)
|
46 |
+
return image
|
47 |
+
|
48 |
+
def predict_single_image(image, model, hp):
|
49 |
+
# Preprocess the image
|
50 |
+
preprocessed_image = process_image_as_batch(image, hp)
|
51 |
+
# Convert the preprocessed image to a TensorFlow tensor if needed
|
52 |
+
preprocessed_image = tf.convert_to_tensor(preprocessed_image)
|
53 |
+
# Add an extra batch dimension (required for model.predict)
|
54 |
+
preprocessed_image = tf.expand_dims(preprocessed_image, axis=0)
|
55 |
+
# Make the prediction
|
56 |
+
predictions = model.predict(preprocessed_image)
|
57 |
+
|
58 |
+
np.around(predictions)
|
59 |
+
y_pred_classes = np.argmax(predictions, axis=1)
|
60 |
+
class_name = hp['class_names'][y_pred_classes[0]]
|
61 |
+
return class_name
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
main()
|