Prajwal3009's picture
simple
e1d9a75 verified
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model
import cv2
# Load the saved model
loaded_model = load_model("accident_detection_model.h5")
st.title("Accident Detection")
# Checkbox to enable camera input
camera_checkbox = st.checkbox("Use Camera")
if camera_checkbox:
image = st.camera_input("Take Photo")
if(st.button("Capture")):
# Preprocess the image
image = Image.open(image)
img_array = np.array(image.resize((150, 150)))
img_array = img_array / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Classify the image
prediction = loaded_model.predict(img_array)
if prediction[0] > 0.5:
st.write("Prediction: Accident")
else:
st.write("Prediction: Non-Accident")
else:
# Upload image
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image.', use_column_width=True)
st.write("")
st.write("Classifying...")
# Preprocess the image
img_array = np.array(image.resize((150, 150)))
img_array = img_array / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Classify the image
prediction = loaded_model.predict(img_array)
if prediction[0] > 0.5:
st.write("Prediction: Accident")
else:
st.write("Prediction: Non-Accident")