microScan / appTest.py
crazyscientist
updated st.cache
25cd769 unverified
raw history blame
No virus
4.42 kB
import streamlit as st
from PIL import Image
from modInference import main
import numpy as np
import math
import numpy as np
from skimage import transform
import os
from keras.models import Model
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.layers import Dense, Dropout, Flatten
import numpy as np
import torch
from models.create_fasterrcnn_model import create_model
st.set_page_config(layout="wide")
st.markdown("")
showImg = Image.open('3dots.jpg')
ogInp = Image.open('3dots.jpg')
showImg = showImg.resize((200, 200))
ogInp = ogInp.resize((200, 200))
cellImgs = []
st.title('MicroScan In Action!')
st.subheader("Enter an image of a thin blood smear. Preview the image and run the application. This program was developed by Anish Pallod =)")
input, outputIm = st.columns(2)
@st.cache_resource
def load_model():
NUM_CLASSES = 2
CLASSES = ['__background__', 'Cell']
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(DEVICE)
checkpoint = torch.load("best_model.pth", map_location=DEVICE)
NUM_CLASSES = checkpoint['config']['NC']
CLASSES = checkpoint['config']['CLASSES']
build_model = create_model[checkpoint['model_name']]
model = build_model(num_classes=NUM_CLASSES, coco_model=False)
model.load_state_dict(checkpoint['model_state_dict'])
model.to(DEVICE).eval()
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
conv_base = VGG16(include_top=False,
weights='imagenet',
input_shape=(200,200,3))
if 2 > 0:
for layer in conv_base.layers[:-2]:
layer.trainable = False
else:
for layer in conv_base.layers:
layer.trainable = False
top_model = conv_base.output
top_model = Flatten(name="flatten")(top_model)
top_model = Dense(4096, activation='relu')(top_model)
top_model = Dense(1048, activation='relu')(top_model)
top_model = Dense(256, activation='relu')(top_model)
top_model = Dense(128, activation='relu')(top_model)
top_model = Dense(64, activation='relu')(top_model)
top_model = Dropout(0.2)(top_model)
output_layer = Dense(5, activation='softmax')(top_model)
CNN = Model(inputs=conv_base.input, outputs=output_layer)
CNN.load_weights("CNN.hdf5")
return CNN, model
CNN, model = load_model()
with input:
st.header("Input")
imageInput = st.file_uploader("Enter an image of a thin blood smear.")
if st.button("Run"):
if imageInput is not None:
image = Image.open(imageInput)
ogInp = image
img_array = np.array(image)
output, cellImgs = main(CNN, model, img_array)
showImg = Image.fromarray(output)
if st.button("Preview"):
if imageInput is not None:
image = Image.open(imageInput)
ogInp = image
st.write("-" * 34)
st.header("How it looks:")
st.image(ogInp)
else:
st.write("-" * 34)
st.header("How it looks:")
st.image(ogInp)
with outputIm:
st.header("General Output")
st.image(showImg)
st.write("-" * 34)
st.header("Segmented Cell Output")
st.markdown("""
<style>
[data-testid=column] [data-testid=stVerticalBlock]{
gap: 0.3rem;
}
</style>
""",unsafe_allow_html=True)
col1,col2,col3,col4,col5,col6,col7 = st.columns(7)
total = len(cellImgs)
print(cellImgs)
barrier = []
for k in range(1, 8):
barrier.append(math.floor(total/7) * k)
leftOver = total % 7
for k in range(leftOver):
barrier[k] += 1
print(barrier)
with col1:
for x in cellImgs[0:barrier[0]]:
st.write(x[1])
st.image(x[0])
with col2:
for x in cellImgs[barrier[0]: barrier[1]]:
st.write(x[1])
st.image(x[0])
with col3:
for x in cellImgs[barrier[1]: barrier[2]]:
st.write(x[1])
st.image(x[0])
with col4:
for x in cellImgs[barrier [2]: barrier[3]]:
st.write(x[1])
st.image(x[0])
with col5:
for x in cellImgs[barrier[3]: barrier[4]]:
st.write(x[1])
st.image(x[0])
with col6:
for x in cellImgs[barrier[4]: barrier[5]]:
st.write(x[1])
st.image(x[0])
with col7:
for x in cellImgs[barrier[5]: barrier[6]]:
st.write(x[1])
st.image(x[0])
# with parameters:
# st.header("Parameters")