surveillance / services /video_service.py
SuriRaja's picture
Update services/video_service.py
8c86000
raw
history blame
1.13 kB
import os
import random
import cv2
VIDEO_DIR = "data" # Corrected to 'data' folder
# Ensure the folder exists
os.makedirs(VIDEO_DIR, exist_ok=True)
# List available video files
video_files = [
os.path.join(VIDEO_DIR, file)
for file in os.listdir(VIDEO_DIR)
if file.lower().endswith((".mp4", ".avi", ".mov"))
]
if not video_files:
raise FileNotFoundError(
f"No videos found in '{VIDEO_DIR}'. Please upload .mp4, .avi, or .mov videos."
)
current_video_idx = 0
video_capture = None
def get_random_video_frame():
global video_capture, current_video_idx
if video_capture is None or not video_capture.isOpened():
selected_video = video_files[current_video_idx]
video_capture = cv2.VideoCapture(selected_video)
ret, frame = video_capture.read()
if not ret:
# Move to next video
current_video_idx = (current_video_idx + 1) % len(video_files)
selected_video = video_files[current_video_idx]
video_capture.release()
video_capture = cv2.VideoCapture(selected_video)
ret, frame = video_capture.read()
return frame