Spaces:
Sleeping
Sleeping
Update services/video_service.py
Browse files- services/video_service.py +67 -48
services/video_service.py
CHANGED
|
@@ -1,68 +1,87 @@
|
|
| 1 |
import cv2
|
| 2 |
-
import
|
| 3 |
import logging
|
| 4 |
-
from typing import List, Dict, Any
|
| 5 |
|
| 6 |
-
#
|
| 7 |
logging.basicConfig(
|
| 8 |
filename="app.log",
|
| 9 |
level=logging.INFO,
|
| 10 |
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
Args:
|
| 18 |
-
|
| 19 |
-
detected_items (List[Dict[str, Any]]): List of detected items with coordinates and labels.
|
| 20 |
-
|
| 21 |
Returns:
|
| 22 |
-
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
if not isinstance(frame, np.ndarray) or frame.size == 0:
|
| 26 |
-
logging.error("Invalid input frame provided to overlay_service.")
|
| 27 |
-
return frame
|
| 28 |
-
if not isinstance(detected_items, list):
|
| 29 |
-
logging.error("Detected items must be a list.")
|
| 30 |
-
return frame
|
| 31 |
-
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
return frame
|
| 66 |
except Exception as e:
|
| 67 |
-
logging.error(f"Error
|
| 68 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import os
|
| 3 |
import logging
|
|
|
|
| 4 |
|
| 5 |
+
# Setup logging
|
| 6 |
logging.basicConfig(
|
| 7 |
filename="app.log",
|
| 8 |
level=logging.INFO,
|
| 9 |
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# Global video capture object
|
| 13 |
+
video_cap = None
|
| 14 |
+
current_frame_index = 0
|
| 15 |
|
| 16 |
+
def preload_video(video_path):
|
| 17 |
+
"""
|
| 18 |
+
Preload the video file.
|
| 19 |
Args:
|
| 20 |
+
video_path: Path to the video file
|
|
|
|
|
|
|
| 21 |
Returns:
|
| 22 |
+
str: Status message
|
| 23 |
"""
|
| 24 |
+
global video_cap, current_frame_index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
+
if not os.path.exists(video_path):
|
| 27 |
+
raise FileNotFoundError(f"Video file not found at {video_path}")
|
| 28 |
+
|
| 29 |
+
video_cap = cv2.VideoCapture(video_path)
|
| 30 |
+
if not video_cap.isOpened():
|
| 31 |
+
raise RuntimeError("Failed to open video file.")
|
| 32 |
|
| 33 |
+
current_frame_index = 0
|
| 34 |
+
total_frames = int(video_cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 35 |
+
logging.info(f"Preloaded video: {video_path} with {total_frames} frames.")
|
| 36 |
+
return f"Successfully loaded video: {video_path} ({total_frames} frames)"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
logging.error(f"Error preloading video: {str(e)}")
|
| 39 |
+
return f"Error loading video: {str(e)}"
|
| 40 |
|
| 41 |
+
def get_next_video_frame():
|
| 42 |
+
"""
|
| 43 |
+
Get the next frame from the video.
|
| 44 |
+
Returns:
|
| 45 |
+
numpy array: Frame if successful, None otherwise
|
| 46 |
+
"""
|
| 47 |
+
global video_cap, current_frame_index
|
| 48 |
+
if video_cap is None or not video_cap.isOpened():
|
| 49 |
+
logging.error("Video capture not initialized.")
|
| 50 |
+
return None
|
| 51 |
|
| 52 |
+
try:
|
| 53 |
+
# Set frame position
|
| 54 |
+
video_cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame_index)
|
| 55 |
+
ret, frame = video_cap.read()
|
| 56 |
+
if not ret:
|
| 57 |
+
# Loop back to the start
|
| 58 |
+
current_frame_index = 0
|
| 59 |
+
video_cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame_index)
|
| 60 |
+
ret, frame = video_cap.read()
|
| 61 |
+
if not ret:
|
| 62 |
+
raise RuntimeError("Failed to read frame from video.")
|
| 63 |
|
| 64 |
+
current_frame_index += 1
|
| 65 |
+
logging.debug(f"Retrieved frame {current_frame_index} from video.")
|
| 66 |
return frame
|
| 67 |
except Exception as e:
|
| 68 |
+
logging.error(f"Error retrieving frame: {str(e)}")
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def reset_video_index():
|
| 72 |
+
"""
|
| 73 |
+
Reset the frame index to the beginning.
|
| 74 |
+
"""
|
| 75 |
+
global current_frame_index
|
| 76 |
+
current_frame_index = 0
|
| 77 |
+
logging.info("Reset video frame index to 0.")
|
| 78 |
+
|
| 79 |
+
def release_video():
|
| 80 |
+
"""
|
| 81 |
+
Release the video capture object.
|
| 82 |
+
"""
|
| 83 |
+
global video_cap
|
| 84 |
+
if video_cap is not None:
|
| 85 |
+
video_cap.release()
|
| 86 |
+
video_cap = None
|
| 87 |
+
logging.info("Released video capture object.")
|