Skip to content

Commit 218013f

Browse files
committed
feat: frame window to have states and transitions
1 parent 06a3822 commit 218013f

5 files changed

Lines changed: 150 additions & 140 deletions

File tree

main.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
video_path=sample_video_path,
2525
artifact_dir=config.VERSION_AND_PATH.ARTIFACTS_PATH,
2626
),
27-
proposition_set=["face", "baby"],
27+
proposition_set=["face", "person", "car"],
28+
artifact_dir=config.VERSION_AND_PATH.ARTIFACTS_PATH,
2829
)
29-
states, transitions, prev_states = frame2automaton.build_automaton(
30-
is_annotation=False
31-
)
32-
print(states)
33-
print(transitions)
30+
31+
frame_window_automata = frame2automaton.start()
32+
3433
print("Development is in progress.")

ns_vfs/data/frame.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,28 @@ class Frame:
1414
class FrameWindow:
1515
frame_window_idx: int
1616
frame_image_set: list = dataclasses.field(default_factory=list)
17+
states: list = dataclasses.field(default_factory=list)
18+
transitions: list = dataclasses.field(default_factory=list)
19+
20+
def get_propositional_confidence(self):
21+
propositional_confidence = [
22+
[]
23+
for i in range(
24+
len(self.frame_image_set[0].propositional_probability.keys())
25+
)
26+
]
27+
propositional_confidence
28+
for frame in self.frame_image_set:
29+
frame: Frame
30+
idx = 0
31+
for prop in frame.propositional_probability.keys():
32+
propositional_confidence[idx].append(
33+
frame.propositional_probability[prop]
34+
)
35+
idx += 1
36+
self.propositional_confidence = propositional_confidence
37+
return propositional_confidence
38+
39+
def update_states(self, states):
40+
self.states = states
41+
return states

ns_vfs/processor/video_processor.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010

1111
from ns_vfs.data.frame import Frame, FrameWindow
12+
from ns_vfs.verification import build_label_func, build_trans_matrix
1213

1314

1415
class 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:

ns_vfs/state.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@ def __init__(
66
self.state_index = index
77
self.frame_index = frame_index
88
self.proposition_set = proposition_set
9-
self.proposition_status_set = (
10-
proposition_status_set # TTT, TFT, FTT, etc.
9+
self.current_proposition_combination = (
10+
proposition_status_set # "initial", TTT, TFT, FTT, etc.
1111
)
12-
self.proposition_true_label_list = self._build_label_list(
12+
self.current_descriptive_label = self._get_descriptive_label(
1313
label=proposition_status_set
1414
)
1515
self.probability = 1
1616

1717
def __repr__(self):
18-
return f"{self.state_index} {self.proposition_true_label_list} {self.frame_index} {self.probability}"
18+
return f"{self.state_index} {self.current_descriptive_label} {self.frame_index} {self.probability}"
1919

2020
def __str__(self):
2121
return f"{self.__repr__()}"
2222

23-
def _build_label_list(self, label):
23+
def _get_descriptive_label(self, label):
2424
labels = []
2525
for i in range(len(self.proposition_set)):
2626
if label[i] == "T":
2727
labels.append(self.proposition_set[i])
2828
return labels
2929

30-
def update(self, frame_index, proposition_status_set):
30+
def update(self, frame_index, proposition_combinations):
3131
self.frame_index = frame_index
32-
self.proposition_status_set = (
33-
proposition_status_set # TTT, TFT, FTT, etc.
32+
self.current_proposition_combination = (
33+
proposition_combinations # TTT, TFT, FTT, etc.
3434
)
35-
self.proposition_true_label_list = self._build_label_list(
36-
label=proposition_status_set
35+
self.current_descriptive_label = self._get_descriptive_label(
36+
label=proposition_combinations
3737
)
3838
self.probability = 1
3939

@@ -46,8 +46,8 @@ def compute_probability(self, probabilities):
4646
-> [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]].
4747
"""
4848
probability = 1
49-
for i in range(len(self.proposition_status_set)):
50-
if self.proposition_status_set[i] == "T":
49+
for i in range(len(self.current_proposition_combination)):
50+
if self.current_proposition_combination[i] == "T":
5151
probability *= probabilities[i][self.frame_index]
5252
else:
5353
probability *= 1 - probabilities[i][self.frame_index]

0 commit comments

Comments
 (0)