File size: 3,128 Bytes
47914fc
 
19233ea
f4079bb
47914fc
 
f6d3f4b
 
 
 
 
 
f4079bb
 
 
 
 
279f468
 
 
 
 
 
 
 
f6d3f4b
 
 
 
 
 
 
 
 
 
 
 
f4079bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6d3f4b
 
 
f4079bb
f6d3f4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4079bb
 
 
 
 
 
 
 
265e5de
 
 
 
 
 
f4079bb
 
f6d3f4b
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import subprocess

#port =  $PORT
#subprocess.run("uvicorn application.server.main:app --host 0.0.0.0 --port 7860", shell=True)


import streamlit as st
import json
import requests
from PIL import Image
import os

#option 2
import numpy as np
from tensorflow.keras.models import load_model
import cv2

#Hide Made with streamlit
st.markdown(
'''<style>
#MainMenu{visibility: hidden;} footer{visibility: hidden;}
#root>div:nth-child(1)>div>div>div>div>section>div{padding-top: .2rem;
</style>''', unsafe_allow_html=True
)


def load_image(image):
	img = Image.open(image)
	return img

def save_uploadedfile(uploadedfile):
     with open(os.path.join("images/img",uploadedfile.name),"wb") as f:
         f.write(uploadedfile.getbuffer())
         uploaded_location = os.path.join("images/img",uploadedfile.name)
     return uploaded_location#st.success("Saved File:{} to {}".format(uploadedfile.name, uploaded_location))
     

def image_predict (image_file):
    model_path = 'application/models/resnet_ct.h5'  
    h5_model = load_model(model_path)
    image = cv2.imread(image_file)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (224, 224)) 
    image = np.array(image) / 255
    image = np.expand_dims(image, axis=0)
    h5_prediction = h5_model.predict(image)  
    print('Prediction from h5 model: {}'.format(h5_prediction))
    print(h5_prediction)
    probability = h5_prediction[0]
    print("H5 Predictions:")
    print (probability)
    if probability[0] > 0.5:
        covid_chest_pred = str('%.2f' % (probability[0] * 100) + '% COVID-Positive')
        probability = (probability[0] * 100)
    else:
        covid_chest_pred = str('%.2f' % ((1 - probability[0]) * 100) + '% COVID-Negative')
        probability = ((1 - probability[0]) * 100)
    return  covid_chest_pred
         

st.title("Covid Prediction App from CT Images")



#taking user inputs

st.write("")

#converting input to json


image = st.file_uploader("Upload CT Scan", type=["png","jpg","jpeg"])
 
if image is not None:
	  # To See details
	  file_details = {"filename":image.name, "filetype":image.type,
	      "filesize":image.size}
	  st.write(file_details)
	  #image1 = Image.open(image)
	  #img_array = np.array(image1)

	#View Uploaded Image
	  st.image(load_image(image),width=250)
	  #save image to disk
	  saved = save_uploadedfile(image)

	  #if st.button ('Analyze'):
	  	  #test_file = open(os.path.join("images/img", image.name), "rb")
	  	  #response = requests.post('http://127.0.0.1:8000/predict/image', files={'file': test_file })
	  	  #prediction = response.json()##json_object["prediction"]
	  	  #st.write(prediction)
	  	  #st. subheader (f"Response from Covid Analyzer API = {prediction}")

         #OPTION 2 - NON API..
	  if st.button ('Analyze'):
	  	  with st.spinner('Analyzing...'):
	  	  	  prediction = image_predict(saved)
	  	  	  #st.write(prediction)
	  	  	  st. subheader (f"Image Prediction = {prediction}")
	  	  	  st.success(f"Image Prediction = {prediction}", icon="✅")

    

#RUN BOTH...
#streamlit run app.py
#uvicorn application.server.main:app 

#OPTION 2....