├── LICENSE ├── README.md ├── bgs └── bg5.png ├── main.py ├── model ├── labels.txt └── pneumonia_classifier.h5 ├── requirements.txt └── util.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Computer vision engineer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pneumonia-classification-web-app-python-streamlit 2 | 3 | 4 |

5 | 6 | Watch the video 7 |
Watch on YouTube: Pneumonia classification web app with Python and Streamlit ! 8 |
9 |

10 | 11 | ## model 12 | 13 | A pneumonia classifier was used to classify X-RAY images into {PNEUMONIA, NORMAL}. 14 | 15 | The model was trained using the data provided in the next section and following [this step by step tutorial on how to train an image classifier with Teachable Machine](https://youtu.be/ybh9p3QOYrs). 16 | 17 | The trained model is available in my [Patreon](https://www.patreon.com/ComputerVisionEngineer). 18 | 19 | ## data 20 | 21 | - Data: https://data.mendeley.com/datasets/rscbjbr9sj/2 22 | - License: CC BY 4.0 23 | - Citation: http://www.cell.com/cell/fulltext/S0092-8674(18)30154-5 24 | -------------------------------------------------------------------------------- /bgs/bg5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/computervisioneng/pneumonia-classification-web-app-python-streamlit/16ced15a382fe2befada1b238b130ed702add3d7/bgs/bg5.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | from keras.models import load_model 3 | from PIL import Image 4 | import numpy as np 5 | 6 | from util import classify, set_background 7 | 8 | 9 | set_background('./bgs/bg5.png') 10 | 11 | # set title 12 | st.title('Pneumonia classification') 13 | 14 | # set header 15 | st.header('Please upload a chest X-ray image') 16 | 17 | # upload file 18 | file = st.file_uploader('', type=['jpeg', 'jpg', 'png']) 19 | 20 | # load classifier 21 | model = load_model('./model/pneumonia_classifier.h5') 22 | 23 | # load class names 24 | with open('./model/labels.txt', 'r') as f: 25 | class_names = [a[:-1].split(' ')[1] for a in f.readlines()] 26 | f.close() 27 | 28 | # display image 29 | if file is not None: 30 | image = Image.open(file).convert('RGB') 31 | st.image(image, use_column_width=True) 32 | 33 | # classify image 34 | class_name, conf_score = classify(image, model, class_names) 35 | 36 | # write classification 37 | st.write("## {}".format(class_name)) 38 | st.write("### score: {}%".format(int(conf_score * 1000) / 10)) 39 | -------------------------------------------------------------------------------- /model/labels.txt: -------------------------------------------------------------------------------- 1 | 0 PNEUMONIA 2 | 1 NORMAL 3 | -------------------------------------------------------------------------------- /model/pneumonia_classifier.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/computervisioneng/pneumonia-classification-web-app-python-streamlit/16ced15a382fe2befada1b238b130ed702add3d7/model/pneumonia_classifier.h5 -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.23.5 2 | streamlit==1.22.0 3 | Pillow==9.5.0 4 | keras==2.12.0 5 | tensorflow==2.12.0 6 | -------------------------------------------------------------------------------- /util.py: -------------------------------------------------------------------------------- 1 | import base64 2 | 3 | import streamlit as st 4 | from PIL import ImageOps, Image 5 | import numpy as np 6 | 7 | 8 | def set_background(image_file): 9 | """ 10 | This function sets the background of a Streamlit app to an image specified by the given image file. 11 | 12 | Parameters: 13 | image_file (str): The path to the image file to be used as the background. 14 | 15 | Returns: 16 | None 17 | """ 18 | with open(image_file, "rb") as f: 19 | img_data = f.read() 20 | b64_encoded = base64.b64encode(img_data).decode() 21 | style = f""" 22 | 28 | """ 29 | st.markdown(style, unsafe_allow_html=True) 30 | 31 | 32 | def classify(image, model, class_names): 33 | """ 34 | This function takes an image, a model, and a list of class names and returns the predicted class and confidence 35 | score of the image. 36 | 37 | Parameters: 38 | image (PIL.Image.Image): An image to be classified. 39 | model (tensorflow.keras.Model): A trained machine learning model for image classification. 40 | class_names (list): A list of class names corresponding to the classes that the model can predict. 41 | 42 | Returns: 43 | A tuple of the predicted class name and the confidence score for that prediction. 44 | """ 45 | # convert image to (224, 224) 46 | image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS) 47 | 48 | # convert image to numpy array 49 | image_array = np.asarray(image) 50 | 51 | # normalize image 52 | normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1 53 | 54 | # set model input 55 | data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32) 56 | data[0] = normalized_image_array 57 | 58 | # make prediction 59 | prediction = model.predict(data) 60 | # index = np.argmax(prediction) 61 | index = 0 if prediction[0][0] > 0.95 else 1 62 | class_name = class_names[index] 63 | confidence_score = prediction[0][index] 64 | 65 | return class_name, confidence_score 66 | --------------------------------------------------------------------------------