elsoori commited on
Commit
b8fcc9f
·
verified ·
1 Parent(s): 2cd095d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -30
app.py CHANGED
@@ -1,11 +1,11 @@
1
-
2
  import streamlit as st
3
  import PIL.Image as Image
4
  import numpy as np
5
  import pandas as pd
6
  import requests
7
  from io import BytesIO
8
- from fastai.vision.all import load_learner # Corrected import
 
9
 
10
  # Initialize Streamlit app
11
  st.title("White Blood Cell Classifier")
@@ -13,42 +13,55 @@ st.title("White Blood Cell Classifier")
13
  # Load the FastAI model for WBC identification
14
  fastai_model = load_learner('model1.pkl')
15
 
16
- # File uploader for image input
17
- uploaded_file = st.file_uploader("Upload an image for classification", type=["jpg", "png"])
 
 
 
 
 
18
 
19
- if uploaded_file:
20
- # Open the uploaded image
21
- image = Image.open(uploaded_file)
22
-
23
- # Display the uploaded image with a caption
24
- st.image(image, caption="Uploaded Image", use_column_width=True)
25
 
26
- # Perform inference with the FastAI model
27
- pred, idx, probs = fastai_model.predict(image)
 
 
 
 
 
 
28
 
29
- # Display a title for the results section
30
- st.subheader("White Blood Cell Classification Results")
31
 
32
- # Define categories for classification
33
- categories = ('EOSINOPHIL', 'LYMPHOCYTE', 'MONOCYTE', 'NEUTROPHIL')
34
 
35
- # Create a DataFrame with classification probabilities
36
- results_df = pd.DataFrame(
37
- {'Cell Type': categories, 'Probability': probs.tolist()}
38
- )
39
 
40
- # Display the probabilities as a bar chart
41
- st.bar_chart(results_df.set_index('Cell Type'))
42
 
43
- # Highlight the most likely class
44
- most_likely_class = categories[idx]
45
- st.success(f"Predicted Class: {most_likely_class}")
46
 
47
- # Additional information about the probabilities
48
- st.write("Detailed Classification Results:")
49
- st.table(results_df)
50
 
51
- else:
52
- st.warning("Upload an image to start classification.")
53
 
 
 
 
54
 
 
 
 
 
 
1
  import streamlit as st
2
  import PIL.Image as Image
3
  import numpy as np
4
  import pandas as pd
5
  import requests
6
  from io import BytesIO
7
+ from fastai.vision.all import load_learner, untar_data, URLs
8
+ import gradio as gr
9
 
10
  # Initialize Streamlit app
11
  st.title("White Blood Cell Classifier")
 
13
  # Load the FastAI model for WBC identification
14
  fastai_model = load_learner('model1.pkl')
15
 
16
+ # Pre-load some example images with their corresponding labels
17
+ example_images = {
18
+ "Eosinophil": "Eosinophil.jpg",
19
+ "Lymphocyte": "Lymphocyte.jpg",
20
+ "Monocyte": "Monocyte.jpg",
21
+ "Neutrophil": "Neutrophil.jpg",
22
+ }
23
 
24
+ # Provide a choice of example images
25
+ st.sidebar.header("Example Images")
26
+ example_choice = st.sidebar.selectbox(
27
+ "Select an example image to classify",
28
+ list(example_images.keys())
29
+ )
30
 
31
+ # Display the chosen example image
32
+ example_url = example_images[example_choice]
33
+ response = requests.get(example_url)
34
+ example_image = Image.open(BytesIO(response.content))
35
+ st.sidebar.image(example_image, caption=f"Example: {example_choice}", use_column_width=True)
36
+
37
+ # File uploader for image input
38
+ uploaded_file = st.file_uploader("Upload your own image for classification", type=["jpg", "png"])
39
 
40
+ # Check if an image has been uploaded or an example has been selected
41
+ image_to_classify = example_image if uploaded_file is None else Image.open(uploaded_file)
42
 
43
+ # Display the image being classified
44
+ st.image(image_to_classify, caption="Image for Classification", use_column_width=True)
45
 
46
+ # Perform inference with the FastAI model
47
+ pred, idx, probs = fastai_model.predict(image_to_classify)
 
 
48
 
49
+ # Display a subheader for results
50
+ st.subheader("White Blood Cell Classification Results")
51
 
52
+ # Define categories for classification
53
+ categories = ('EOSINOPHIL', 'LYMPHOCYTE', 'MONOCYTE', 'NEUTROPHIL')
 
54
 
55
+ # Create a DataFrame with classification probabilities
56
+ results_df = pd.DataFrame({'Cell Type': categories, 'Probability': probs.tolist()})
 
57
 
58
+ # Display probabilities as a bar chart
59
+ st.bar_chart(results_df.set_index('Cell Type'))
60
 
61
+ # Highlight the most likely class
62
+ most_likely_class = categories[idx]
63
+ st.success(f"Predicted Class: {most_likely_class}")
64
 
65
+ # Additional information about probabilities
66
+ st.write("Detailed Classification Results:")
67
+ st.table(results_df)