Spaces:
Runtime error
Runtime error
Update services/video_service.py
Browse files- services/video_service.py +24 -43
services/video_service.py
CHANGED
@@ -2,60 +2,41 @@ import os
|
|
2 |
import random
|
3 |
import cv2
|
4 |
|
5 |
-
#
|
6 |
-
VIDEO_DIR = "sample_videos"
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
|
11 |
-
#
|
12 |
video_files = [
|
13 |
os.path.join(VIDEO_DIR, file)
|
14 |
for file in os.listdir(VIDEO_DIR)
|
15 |
-
if
|
16 |
]
|
17 |
|
18 |
if not video_files:
|
19 |
-
raise
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
frame_iterator = None
|
27 |
-
|
28 |
-
def open_new_video():
|
29 |
-
global current_video, frame_iterator
|
30 |
-
video_path = random_state.choice(video_files)
|
31 |
-
current_video = cv2.VideoCapture(video_path)
|
32 |
-
|
33 |
-
if not current_video.isOpened():
|
34 |
-
raise IOError(f"Cannot open video: {video_path}")
|
35 |
-
|
36 |
-
frame_iterator = iter_video_frames(current_video)
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
if not ret:
|
42 |
-
break
|
43 |
-
yield frame
|
44 |
|
45 |
-
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
global current_video, frame_iterator
|
51 |
-
|
52 |
-
if frame_iterator is None:
|
53 |
-
open_new_video()
|
54 |
-
|
55 |
-
try:
|
56 |
-
frame = next(frame_iterator)
|
57 |
-
return frame
|
58 |
-
except StopIteration:
|
59 |
-
open_new_video()
|
60 |
-
frame = next(frame_iterator)
|
61 |
-
return frame
|
|
|
2 |
import random
|
3 |
import cv2
|
4 |
|
5 |
+
VIDEO_DIR = "data" # Corrected to 'data' folder
|
|
|
6 |
|
7 |
+
# Ensure the folder exists
|
8 |
+
os.makedirs(VIDEO_DIR, exist_ok=True)
|
9 |
|
10 |
+
# List available video files
|
11 |
video_files = [
|
12 |
os.path.join(VIDEO_DIR, file)
|
13 |
for file in os.listdir(VIDEO_DIR)
|
14 |
+
if file.lower().endswith((".mp4", ".avi", ".mov"))
|
15 |
]
|
16 |
|
17 |
if not video_files:
|
18 |
+
raise FileNotFoundError(
|
19 |
+
f"No videos found in '{VIDEO_DIR}'. Please upload .mp4, .avi, or .mov videos."
|
20 |
+
)
|
21 |
|
22 |
+
current_video_idx = 0
|
23 |
+
video_capture = None
|
24 |
|
25 |
+
def get_random_video_frame():
|
26 |
+
global video_capture, current_video_idx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
if video_capture is None or not video_capture.isOpened():
|
29 |
+
selected_video = video_files[current_video_idx]
|
30 |
+
video_capture = cv2.VideoCapture(selected_video)
|
|
|
|
|
|
|
31 |
|
32 |
+
ret, frame = video_capture.read()
|
33 |
|
34 |
+
if not ret:
|
35 |
+
# Move to next video
|
36 |
+
current_video_idx = (current_video_idx + 1) % len(video_files)
|
37 |
+
selected_video = video_files[current_video_idx]
|
38 |
+
video_capture.release()
|
39 |
+
video_capture = cv2.VideoCapture(selected_video)
|
40 |
+
ret, frame = video_capture.read()
|
41 |
|
42 |
+
return frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|