ducdatit2002 commited on
Commit
74ec428
·
verified ·
1 Parent(s): b67ff7f

Upload 3 files

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