Spaces:
Sleeping
Sleeping
Update services/video_service.py
Browse files- services/video_service.py +18 -3
services/video_service.py
CHANGED
@@ -29,9 +29,21 @@ def preload_video(video_path: str) -> None:
|
|
29 |
cap = cv2.VideoCapture(video_path)
|
30 |
if not cap.isOpened():
|
31 |
raise RuntimeError(f"Failed to open video: {video_path}. Ensure the file exists and is a supported format (.mp4, .avi).")
|
|
|
|
|
32 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
33 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
current_frame_idx = 0
|
36 |
except Exception as e:
|
37 |
logging.error(f"Error loading video {video_path}: {str(e)}")
|
@@ -51,10 +63,12 @@ def reset_video_index() -> None:
|
|
51 |
Reset the video frame index to the beginning.
|
52 |
"""
|
53 |
global current_frame_idx
|
54 |
-
if cap is not None:
|
55 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
56 |
current_frame_idx = 0
|
57 |
logging.info("Reset video to start.")
|
|
|
|
|
58 |
|
59 |
def get_next_video_frame() -> Optional[np.ndarray]:
|
60 |
"""
|
@@ -74,10 +88,11 @@ def get_next_video_frame() -> Optional[np.ndarray]:
|
|
74 |
success, frame = cap.read()
|
75 |
if not success or frame is None:
|
76 |
# Video ended, reset to start
|
|
|
77 |
reset_video_index()
|
78 |
success, frame = cap.read()
|
79 |
if not success or frame is None:
|
80 |
-
logging.warning("
|
81 |
return None
|
82 |
current_frame_idx = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
|
83 |
logging.info(f"Retrieved frame {current_frame_idx}")
|
|
|
29 |
cap = cv2.VideoCapture(video_path)
|
30 |
if not cap.isOpened():
|
31 |
raise RuntimeError(f"Failed to open video: {video_path}. Ensure the file exists and is a supported format (.mp4, .avi).")
|
32 |
+
|
33 |
+
# Validate video properties
|
34 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
35 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
36 |
+
if total_frames <= 0:
|
37 |
+
raise RuntimeError(f"Invalid video: {video_path}. No frames detected.")
|
38 |
+
if fps <= 0:
|
39 |
+
raise RuntimeError(f"Invalid video: {video_path}. Invalid FPS.")
|
40 |
+
|
41 |
+
# Check video codec
|
42 |
+
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
|
43 |
+
codec = "".join([chr((fourcc >> 8 * i) & 0xFF) for i in range(4)])
|
44 |
+
log_message = (f"Loaded video: {video_path}, Total Frames: {total_frames}, "
|
45 |
+
f"FPS: {fps}, Codec: {codec}")
|
46 |
+
logging.info(log_message)
|
47 |
current_frame_idx = 0
|
48 |
except Exception as e:
|
49 |
logging.error(f"Error loading video {video_path}: {str(e)}")
|
|
|
63 |
Reset the video frame index to the beginning.
|
64 |
"""
|
65 |
global current_frame_idx
|
66 |
+
if cap is not None and cap.isOpened():
|
67 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
68 |
current_frame_idx = 0
|
69 |
logging.info("Reset video to start.")
|
70 |
+
else:
|
71 |
+
logging.warning("Cannot reset video: Video capture not initialized.")
|
72 |
|
73 |
def get_next_video_frame() -> Optional[np.ndarray]:
|
74 |
"""
|
|
|
88 |
success, frame = cap.read()
|
89 |
if not success or frame is None:
|
90 |
# Video ended, reset to start
|
91 |
+
logging.info("Reached end of video, resetting to start.")
|
92 |
reset_video_index()
|
93 |
success, frame = cap.read()
|
94 |
if not success or frame is None:
|
95 |
+
logging.warning("Failed to retrieve frame after reset.")
|
96 |
return None
|
97 |
current_frame_idx = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
|
98 |
logging.info(f"Retrieved frame {current_frame_idx}")
|