Spaces:
Runtime error
Runtime error
File size: 2,283 Bytes
74ec428 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import streamlit as st
import torch
import cv2
import numpy as np
from PIL import Image
import tempfile
import os
# Load the model from local path
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
def detect_fall(image):
results = model(image)
return results
def main():
st.title("Fall Detection Application")
option = st.sidebar.selectbox("Choose input type", ("Upload Image/Video", "Use Camera"))
if option == "Upload Image/Video":
uploaded_file = st.file_uploader("Upload Image or Video", type=['jpg', 'jpeg', 'png', 'mp4', 'avi', 'mov'])
if uploaded_file is not None:
if uploaded_file.type.startswith('image'):
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
image = cv2.imdecode(file_bytes, 1)
st.image(image, caption='Uploaded Image.', use_column_width=True)
results = detect_fall(image)
st.image(np.squeeze(results.render()), caption='Processed Image.', use_column_width=True)
elif uploaded_file.type.startswith('video'):
tfile = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
tfile.write(uploaded_file.read())
tfile.close()
st.write(f'Temporary file path: {tfile.name}')
vidcap = cv2.VideoCapture(tfile.name)
stframe = st.empty()
while vidcap.isOpened():
success, frame = vidcap.read()
if not success:
break
results = detect_fall(frame)
processed_frame = np.squeeze(results.render())
stframe.image(processed_frame, channels="BGR")
vidcap.release()
os.remove(tfile.name)
elif option == "Use Camera":
stframe = st.empty()
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
results = detect_fall(frame)
processed_frame = np.squeeze(results.render())
stframe.image(processed_frame, channels="BGR")
cap.release()
if __name__ == '__main__':
main()
|