Spaces:
Sleeping
Sleeping
Commit ·
db4a5d2
1
Parent(s): 72305ce
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|