SuriRaja commited on
Commit
aa9753b
·
1 Parent(s): 6457f56

Update services/video_service.py

Browse files
Files changed (1) hide show
  1. services/video_service.py +14 -15
services/video_service.py CHANGED
@@ -4,26 +4,25 @@ import cv2
4
  import random
5
  import os
6
 
7
- VIDEO_FOLDER = "data/"
8
- VIDEO_FILES = [
9
- "drone_day.mp4",
10
- "night_intrusion.mp4",
11
- "thermal_hotspot.mp4",
12
- "shadow_dust_issue.mp4",
13
- "alert_response.mp4",
14
- ]
15
 
16
- def get_video_frame():
17
- """Randomly pick a video and yield frames one by one."""
18
- video_path = os.path.join(VIDEO_FOLDER, random.choice(VIDEO_FILES))
19
- cap = cv2.VideoCapture(video_path)
20
 
 
 
 
 
 
 
 
 
21
  while cap.isOpened():
22
  ret, frame = cap.read()
23
  if not ret:
 
24
  cap.release()
25
- # Once finished, pick another random video and continue
26
- video_path = os.path.join(VIDEO_FOLDER, random.choice(VIDEO_FILES))
27
  cap = cv2.VideoCapture(video_path)
28
  continue
29
- yield frame
 
4
  import random
5
  import os
6
 
7
+ VIDEO_FOLDER = "data"
 
 
 
 
 
 
 
8
 
9
+ def list_video_files():
10
+ """List all video files inside the data/ folder."""
11
+ return [os.path.join(VIDEO_FOLDER, f) for f in os.listdir(VIDEO_FOLDER) if f.endswith(".mp4")]
 
12
 
13
+ def get_random_video_frame():
14
+ """Randomly pick a video from the folder and stream frames from it."""
15
+ video_files = list_video_files()
16
+ if not video_files:
17
+ raise FileNotFoundError("No video files found in the 'data/' folder.")
18
+
19
+ video_path = random.choice(video_files)
20
+ cap = cv2.VideoCapture(video_path)
21
  while cap.isOpened():
22
  ret, frame = cap.read()
23
  if not ret:
24
+ # Restart when video ends
25
  cap.release()
 
 
26
  cap = cv2.VideoCapture(video_path)
27
  continue
28
+ yield frame, os.path.basename(video_path)