Spaces:
Sleeping
Sleeping
Create app.py
Browse filesapplication file
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import models
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from util import classify, set_background
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
st.title('Chest X-Ray Pneumonia Detector')
|
| 9 |
+
|
| 10 |
+
st.header('Please upload a chest X-ray image.')
|
| 11 |
+
|
| 12 |
+
file = st.file_uploader('-', type=['jpeg', 'jpg', 'png'])
|
| 13 |
+
|
| 14 |
+
# load classifier
|
| 15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
model = models.resnet18(weights=False)
|
| 17 |
+
num_ftrs = model.fc.in_features
|
| 18 |
+
model.fc = torch.nn.Linear(num_ftrs, 2) # binary classification
|
| 19 |
+
model.load_state_dict(torch.load('./resnet18.pth', map_location=device))
|
| 20 |
+
model.to(device)
|
| 21 |
+
model.eval()
|
| 22 |
+
|
| 23 |
+
# load class names
|
| 24 |
+
# class_names = ['Normal', 'Pneumonia']
|
| 25 |
+
with open('./labels.txt', 'r') as f:
|
| 26 |
+
class_names = [a[:-1].split(' ')[1] for a in f.readlines()]
|
| 27 |
+
f.close()
|
| 28 |
+
|
| 29 |
+
# display image
|
| 30 |
+
if file is not None:
|
| 31 |
+
image = Image.open(file).convert('RGB')
|
| 32 |
+
st.image(image, use_column_width=True)
|
| 33 |
+
|
| 34 |
+
# classify image
|
| 35 |
+
class_name, conf_score = classify(image, model, class_names)
|
| 36 |
+
|
| 37 |
+
# write classification
|
| 38 |
+
st.write("## {}".format(class_name))
|
| 39 |
+
st.write("### Confidence: {:.2f}%".format(conf_score * 100))
|
| 40 |
+
|
| 41 |
+
set_background('./background.jpg')
|
| 42 |
+
# Footer
|
| 43 |
+
footer = """
|
| 44 |
+
<div style="position: fixed; bottom: 0; width: 100%; background-color: #EDF3FA; padding: 10px; text-align: center;">
|
| 45 |
+
Created by Imran Nawar
|
| 46 |
+
</div>
|
| 47 |
+
"""
|
| 48 |
+
st.markdown(footer, unsafe_allow_html=True)
|