-
Notifications
You must be signed in to change notification settings - Fork 0
video_analyzer
The VideoAnalyzer class provides tools for loading, analyzing, and processing video files. It supports frame-by-frame analysis using an ImageAnalyzer, saving individual frames, creating timelapse videos, and extracting biological information from video sequences.
class VideoAnalyzer:
def __init__(self):
"""Initialize the VideoAnalyzer."""
...| Attribute | Type | Description |
|---|---|---|
video |
cv2.VideoCapture |
Video capture object. |
current_frame |
np.ndarray |
Current frame as a NumPy array. |
frame_count |
int |
Total number of frames in the video. |
fps |
int |
Frames per second of the video. |
image_analyzer |
ImageAnalyzer |
ImageAnalyzer instance for image processing. |
-
__init__(self)Initializes a newVideoAnalyzerinstance.
-
load_video(self, video_path)Loads a video file.-
Parameters:
-
video_path: Path to the video file.
-
-
Parameters:
-
get_frame(self, frame_number=None) -> np.ndarrayRetrieves a specific frame or the next frame if no frame number is specified.-
Parameters:
-
frame_number: Optional frame number to retrieve.
-
-
Returns: The frame as a NumPy array.
-
-
save_frame(self, output_path, frame=None)Saves the current frame or a specific frame as an image.-
Parameters:
-
output_path: Path to save the image. -
frame: Optional frame to save (if not provided, saves the current frame).
-
-
Parameters:
-
analyze_frame(self, frame=None) -> Dict[str, Any]Analyzes the current frame or a specific frame usingImageAnalyzer.-
Parameters:
-
frame: Optional frame to analyze (if not provided, analyzes the current frame).
-
-
Returns: Analysis results, including cells, nuclei, and mitochondria.
-
-
analyze_video(self, frame_interval=1) -> List[Dict[str, Any]]Analyzes the entire video at specified frame intervals.-
Parameters:
-
frame_interval: Interval between frames to analyze.
-
-
Returns: List of analysis results for each analyzed frame.
-
-
create_timelapse(self, output_path, start_frame, end_frame, interval=1, resize_factor=1.0)Creates a timelapse video from a range of frames.-
Parameters:
-
output_path: Path to save the timelapse video. -
start_frame: Starting frame number. -
end_frame: Ending frame number. -
interval: Number of frames to skip between each frame in the timelapse. -
resize_factor: Factor by which to resize frames (1.0 means no resizing).
-
-
Parameters:
-
close(self)Releases the video capture object.
# Initialize VideoAnalyzer
video_analyzer = VideoAnalyzer()
# Load a video file
video_analyzer.load_video("path/to/video.mp4")
# Get a specific frame
frame = video_analyzer.get_frame(100)
print(f"Retrieved frame shape: {frame.shape}")
# Save the current frame
video_analyzer.save_frame("path/to/frame_100.png")
# Analyze a frame
analysis_results = video_analyzer.analyze_frame()
print(f"Analysis results: {analysis_results}")
# Analyze the entire video
video_analysis = video_analyzer.analyze_video(frame_interval=5)
print(f"Video analysis results: {video_analysis}")
# Create a timelapse video
video_analyzer.create_timelapse(
"path/to/timelapse.mp4",
start_frame=0,
end_frame=100,
interval=2,
resize_factor=0.5
)
# Close the video capture
video_analyzer.close()-
cv2(OpenCV): For video processing, frame retrieval, and video writing. -
biobridge.tools.image_analyzer.ImageAnalyzer: For image analysis.
- The class includes checks for valid input data and handles potential errors during video processing and frame analysis.
- The
VideoAnalyzerclass is designed for analyzing video sequences, particularly those depicting biological processes. - It supports frame-by-frame analysis using an
ImageAnalyzerto extract biological information such as cells, nuclei, and mitochondria. - The class provides methods for saving individual frames and creating timelapse videos.
- The
analyze_framemethod uses anImageAnalyzerto perform detailed analysis of each frame. - The
analyze_videomethod allows for analyzing the entire video at specified frame intervals.