segestic commited on
Commit
f4079bb
·
1 Parent(s): 8c9de91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -7
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import subprocess
2
 
3
  #port = $PORT
4
- subprocess.run("uvicorn application.server.main:app --host 0.0.0.0 --port 7860", shell=True)
5
 
6
 
7
  import streamlit as st
@@ -10,6 +10,11 @@ import requests
10
  from PIL import Image
11
  import os
12
 
 
 
 
 
 
13
 
14
  def load_image(image):
15
  img = Image.open(image)
@@ -22,9 +27,33 @@ def save_uploadedfile(uploadedfile):
22
  return uploaded_location#st.success("Saved File:{} to {}".format(uploadedfile.name, uploaded_location))
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  st.title("Covid Prediction App from CT Images")
26
 
27
 
 
28
  #taking user inputs
29
 
30
  st.write("")
@@ -47,12 +76,22 @@ if image is not None:
47
  #save image to disk
48
  saved = save_uploadedfile(image)
49
 
50
- if st.button ('Analyze'):
51
- test_file = open(os.path.join("images/img", image.name), "rb")
52
- response = requests.post('http://127.0.0.1:8000/predict/image', files={'file': test_file })
53
- prediction = response.json()##json_object["prediction"]
54
- st.write(prediction)
55
- st. subheader (f"Response from Covid Analyzer API = {prediction}")
 
 
 
 
 
 
 
 
 
 
56
 
57
  #RUN BOTH...
58
  #streamlit run app.py
 
1
  import subprocess
2
 
3
  #port = $PORT
4
+ #subprocess.run("uvicorn application.server.main:app --host 0.0.0.0 --port 7860", shell=True)
5
 
6
 
7
  import streamlit as st
 
10
  from PIL import Image
11
  import os
12
 
13
+ #option 2
14
+ import numpy as np
15
+ from tensorflow.keras.models import load_model
16
+ import cv2
17
+
18
 
19
  def load_image(image):
20
  img = Image.open(image)
 
27
  return uploaded_location#st.success("Saved File:{} to {}".format(uploadedfile.name, uploaded_location))
28
 
29
 
30
+ def image_predict (image_file):
31
+ model_path = 'application/models/resnet_ct.h5'
32
+ h5_model = load_model(model_path)
33
+ image = cv2.imread(image_file)
34
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
35
+ image = cv2.resize(image, (224, 224))
36
+ image = np.array(image) / 255
37
+ image = np.expand_dims(image, axis=0)
38
+ h5_prediction = h5_model.predict(image)
39
+ print('Prediction from h5 model: {}'.format(h5_prediction))
40
+ print(h5_prediction)
41
+ probability = h5_prediction[0]
42
+ print("H5 Predictions:")
43
+ print (probability)
44
+ if probability[0] > 0.5:
45
+ covid_chest_pred = str('%.2f' % (probability[0] * 100) + '% COVID-Positive')
46
+ probability = (probability[0] * 100)
47
+ else:
48
+ covid_chest_pred = str('%.2f' % ((1 - probability[0]) * 100) + '% COVID-Negative')
49
+ probability = ((1 - probability[0]) * 100)
50
+ return covid_chest_pred
51
+
52
+
53
  st.title("Covid Prediction App from CT Images")
54
 
55
 
56
+
57
  #taking user inputs
58
 
59
  st.write("")
 
76
  #save image to disk
77
  saved = save_uploadedfile(image)
78
 
79
+ #if st.button ('Analyze'):
80
+ #test_file = open(os.path.join("images/img", image.name), "rb")
81
+ #response = requests.post('http://127.0.0.1:8000/predict/image', files={'file': test_file })
82
+ #prediction = response.json()##json_object["prediction"]
83
+ #st.write(prediction)
84
+ #st. subheader (f"Response from Covid Analyzer API = {prediction}")
85
+
86
+ #OPTION 2 - NON API..
87
+ if st.button ('Analyze'):
88
+ with st.spinner('Analyzing...'):
89
+ prediction = image_predict(saved)
90
+ #st.write(prediction)
91
+ st. subheader (f"Image Prediction = {prediction}")
92
+ st.success(f"Image Prediction = {prediction}", icon="✅")
93
+
94
+
95
 
96
  #RUN BOTH...
97
  #streamlit run app.py