Spaces:
Sleeping
Sleeping
Update services/video_service.py
Browse files- services/video_service.py +22 -19
services/video_service.py
CHANGED
@@ -1,32 +1,35 @@
|
|
1 |
-
|
|
|
2 |
import os
|
|
|
3 |
|
4 |
-
VIDEO_DIR = "data" #
|
5 |
-
video_files = sorted(
|
6 |
-
os.path.join(VIDEO_DIR, f)
|
7 |
-
|
8 |
-
if f.lower().endswith((".mp4", ".avi", ".mov"))
|
9 |
-
])
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
def get_next_video_frame():
|
15 |
-
global
|
16 |
-
global
|
17 |
|
18 |
if not video_files:
|
19 |
-
raise
|
20 |
|
21 |
-
if
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
ret, frame =
|
25 |
|
26 |
if not ret:
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
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
|