Spaces:
Runtime error
Runtime error
kuldipparaliya
commited on
Upload 7 files
Browse files- app.py +92 -0
- assets/best.pt +3 -0
- requirement.txt +7 -0
- utils/__pycache__/yolo_processor.cpython-312.pyc +0 -0
- utils/__pycache__/yolo_processor.cpython-38.pyc +0 -0
- utils/video_player.py +17 -0
- utils/yolo_processor.py +36 -0
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
from utils.yolo_processor import YOLOProcessor
|
5 |
+
import tempfile
|
6 |
+
import numpy as np
|
7 |
+
import base64
|
8 |
+
|
9 |
+
|
10 |
+
processed_image = None
|
11 |
+
processed_video_path = None
|
12 |
+
|
13 |
+
def detect_fall(image, model_path):
|
14 |
+
model = YOLOProcessor(model_path)
|
15 |
+
result_image = model.detect_fall(image)
|
16 |
+
return result_image
|
17 |
+
|
18 |
+
def main():
|
19 |
+
global processed_image, processed_video_path
|
20 |
+
|
21 |
+
st.title("Fall Detection with YOLO")
|
22 |
+
st.markdown("---")
|
23 |
+
option = st.sidebar.selectbox("Choose an option", ["Image", "Video"])
|
24 |
+
|
25 |
+
if option == "Image":
|
26 |
+
st.subheader("Upload Image")
|
27 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
28 |
+
|
29 |
+
if uploaded_file is not None:
|
30 |
+
image = Image.open(uploaded_file)
|
31 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
32 |
+
st.markdown("---")
|
33 |
+
st.subheader("Detecting Fall...")
|
34 |
+
|
35 |
+
if processed_image is None: # Process the image only if it hasn't been processed before
|
36 |
+
with st.spinner('Detecting fall...'):
|
37 |
+
processed_image = detect_fall(image, "assets/best.pt")
|
38 |
+
st.image(processed_image, caption='Result', use_column_width=True)
|
39 |
+
|
40 |
+
# Download button for the result image
|
41 |
+
if st.button('Download Result Image'):
|
42 |
+
download_image(processed_image, filename='result_image.png')
|
43 |
+
|
44 |
+
elif option == "Video":
|
45 |
+
st.subheader("Upload Video")
|
46 |
+
uploaded_file = st.file_uploader("Choose a video", type=["mp4"])
|
47 |
+
|
48 |
+
if uploaded_file is not None:
|
49 |
+
st.markdown("---")
|
50 |
+
st.subheader("Processing and Detecting Fall...")
|
51 |
+
|
52 |
+
temp_dir = tempfile.TemporaryDirectory()
|
53 |
+
temp_file_path = os.path.join(temp_dir.name, "uploaded_video.mp4")
|
54 |
+
with open(temp_file_path, "wb") as f:
|
55 |
+
f.write(uploaded_file.read())
|
56 |
+
|
57 |
+
output_path = os.path.join(temp_dir.name, "processed_video.mp4")
|
58 |
+
|
59 |
+
if processed_video_path is None:
|
60 |
+
with st.spinner('Processing and detecting fall...'):
|
61 |
+
yolo_processor = YOLOProcessor("assets/best.pt")
|
62 |
+
yolo_processor.process_video(temp_file_path, output_path)
|
63 |
+
processed_video_path = output_path
|
64 |
+
|
65 |
+
st.subheader("Result Video")
|
66 |
+
st.video(processed_video_path)
|
67 |
+
|
68 |
+
if st.button('Download Result Video'):
|
69 |
+
download_file(processed_video_path, filename='processed_video.mp4')
|
70 |
+
|
71 |
+
temp_dir.cleanup()
|
72 |
+
|
73 |
+
def download_image(image, filename):
|
74 |
+
if isinstance(image, np.ndarray):
|
75 |
+
image = Image.fromarray(image)
|
76 |
+
image.save(filename)
|
77 |
+
with open(filename, "rb") as f:
|
78 |
+
image_bytes = f.read()
|
79 |
+
b64 = base64.b64encode(image_bytes).decode()
|
80 |
+
href = f'<a href="data:image/png;base64,{b64}" download="{filename}">Click here to download {filename}</a>'
|
81 |
+
st.markdown(href, unsafe_allow_html=True)
|
82 |
+
|
83 |
+
def download_file(file_path, filename):
|
84 |
+
with open(file_path, 'rb') as f:
|
85 |
+
data = f.read()
|
86 |
+
b64 = base64.b64encode(data).decode()
|
87 |
+
href = f'<a href="data:file/mp4;base64,{b64}" download="{filename}">Click here to download {filename}</a>'
|
88 |
+
st.markdown(href, unsafe_allow_html=True)
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
main()
|
92 |
+
|
assets/best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:80114db2b061860c59475b7b6d56087f59b3fb42e332583c9966876a5c613003
|
3 |
+
size 52017281
|
requirement.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
numpy
|
3 |
+
ultralytics
|
4 |
+
streamlit
|
5 |
+
opencv-python
|
6 |
+
torch
|
7 |
+
torch-vision
|
utils/__pycache__/yolo_processor.cpython-312.pyc
ADDED
Binary file (2.42 kB). View file
|
|
utils/__pycache__/yolo_processor.cpython-38.pyc
ADDED
Binary file (1.48 kB). View file
|
|
utils/video_player.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
|
3 |
+
class VideoPlayer:
|
4 |
+
def play_video(self, video_path):
|
5 |
+
cap = cv2.VideoCapture(video_path)
|
6 |
+
|
7 |
+
while cap.isOpened():
|
8 |
+
ret, frame = cap.read()
|
9 |
+
if ret:
|
10 |
+
cv2.imshow('Video', frame)
|
11 |
+
if cv2.waitKey(60) & 0xFF == ord('q'):
|
12 |
+
break
|
13 |
+
else:
|
14 |
+
break
|
15 |
+
|
16 |
+
cap.release()
|
17 |
+
cv2.destroyAllWindows()
|
utils/yolo_processor.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import streamlit as st
|
3 |
+
from ultralytics import YOLO
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
|
7 |
+
class YOLOProcessor:
|
8 |
+
def __init__(self, model_path):
|
9 |
+
self.model = YOLO(model_path)
|
10 |
+
|
11 |
+
def detect_fall(self, image):
|
12 |
+
result = self.model.predict(image, conf=0.5)
|
13 |
+
result_image = result[0].plot()
|
14 |
+
result_image = cv2.cvtColor(result_image,cv2.COLOR_BGR2RGB)
|
15 |
+
return result_image
|
16 |
+
|
17 |
+
def process_video(self, input_path, output_path):
|
18 |
+
vid = cv2.VideoCapture(input_path)
|
19 |
+
width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
|
20 |
+
height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
21 |
+
fps = int(vid.get(cv2.CAP_PROP_FPS))
|
22 |
+
output = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
23 |
+
|
24 |
+
while vid.isOpened():
|
25 |
+
ret, frame = vid.read()
|
26 |
+
|
27 |
+
if ret:
|
28 |
+
result = self.model.predict(frame, conf=0.5)
|
29 |
+
processed_frame = result[0].plot()
|
30 |
+
output.write(processed_frame)
|
31 |
+
else:
|
32 |
+
break
|
33 |
+
|
34 |
+
vid.release()
|
35 |
+
output.release()
|
36 |
+
|