|
1 | 1 | import pandas as pd
|
| 2 | +import numpy as np |
2 | 3 |
|
3 | 4 | from typing import Tuple
|
4 | 5 |
|
| 6 | +THRESHOLD_NS = 10 * 1000 * 1000 #10 milisecond |
| 7 | + |
| 8 | +def compute_time_step(video_timestamps: pd.DataFrame) -> float: |
| 9 | + """ |
| 10 | + Compute the time steps of a video based on its timestamps. |
| 11 | +
|
| 12 | + Parameters: |
| 13 | + video_timestamps (pd.DataFrame): A pandas DataFrame containing timestamps of a video. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + float: The time step of the video time stamps. |
| 17 | + """ |
| 18 | + |
| 19 | + time_step = (video_timestamps['timestamp'].diff()).dropna().value_counts().index[0] |
| 20 | + |
| 21 | + |
| 22 | + return time_step |
| 23 | +def repair_dropped_frames(df: pd.DataFrame, time_step: float) -> pd.DataFrame: |
| 24 | + df['timestamp'] = pd.to_datetime(df['timestamp']).astype(np.int64) |
| 25 | + timestamps = df['timestamp'] |
| 26 | + repaired_rows = [] |
| 27 | + |
| 28 | + # Check for missing timestamps and generate them |
| 29 | + for i in range(len(timestamps) - 1): |
| 30 | + timestamp = timestamps.iloc[i] |
| 31 | + next_timestamp = timestamps.iloc[i + 1] |
| 32 | + |
| 33 | + repaired_rows.append([timestamp, 'Original']) |
| 34 | + |
| 35 | + if next_timestamp - timestamp > time_step: |
| 36 | + missing_timestamps_count = int((next_timestamp - timestamp)/ time_step) - 1 |
| 37 | + interval = (next_timestamp - timestamp) / (missing_timestamps_count + 1) |
| 38 | + |
| 39 | + for j in range(1, missing_timestamps_count + 1): |
| 40 | + new_timestamp = (timestamp + j * interval).astype(np.int64) |
| 41 | + repaired_rows.append([new_timestamp, 'Generated']) |
| 42 | + |
| 43 | + # Add the last row |
| 44 | + repaired_rows.append([timestamps.iloc[-1], 'Original']) |
| 45 | + |
| 46 | + print(len(repaired_rows)) |
| 47 | + # Create a new DataFrame with repaired rows |
| 48 | + columns = ['timestamp', 'Generated'] |
| 49 | + output_df = pd.DataFrame(repaired_rows, columns=columns) |
| 50 | + output_df['timestamp'] = pd.to_datetime(output_df['timestamp']).astype(np.int64) |
| 51 | + |
| 52 | + return output_df |
5 | 53 |
|
6 |
| -def repair_dropped_frames(df: pd.DataFrame) -> pd.DataFrame: |
7 |
| - return df |
8 | 54 |
|
9 | 55 | def save_dataframes(dataframes, prefix='df'):
|
10 | 56 | # Generate filenames based on a pattern or numbering scheme
|
|
0 commit comments