Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def select_video_file():
# Display options
extensions_list = ', '.join(video_extensions)
print(Fore.CYAN + f"\nEnter 'u' to upload a video ({extensions_list})")
print("Enter 'l' for live video input")
print("Enter 'q' to quit")
print("Or select an uploaded video below:\n")

Expand All @@ -100,13 +101,19 @@ def select_video_file():
print(Fore.YELLOW + "No video files found in the 'media' folder. Uploading files can be selected using 'u'.\n")

# Get user input
user_input = input(Fore.CYAN + "\nEnter 'u' to upload, 'q' to quit, or a number to select a file: ")
#user_input = input(Fore.CYAN + "\nEnter 'u' to upload, 'q' to quit, or a number to select a file: ")
user_input = input(Fore.GREEN + "Your choice: ").strip().lower()


# Handle quit
if user_input.lower() == 'q':
print(Fore.GREEN + "\nExiting program.\n")
exit()

elif user_input == 'l':
print(Fore.GREEN + "Live video input selected.")
return 'live'

# Handle upload option
elif user_input.lower() == 'u':
# Use show_file_picker() to get the selected files
Expand Down Expand Up @@ -141,4 +148,45 @@ def select_video_file():

# Invalid input handling
else:
print(Fore.RED + "Invalid input. Please enter 'u', 'q', or a number corresponding to a video file.")
print(Fore.RED + "Invalid input. Please enter 'u', 'q', or a number corresponding to a video file.")

def process_video(video_file, model, pipe):
if video_file == 'live':
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# Process the frame with the model and pipeline
process_frame(frame, model, pipe)
cv2.imshow('Live Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
else:
# Existing code to process video file
pass
def process_frame(frame, model, pipe):
# Implement frame processing logic here
pass

if __name__ == "__main__":
# Initialize the video classification pipeline
pipe = initialize_pipeline()

# Load the YOLOv8 model
model = YOLO(YOLO_MODEL_NAME)
model(verbose=False)[0]

# Select the video file to process
video_file = select_video_file()

# Process the selected video file
process_video(video_file, model, pipe)

# Analyze the fall segments
analyze_fall_segments(pipe)

# Clean up data directory on exit
clear_temp_segments(data_dir)
41 changes: 38 additions & 3 deletions utils/video_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Setup
init(autoreset=True) # Initialize colorama


# Import config
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils.config import data_dir, FALL_MODEL_NAME
Expand All @@ -22,9 +23,20 @@ def initialize_pipeline():

def process_video(video_file, model, pipe):
"""Creates clips of possible falls in the video and saves them in the temp_segments directory."""
message_printed = False
if video_file == 'live':
cap = cv2.VideoCapture(0)
print(Fore.GREEN + "Using live video input from webcam.")
else:
cap = cv2.VideoCapture(video_file)
print(Fore.GREEN + f"Using video file: {video_file}")

if not cap.isOpened():
print(Fore.RED + "Error: Could not open video source.")
return

# Let's make checkpoints in the video when we think there might be a fall
cap = cv2.VideoCapture(video_file)
##cap = cv2.VideoCapture(video_file)
count = 0
frame_buffer = collections.deque(maxlen=30) # Buffer to store frames
fall_detected = False
Expand Down Expand Up @@ -96,8 +108,20 @@ def process_video(video_file, model, pipe):
video_writer.release()
video_writer = None

cv2.imshow("RGB", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
if video_file == 'live':
cv2.imshow("Live Video", frame)
if message_printed == False:
print(Fore.GREEN + "Press 'q' to stop video capture.")
message_printed = True

else:
cv2.imshow("RGB", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break

# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
print(Fore.GREEN + "Stopping video capture.")
break

# Release resources when video processing is complete
Expand Down Expand Up @@ -134,6 +158,17 @@ def analyze_fall_segments(pipe):
for pred in result:
if 'fall' in pred['label'].lower():
print(Fore.GREEN + f"{fall_count} FALL DETECTED! Label: {pred['label']}, Confidence: {pred['score']}")
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
cv2.putText(frame, f"{fall_count} FALL DETECTED! Label: {pred['label']}, Confidence: {pred['score']}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow("Fall Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

# Print the total number of falls detected
print(f"Total number of falls detected: {fall_count}")