99import numpy as np
1010
1111from ns_vfs .data .frame import Frame , FrameWindow
12+ from ns_vfs .verification import build_label_func , build_trans_matrix
1213
1314
1415class VideoProcessor (abc .ABC ):
@@ -50,20 +51,22 @@ def _resize_frame(self, frame_img, frame_scale):
5051 ),
5152 )
5253
53- def get_synchronous_frame (
54+ def build_frame_synchronously (
5455 self ,
5556 proposition_set : list ,
5657 calculate_propositional_confidence : callable ,
58+ build_automaton : callable ,
5759 frame_duration_sec : int = 2 ,
5860 frame_scale : int | None = None ,
5961 sliding_window_size : int = 5 ,
62+ save_image : bool = False ,
6063 ):
6164 frame_window_idx = 0
6265 frame_counter = 0
6366 frame_idx = 0
6467 # Calculate the frame skip rate
6568 frame_window = dict (frame_window_idx = 0 )
66- frame_set = list ()
69+ temp_frame_set = list ()
6770
6871 while self ._cap .isOpened ():
6972 ret , frame_img = self ._cap .read ()
@@ -84,21 +87,50 @@ def get_synchronous_frame(
8487 )
8588 # Calculate propositional confidence
8689 for proposition in proposition_set :
90+ propositional_confidence = (
91+ calculate_propositional_confidence (
92+ proposition = proposition ,
93+ frame_img = frame_img ,
94+ is_annotation = save_image ,
95+ )
96+ )
8797 frame .propositional_probability [
8898 str (proposition )
89- ] = calculate_propositional_confidence (
90- proposition = proposition , frame_img = frame_img
91- )
92- # Build State
99+ ] = propositional_confidence
100+ temp_frame_set .append (frame )
101+
102+ if len (temp_frame_set ) == sliding_window_size :
103+ if save_image :
104+ self .replay_and_save (frames = temp_frame_set )
93105
94- frame_set . append ( frame )
95- if len ( frame_set ) == sliding_window_size :
106+ # Process frame window
107+ frame_set = temp_frame_set . copy ()
96108 frame_window [frame_window_idx ] = FrameWindow (
97109 frame_window_idx = frame_window_idx ,
98- frame_image_set = frame_set . copy () ,
110+ frame_image_set = frame_set ,
99111 )
112+
113+ propositional_confidence = frame_window [
114+ frame_window_idx
115+ ].get_propositional_confidence ()
116+
117+ # Build State & Compute Probability
118+ states , transitions = build_automaton (
119+ frame_set , propositional_confidence
120+ )
121+ frame_window [frame_window_idx ].states = states
122+ frame_window [frame_window_idx ].transitions = transitions
123+
124+ # Verification - Build Transition Matrix
125+ transition_matrix = build_trans_matrix (
126+ transitions = transitions , states = states
127+ )
128+ state_labeling = build_label_func (states , proposition_set )
129+ transition_matrix
130+ state_labeling
131+
100132 frame_window_idx += 1
101- frame_set .pop (0 )
133+ temp_frame_set .pop (0 )
102134
103135 frame_idx += 1
104136 frame_counter += 1
@@ -118,7 +150,7 @@ def get_frame_by_sliding_window(
118150 frame_idx = 0
119151 # Calculate the frame skip rate
120152 frame_window = dict (frame_window_idx = 0 )
121- frame_set = list ()
153+ temp_frame_set = list ()
122154
123155 while self ._cap .isOpened ():
124156 ret , frame_img = self ._cap .read ()
@@ -132,16 +164,16 @@ def get_frame_by_sliding_window(
132164 if frame_scale is not None :
133165 frame_img = self ._resize_frame (frame_img , frame_scale )
134166 Frame
135- frame_set .append (
167+ temp_frame_set .append (
136168 Frame (frame_index = frame_idx , frame_image = frame_img )
137169 )
138- if len (frame_set ) == sliding_window_size :
170+ if len (temp_frame_set ) == sliding_window_size :
139171 frame_window [frame_window_idx ] = FrameWindow (
140172 frame_window_idx = frame_window_idx ,
141- frame_image_set = frame_set .copy (),
173+ frame_image_set = temp_frame_set .copy (),
142174 )
143175 frame_window_idx += 1
144- frame_set .pop (0 )
176+ temp_frame_set .pop (0 )
145177
146178 frame_idx += 1
147179 frame_counter += 1
@@ -192,7 +224,7 @@ def get_frame(
192224
193225 def replay_and_save (
194226 self ,
195- frames : list [np . ndarray ],
227+ frames : list [Frame ],
196228 frame_rate = 2 ,
197229 is_imshow : bool = False ,
198230 output_dir : str | None = None ,
@@ -206,7 +238,7 @@ def replay_and_save(
206238 shutil .rmtree (output_dir )
207239 os .makedirs (output_dir )
208240 for idx , frame in enumerate (frames ):
209- plt .imshow (cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB ))
241+ plt .imshow (cv2 .cvtColor (frame . frame_image , cv2 .COLOR_BGR2RGB ))
210242 plt .axis ("off" ) # hide the axis values
211243 plt .savefig (os .path .join (output_dir , f"frame_{ idx } .png" ))
212244 if is_imshow :
0 commit comments