nielitropar commited on
Commit
a782713
·
1 Parent(s): cd52df1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -53
app.py CHANGED
@@ -1,71 +1,56 @@
1
  import streamlit as st
 
 
 
2
  import numpy as np
3
- from PIL import Image
4
  from tensorflow.keras.preprocessing import image
5
- from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
6
- import cv2
 
 
 
 
 
7
 
8
  # Function to preprocess the image
9
- def preprocess_image(img_path):
10
- img = image.load_img(img_path, target_size=(200, 200))
11
  img_array = image.img_to_array(img)
12
  img_array = np.expand_dims(img_array, axis=0)
13
  img_array /= 255.0 # Normalize the image
14
  return img_array
15
 
16
- # Function to preprocess a set of images
17
- def preprocess_images(img_set):
18
- processed_set = []
19
- for img_path in img_set:
20
- processed_set.append(preprocess_image(img_path))
21
- return np.vstack(processed_set)
22
-
23
- # Function to predict brain tumor probability
24
- def predict_tumor_probability(model, img_set):
25
- processed_set = preprocess_images(img_set)
26
- return model.predict(processed_set)
27
-
28
- # Sidebar
29
- st.sidebar.title("Brain Tumor Detection App")
30
-
31
- # Upload images
32
- st.sidebar.header("Upload Images")
33
- uploaded_files = st.sidebar.file_uploader("Choose images...", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
34
-
35
- # Example images
36
- example_images = [
37
- 'examples/1 no.jpeg',
38
- 'examples/2 no.jpeg',
39
- 'examples/3 no.jpg',
40
- 'examples/1 yes.jpg',
41
- 'examples/2 yes.jpg',
42
- 'examples/3 yes.jpg'
43
- ]
44
-
45
- # Display examples
46
- st.sidebar.header("Example Images")
47
- selected_examples = st.sidebar.multiselect("Select example images:", example_images)
48
 
49
- # Combine uploaded and selected examples
50
- selected_images = uploaded_files or selected_examples
51
 
52
- if selected_images:
53
- st.sidebar.header("Selected Images")
54
- st.sidebar.image(selected_images, width=100)
 
 
55
 
56
- # Predict tumor probability
57
- st.header("Brain Tumor Detection Results")
58
- st.subheader("Uploaded and Selected Images")
 
59
 
60
- # Load the model
61
- braintumor_model = load_model('models/brain_tumor_binary.h5')
62
 
63
- # Display prediction results
64
- probabilities = predict_tumor_probability(braintumor_model, selected_images)
 
65
 
66
- for i, img_path in enumerate(selected_images):
67
- st.image(img_path, caption=f"Probability: {probabilities[i][0]:.2%}", use_column_width=True)
68
 
69
- else:
70
- st.warning("Please upload or select at least one image.")
 
 
71
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ from werkzeug.utils import secure_filename
4
+ import cv2
5
  import numpy as np
6
+ from tensorflow.keras.models import load_model
7
  from tensorflow.keras.preprocessing import image
8
+
9
+ # Load the trained model
10
+ model = load_model("Bone_fracture_classifier_model.h5")
11
+
12
+ # Function to check if the file extension is allowed
13
+ def allowed_file(filename):
14
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'jpg', 'jpeg', 'png'}
15
 
16
  # Function to preprocess the image
17
+ def preprocess_image(file_path):
18
+ img = image.load_img(file_path, target_size=(200, 200))
19
  img_array = image.img_to_array(img)
20
  img_array = np.expand_dims(img_array, axis=0)
21
  img_array /= 255.0 # Normalize the image
22
  return img_array
23
 
24
+ def main():
25
+ st.title("Bone Fracture Detection App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
28
 
29
+ if uploaded_file is not None:
30
+ # Check if the file extension is allowed
31
+ if allowed_file(uploaded_file.name):
32
+ # Display the selected image
33
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
34
 
35
+ # Save the uploaded image temporarily
36
+ temp_image_path = "temp_image.jpg"
37
+ with open(temp_image_path, "wb") as temp_image:
38
+ temp_image.write(uploaded_file.read())
39
 
40
+ # Preprocess the image
41
+ img_array = preprocess_image(temp_image_path)
42
 
43
+ # Make prediction
44
+ prediction = model.predict(img_array)[0, 0]
45
+ result = "Broken" if prediction > 0.5 else "Not Broken"
46
 
47
+ st.write(f"Prediction: {result}")
48
+ st.write(f"Confidence: {prediction:.2%}")
49
 
50
+ # Remove the temporary image file
51
+ os.remove(temp_image_path)
52
+ else:
53
+ st.warning("Invalid file format. Please upload an image with a valid format (jpg, jpeg, or png).")
54
 
55
+ if __name__ == "__main__":
56
+ main()