SuriRaja commited on
Commit
ae09838
·
1 Parent(s): 86ed826

Update services/video_service.py

Browse files
Files changed (1) hide show
  1. services/video_service.py +22 -19
services/video_service.py CHANGED
@@ -1,32 +1,35 @@
1
- import cv2
 
2
  import os
 
3
 
4
- VIDEO_DIR = "data" # All your videos are stored under 'data/' folder
5
- video_files = sorted([
6
- os.path.join(VIDEO_DIR, f)
7
- for f in os.listdir(VIDEO_DIR)
8
- if f.lower().endswith((".mp4", ".avi", ".mov"))
9
- ])
10
 
11
- video_index = 0
12
- cap = None
13
 
14
  def get_next_video_frame():
15
- global cap
16
- global video_index
17
 
18
  if not video_files:
19
- raise RuntimeError("No videos found in the data directory.")
20
 
21
- if cap is None or not cap.isOpened():
22
- cap = cv2.VideoCapture(video_files[video_index])
 
 
23
 
24
- ret, frame = cap.read()
25
 
26
  if not ret:
27
- cap.release()
28
- video_index = (video_index + 1) % len(video_files)
29
- cap = cv2.VideoCapture(video_files[video_index])
30
- ret, frame = cap.read()
 
31
 
32
  return frame
 
1
+ # services/video_service.py
2
+
3
  import os
4
+ import cv2
5
 
6
+ VIDEO_DIR = "data" # Your videos are in /data
7
+ video_files = sorted(
8
+ [os.path.join(VIDEO_DIR, f) for f in os.listdir(VIDEO_DIR) if f.lower().endswith(('.mp4', '.avi', '.mov'))]
9
+ )
 
 
10
 
11
+ current_video_idx = 0
12
+ video_capture = None
13
 
14
  def get_next_video_frame():
15
+ global current_video_idx
16
+ global video_capture
17
 
18
  if not video_files:
19
+ raise Exception("No video files found in 'data' directory!")
20
 
21
+ if video_capture is None:
22
+ if current_video_idx >= len(video_files):
23
+ current_video_idx = 0 # restart from beginning
24
+ video_capture = cv2.VideoCapture(video_files[current_video_idx])
25
 
26
+ ret, frame = video_capture.read()
27
 
28
  if not ret:
29
+ # End of current video, move to next
30
+ video_capture.release()
31
+ video_capture = None
32
+ current_video_idx += 1
33
+ return get_next_video_frame() # recursively call
34
 
35
  return frame