Medvira commited on
Commit
88b4257
·
verified ·
1 Parent(s): 1fc1ad1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -78
app.py CHANGED
@@ -1,94 +1,42 @@
1
  import streamlit as st
2
  import os
3
- import cv2
4
- import numpy as np
5
- from ultralytics import YOLO
6
-
7
- # Load YOLO model
8
- model = YOLO('yolov8n.pt') # Ensure you have the correct model file
9
-
10
- def process_video(video_path):
11
- cap = cv2.VideoCapture(video_path)
12
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
13
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
14
- fps = int(cap.get(cv2.CAP_PROP_FPS))
15
-
16
- # Create a video writer to save the output
17
- output_path = os.path.join(os.getcwd(), "output.mp4")
18
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
19
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
20
-
21
- while cap.isOpened():
22
- ret, frame = cap.read()
23
- if not ret:
24
- break
25
-
26
- results = model(frame)
27
- for result in results:
28
- for bbox in result.boxes:
29
- x1, y1, x2, y2 = map(int, bbox.xyxy[0])
30
- confidence = float(bbox.conf)
31
- cls = int(bbox.cls)
32
-
33
- if cls == 0: # Assuming class 0 is 'person'
34
- w = x2 - x1
35
- h = y2 - y1
36
-
37
- if h < w:
38
- color = (0, 0, 255) # Red color for fall detected
39
- label = "Fall Detected"
40
- else:
41
- color = (0, 255, 0) # Green color for normal detection
42
- label = "Person"
43
-
44
- cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
45
- cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
46
-
47
- out.write(frame)
48
-
49
- cap.release()
50
- out.release()
51
-
52
- # Double-check that the file was saved
53
- if os.path.exists(output_path):
54
- st.success(f"Video successfully processed and saved to {output_path}")
55
- else:
56
- st.error("Failed to save the processed video.")
57
-
58
- return output_path
59
 
60
  # Streamlit interface
61
  st.title("Fall Detection App")
62
- st.write("Upload a video or choose from the default videos to detect falls.")
63
 
64
  # Create two columns
65
  left_column, right_column = st.columns(2)
66
 
67
  # Right column for video selection
68
  with right_column:
 
69
  default_videos = {
70
- "Video 1": os.path.join(os.getcwd(), "fall_test_01.mp4"),
71
- "Video 2": os.path.join(os.getcwd(), "fall_test_02.mp4"),
72
- "Video 3": "video3.mp4",
 
 
 
 
 
 
 
 
 
73
  }
74
 
 
75
  option = st.selectbox("Choose a video", list(default_videos.keys()))
76
- uploaded_video = st.file_uploader("Or upload your own video", type=["mp4", "avi", "mov"])
77
-
78
- if uploaded_video is not None:
79
- video_path = os.path.join(os.getcwd(), uploaded_video.name)
80
- with open(video_path, 'wb') as f:
81
- f.write(uploaded_video.getbuffer())
82
- st.success(f"Uploaded {uploaded_video.name}")
83
- else:
84
- video_path = default_videos[option]
85
 
86
- if st.button("Process Video"):
87
- output_video = process_video(video_path)
88
- if output_video and os.path.exists(output_video):
89
- left_column.video(output_video) # Display video in the left column
90
- left_column.write("Download the processed video:")
91
- with open(output_video, "rb") as video_file:
92
- left_column.download_button("Download", video_file, "output.mp4")
93
- else:
94
- st.error("There was an issue processing the video. Please try again.")
 
 
 
1
  import streamlit as st
2
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # Streamlit interface
5
  st.title("Fall Detection App")
6
+ st.write("Select a video to see the pre-processed fall detection results.")
7
 
8
  # Create two columns
9
  left_column, right_column = st.columns(2)
10
 
11
  # Right column for video selection
12
  with right_column:
13
+ # Define the paths for default videos and their corresponding pre-processed outputs
14
  default_videos = {
15
+ "Video 1": {
16
+ "input": os.path.join(os.getcwd(), "fall_test_01.mp4"),
17
+ "output": os.path.join(os.getcwd(), "processed_fall_test_01.mp4"),
18
+ },
19
+ "Video 2": {
20
+ "input": os.path.join(os.getcwd(), "fall_test_02.mp4"),
21
+ "output": os.path.join(os.getcwd(), "video_02_results.mp4"),
22
+ },
23
+ "Video 3": {
24
+ "input": os.path.join(os.getcwd(), "fall_test_03.mp4"),
25
+ "output": os.path.join(os.getcwd(), "processed_fall_test_03.mp4"),
26
+ },
27
  }
28
 
29
+ # User selects which video to view
30
  option = st.selectbox("Choose a video", list(default_videos.keys()))
 
 
 
 
 
 
 
 
 
31
 
32
+ # Load the corresponding pre-processed video path
33
+ output_video_path = default_videos[option]["output"]
34
+
35
+ # Left column to display the pre-processed video
36
+ if os.path.exists(output_video_path):
37
+ left_column.video(output_video_path) # Display the pre-processed video
38
+ left_column.write("Download the processed video:")
39
+ with open(output_video_path, "rb") as video_file:
40
+ left_column.download_button("Download", video_file, f"{option}_output.mp4")
41
+ else:
42
+ st.error("Processed video not found. Please check the paths and try again.")