Source code for models.model_video_control

"""
This model_video_control module manages the configuration and settings related to video processing in the MoilApp
"""
import time

import cv2


[docs] class ModelVideoConfig: """ Class responsible for configuring and managing video settings. Attributes: _model (object): The main model instance of the application. _fps_video (float): Frames per second of the video. _image (numpy.ndarray): The current frame of the video. _total_frame_video (float): Total number of frames in the video. _pos_frame_video (float): Current position of the frame in the video. _video_time (list): List containing total and recent time information of the video. Methods: record_video_pressed(): Configures video recording settings based on user input. calculate_frame_video(): Calculates video frame information such as FPS, total frames, and current position. """ def __init__(self, model): self._model = model self._fps_video = None self._image = None self._total_frame_video = None self._pos_frame_video = None self._video_time = [None, None, None, None] @property def fps_video(self): """ Getter for the FPS (frames per second) of the video. Return: float: The FPS of the video. """ try: return self._fps_video except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.fps_video(): Error: {str(e)}") @fps_video.setter def fps_video(self, value): """ Setter for the FPS (frames per second) of the video. Arg: value (float): The new value to set for the FPS of the video. """ try: self._fps_video = value except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.fps_video(): Error: {str(e)}") @property def total_frame_video(self): """ Getter for the total number of frames in the video. Return: int: The total number of frames in the video. """ try: return self._total_frame_video except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.total_frame_video(): Error: {str(e)}") @total_frame_video.setter def total_frame_video(self, value): """ Setter for the total number of frames in the video. Arg: value (int): The new value to set for the total number of frames in the video. """ try: self._total_frame_video = value except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.total_frame_video(): Error: {str(e)}") @property def pos_frame_video(self): """ Getter for the position of the current frame in the video. Return: int: The position of the current frame in the video. """ try: return self._pos_frame_video except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.pos_frame_video(): Error: {str(e)}") @pos_frame_video.setter def pos_frame_video(self, value): """ Setter for the position of the current frame in the video. Arg: value (int): The new value to set for the position of the current frame in the video. """ try: self._pos_frame_video = value except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.pos_frame_video(): Error: {str(e)}") @property def video_time(self): """ Getter for the total time duration of the video. Return: float: The total time duration of the video. """ try: return self._video_time except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.video_time(): Error: {str(e)}") @video_time.setter def video_time(self, value): """ Setter for the total time duration of the video. Arg: value (float): The new value to set for the total time duration of the video. """ try: self._video_time = value except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.video_time(): Error: {str(e)}")
[docs] def record_video_pressed(self, record_option): """ Configure video recording settings based on user input. Arg: record_option (str): The record option selected by the user. Return: cv2.VideoWriter: The video writer object for recording. """ try: if self._model.debug_mode: self._model.activity_logger.info(f"ModelVideoConfig: record_video_pressed(), " f"configuration record video when action pressed with args record option") fourcc = cv2.VideoWriter_fourcc(*'XVID') filename = f"../saved_image/recorded_video_{record_option}_{self._model.mode_view}_{time.strftime('%Y%m%d_%H%M%S')}.avi" fps_record = self.fps_video or 16 # Default to 20 if fps_record is None if self._model.image_original is not None and record_option == "Original": h, w = self._image.shape[:2] video_writer = cv2.VideoWriter(filename, fourcc, round(fps_record), (w, h)) else: h, w = self._image.shape[:2] video_writer = cv2.VideoWriter(filename, fourcc, round(fps_record), (w, h)) return video_writer, filename except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.record_video_pressed(): Error: {str(e)}")
# def calculate_fps_streaming_camera(self): # num_frames = 120 # start_time = time.time() # frame_count = 0 # while frame_count < num_frames: # ret, frame = self._model.cap.read() # if not ret: # print("Error: Couldn't read frame.") # break # frame_count += 1 # # end_time = time.time() # elapsed_time = end_time - start_time # return frame_count / elapsed_time
[docs] def calculate_frame_video(self, video_source): """ Calculate video frame information such as FPS, total frames, and current position. Arg: video_source: The video source. """ try: if self._model.debug_mode: self._model.activity_logger.info("ModelVideoConfig: calculate_frame_video(), " "Calculate frame video to get video time") success, self._image = video_source.read() if success: self.fps_video = video_source.get(cv2.CAP_PROP_FPS) if self.fps_video != 0.0: self.pos_frame_video = video_source.get(cv2.CAP_PROP_POS_FRAMES) self.total_frame_video = float(video_source.get(cv2.CAP_PROP_FRAME_COUNT)) duration_sec = int(self._total_frame_video / self.fps_video) total_minutes = duration_sec // 60 duration_sec %= 60 total_seconds = duration_sec sec_pos = int(self._pos_frame_video / self.fps_video) recent_minute = int(sec_pos // 60) sec_pos %= 60 recent_sec = sec_pos self.video_time = [total_minutes, total_seconds, recent_minute, recent_sec] else: self.pos_frame_video = None self.total_frame_video = None self.fps_video = None self.video_time = [None, None, None, None] except Exception as e: self._model.activity_logger.error(f"ModelVideoConfig.calculate_frame_video(): Error: {str(e)}")