Skip to content

Commit 602d408

Browse files
committed
Improved main body.
1 parent 7e6518f commit 602d408

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

PostProcessing/PostProcessVideos.py

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pandas as pd
88
import cv2
9+
import re
910

1011
from dataframes import compute_time_range, trim_into_interval, repair_dropped_frames
1112

@@ -66,28 +67,38 @@ def extract(input_dir, output_dir):
6667
cap.release()
6768

6869

69-
70+
#
71+
#
72+
#
7073
def main(input_dir: Path, output_dir: Path):
7174

7275
# input_dir = Path("/Users/tbc/Desktop/videos/")
7376
# output_dir = Path("/Users/tbc/Desktop/output_videos/")
7477

75-
#
76-
# Find all CSV files in the directory and read it into a data frame (DONE)
78+
print(f"Scanning dir {str(input_dir)}...")
7779

7880
#
81+
# Find all CSV files in the directory and read it into a data frame
82+
clientIDpattern = "[\\da-f]" * 16
83+
patt = re.compile("^" + clientIDpattern + "$")
84+
7985
clientIDs: List[str] = []
8086
for p in input_dir.iterdir():
81-
print("Found client -->", p.stem)
82-
# TODO -- we could also check if the ClientID complies to the numerical format (using regex).
83-
clientIDs.append(p.stem)
87+
res = patt.match(p.stem)
88+
if res:
89+
print("Found client -->", p.stem)
90+
# TODO -- we could also check if the ClientID complies to the numerical format (using regex).
91+
clientIDs.append(p.stem)
92+
else:
93+
print("Discarding ", p.stem)
8494

85-
# Will be filled with key=clientID:str, data=Tuple[csv:DataFrame, videofile:str]
86-
clients_data: Dict[str, Tuple[pd.DataFrame, str]] = dict()
95+
n_clients = len(clientIDs)
8796

88-
df_list = []
97+
# Will accumulate the list of dataframes and mp4 files in the same order of the client IDs.
98+
df_list: List[pd.DataFrame] = []
99+
mp4_list: List[str] = []
89100

90-
for cID in clientIDs[1:]:
101+
for cID in clientIDs:
91102
client_dir = input_dir / cID
92103
CSVs = list(client_dir.glob("*.csv"))
93104
MP4s = list(client_dir.glob("*.mp4"))
@@ -104,40 +115,54 @@ def main(input_dir: Path, output_dir: Path):
104115

105116
df: pd.DataFrame = pd.read_csv(csv_file, header=None)
106117

107-
clients_data[cID] = (df, str(mp4_file))
108118
df_list.append(df)
119+
mp4_list.append(str(mp4_file))
109120

110-
# Define the path to the directory containing the CSV files
111-
# csv_path = "/Users/tbc/Desktop/test_data/"
121+
#
122+
# Print collected info
123+
for i in range(n_clients):
124+
cID = clientIDs[i]
125+
df = df_list[i]
126+
mp4 = mp4_list[i]
127+
print(f"For client ID {cID}: {len(df)} frames for file {mp4}")
112128

113129
#
114130
# Repair CSVs (TODO - Mina)
115-
# repaired_client_data = dict()
116-
# for cID, (df, mp4) in clients_data:
117-
# repaired_df = repair_dropped_frames(df)
118-
# repaired_client_data[cID] = repaired_df, mp4
131+
repaired_df_list: List[pd.DataFrame] = []
132+
for cID, df in zip(clientIDs, df_list):
133+
repaired_df = repair_dropped_frames(df)
134+
repaired_df_list.append(repaired_df)
119135

136+
assert len(clientIDs) == len(df_list) == len(mp4_list) == len(repaired_df_list)
120137

121138
#
122139
# Find time ranges (Saurabh, To test better)
123140
# Compute the time range
124-
#dfs = [df for k, (df, _) in clients_data]
125-
min_common, max_common = compute_time_range(df_list)
141+
min_common, max_common = compute_time_range(repaired_df_list)
126142

127143
#
128144
# Trim CSVs (TODO)
129145
# Trim the data frames to the time range and save to new CSV files
130-
csv_path = output_dir / "test"
131146
# TODO -- actually, we don't need to save them. We could just return them as DataFrame instances
132-
trimmed_dataframes = trim_into_interval(df_list, min_common, max_common, THRESHOLD_NS)
133-
147+
trimmed_dataframes = trim_into_interval(repaired_df_list, min_common, max_common, THRESHOLD_NS)
148+
149+
assert len(clientIDs) == len(trimmed_dataframes), f"Expected {len(clientIDs)} trimmed dataframes. Found f{len(trimmed_dataframes)}"
150+
151+
client0ID = clientIDs[0]
152+
client0size = len(trimmed_dataframes[0])
153+
print(f"For client {client0ID}: {client0size} frames")
154+
for cID, df in zip(clientIDs[1:], trimmed_dataframes[1:]):
155+
dfsize = len(df)
156+
if client0size != dfsize:
157+
raise Exception(f"For client {cID}: expecting {client0size}, found {dfsize}")
158+
159+
print("Good. All trimmed dataframes have the same number of entries.")
134160

135161
#
136162
# Extract the frames from the original videos
137163
# and rename the file names to the timestamps (DONE)
138164
# extract(input_dir, output_dir)
139165

140-
141166
#
142167
# Reconstruct videos (TODO)
143168

@@ -152,11 +177,11 @@ def main(input_dir: Path, output_dir: Path):
152177
"with missing/dropped frames inserted as (black) artificial data."
153178
)
154179
parser.add_argument(
155-
"--infolder", type=str, help="The folder containing the collected videos and CSV files with the timestamps.",
180+
"--infolder", "-i", type=str, help="The folder containing the collected videos and CSV files with the timestamps.",
156181
required=True
157182
)
158183
parser.add_argument(
159-
"--outfolder", type=str, help="The folder where the repaired and aligned frames will be stored.",
184+
"--outfolder", "-o", type=str, help="The folder where the repaired and aligned frames will be stored.",
160185
required=True
161186
)
162187

@@ -168,7 +193,7 @@ def main(input_dir: Path, output_dir: Path):
168193
if not infolder.exists():
169194
raise Exception(f"Input folder '{infolder}' doesn't exist.")
170195

171-
if not infolder.exists():
196+
if not outfolder.exists():
172197
raise Exception(f"Output folder '{outfolder}' doesn't exist.")
173198

174199
main(infolder, outfolder)

PostProcessing/dataframes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from typing import Tuple
44

5+
56
def repair_dropped_frames(df: pd.DataFrame) -> pd.DataFrame:
6-
pass
7+
return df
78

89

910
# Function to find the largest value in the first entry of all dataframes

0 commit comments

Comments
 (0)