Project

General

Profile

Files » video_processing.py

video processing file of the program - Pratama Kwee BRANDON, 11/12/2025 08:22 PM

 
1
import cv2
2

    
3
class VideoProcessing:
4
    """@brief A class for loading, reading, and managing video streams using OpenCV.
5

    
6
    @details
7
    This class provides basic functionality for working with video files:
8
    - Loading a video from a file path.
9
    - Reading frames sequentially.
10
    - Releasing system resources after use.
11

    
12
    It is designed for modular integration with image-processing workflows, 
13
    enabling real-time or frame-by-frame analysis from video inputs.
14
    """
15

    
16
    def __init__(self, video_path=None):
17
        """@brief Initialize the VideoProcessing object.
18

    
19
        @details
20
        Optionally takes a video file path at initialization. If provided, 
21
        the constructor automatically loads the video for reading.
22

    
23
        @param video_path (optional) The file path to the video file to load.
24
        """
25
        self.cap = None
26
        if video_path:
27
            self.load_video(video_path)
28

    
29
    def load_video(self, video_path):
30
        """@brief Load a video file for processing.
31

    
32
        @details
33
        Opens a video file using OpenCV’s `VideoCapture`. If another video is 
34
        already loaded, it is safely released before opening the new one.
35

    
36
        @param video_path The file path to the video file to load.
37

    
38
        @throws ValueError If the video cannot be opened successfully.
39
        """
40
        if self.cap is not None:
41
            self.cap.release()
42

    
43
        self.cap = cv2.VideoCapture(video_path)
44

    
45
        if not self.cap.isOpened():
46
            raise ValueError(f"Cannot open video file: {video_path}")
47

    
48
    def get_next_frame(self):
49
        """@brief Retrieve the next frame from the video stream.
50

    
51
        @details
52
        Reads the next available frame from the video. If the end of the video
53
        is reached or the video is not properly loaded, returns `None`.
54

    
55
        @return The next video frame as a NumPy array, or `None` if no frame is available.
56

    
57
        @throws RuntimeError If no video is loaded before calling this function.
58
        """
59
        if self.cap is None:
60
            raise RuntimeError("No video loaded. Call load_video() first.")
61

    
62
        ret, frame = self.cap.read()
63
        if not ret:
64
            return None  # End of video or error
65

    
66
        return frame
67

    
68
    def release(self):
69
        """@brief Release the video capture resource.
70

    
71
        @details
72
        Safely releases the OpenCV `VideoCapture` object to free up system resources.
73
        This should always be called when video processing is complete.
74
        """
75
        if self.cap is not None:
76
            self.cap.release()
77
            self.cap = None
(13-13/17)