Spaces:
Runtime error
Runtime error
File size: 10,887 Bytes
7e356f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import cv2\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"dataframes = [\n",
" \"../data/dataset-unfiltered.parquet\",\n",
" \"../data/dataset.parquet\",\n",
"]\n",
"df_path = dataframes[1]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_parquet(df_path)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>video_id</th>\n",
" <th>frame_idx</th>\n",
" <th>timestamp</th>\n",
" <th>base64_image</th>\n",
" <th>dim_0</th>\n",
" <th>dim_1</th>\n",
" <th>dim_2</th>\n",
" <th>dim_3</th>\n",
" <th>dim_4</th>\n",
" <th>dim_5</th>\n",
" <th>...</th>\n",
" <th>dim_502</th>\n",
" <th>dim_503</th>\n",
" <th>dim_504</th>\n",
" <th>dim_505</th>\n",
" <th>dim_506</th>\n",
" <th>dim_507</th>\n",
" <th>dim_508</th>\n",
" <th>dim_509</th>\n",
" <th>dim_510</th>\n",
" <th>dim_511</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>8Ilh1ewceco</td>\n",
" <td>145</td>\n",
" <td>5.0</td>\n",
" <td>b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH...</td>\n",
" <td>0.009040</td>\n",
" <td>0.003338</td>\n",
" <td>0.029684</td>\n",
" <td>-0.033058</td>\n",
" <td>0.040864</td>\n",
" <td>-0.006447</td>\n",
" <td>...</td>\n",
" <td>0.033575</td>\n",
" <td>-0.019076</td>\n",
" <td>0.047166</td>\n",
" <td>-0.010574</td>\n",
" <td>-0.018608</td>\n",
" <td>-0.013465</td>\n",
" <td>-0.020017</td>\n",
" <td>0.086240</td>\n",
" <td>-0.029653</td>\n",
" <td>0.035949</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>8Ilh1ewceco</td>\n",
" <td>290</td>\n",
" <td>10.0</td>\n",
" <td>b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH...</td>\n",
" <td>0.004891</td>\n",
" <td>0.006527</td>\n",
" <td>0.004417</td>\n",
" <td>-0.000323</td>\n",
" <td>0.006400</td>\n",
" <td>-0.024191</td>\n",
" <td>...</td>\n",
" <td>-0.043122</td>\n",
" <td>-0.010695</td>\n",
" <td>0.005672</td>\n",
" <td>0.000172</td>\n",
" <td>-0.014442</td>\n",
" <td>-0.014647</td>\n",
" <td>-0.016840</td>\n",
" <td>0.100285</td>\n",
" <td>0.013794</td>\n",
" <td>0.015046</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>8Ilh1ewceco</td>\n",
" <td>435</td>\n",
" <td>15.0</td>\n",
" <td>b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH...</td>\n",
" <td>-0.022159</td>\n",
" <td>0.020703</td>\n",
" <td>-0.021607</td>\n",
" <td>-0.019721</td>\n",
" <td>-0.006067</td>\n",
" <td>-0.035070</td>\n",
" <td>...</td>\n",
" <td>-0.017047</td>\n",
" <td>-0.018341</td>\n",
" <td>-0.006733</td>\n",
" <td>-0.007040</td>\n",
" <td>-0.008368</td>\n",
" <td>0.009755</td>\n",
" <td>-0.045662</td>\n",
" <td>0.116601</td>\n",
" <td>-0.000572</td>\n",
" <td>-0.000985</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>8Ilh1ewceco</td>\n",
" <td>580</td>\n",
" <td>20.0</td>\n",
" <td>b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH...</td>\n",
" <td>-0.015903</td>\n",
" <td>0.033545</td>\n",
" <td>0.009257</td>\n",
" <td>-0.033540</td>\n",
" <td>0.010586</td>\n",
" <td>-0.028067</td>\n",
" <td>...</td>\n",
" <td>-0.016532</td>\n",
" <td>0.012388</td>\n",
" <td>0.020868</td>\n",
" <td>-0.012635</td>\n",
" <td>0.010914</td>\n",
" <td>0.009203</td>\n",
" <td>-0.010078</td>\n",
" <td>0.063971</td>\n",
" <td>-0.038024</td>\n",
" <td>0.025840</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>8Ilh1ewceco</td>\n",
" <td>725</td>\n",
" <td>25.0</td>\n",
" <td>b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH...</td>\n",
" <td>-0.010193</td>\n",
" <td>-0.012323</td>\n",
" <td>0.023012</td>\n",
" <td>-0.015893</td>\n",
" <td>0.047041</td>\n",
" <td>0.050783</td>\n",
" <td>...</td>\n",
" <td>-0.024013</td>\n",
" <td>-0.009684</td>\n",
" <td>-0.035481</td>\n",
" <td>-0.024743</td>\n",
" <td>-0.009812</td>\n",
" <td>0.035214</td>\n",
" <td>-0.008957</td>\n",
" <td>0.124215</td>\n",
" <td>-0.012410</td>\n",
" <td>0.040907</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows Γ 516 columns</p>\n",
"</div>"
],
"text/plain": [
" video_id frame_idx timestamp \n",
"0 8Ilh1ewceco 145 5.0 \\\n",
"1 8Ilh1ewceco 290 10.0 \n",
"2 8Ilh1ewceco 435 15.0 \n",
"3 8Ilh1ewceco 580 20.0 \n",
"4 8Ilh1ewceco 725 25.0 \n",
"\n",
" base64_image dim_0 dim_1 \n",
"0 b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH... 0.009040 0.003338 \\\n",
"1 b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH... 0.004891 0.006527 \n",
"2 b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH... -0.022159 0.020703 \n",
"3 b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH... -0.015903 0.033545 \n",
"4 b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgH... -0.010193 -0.012323 \n",
"\n",
" dim_2 dim_3 dim_4 dim_5 ... dim_502 dim_503 dim_504 \n",
"0 0.029684 -0.033058 0.040864 -0.006447 ... 0.033575 -0.019076 0.047166 \\\n",
"1 0.004417 -0.000323 0.006400 -0.024191 ... -0.043122 -0.010695 0.005672 \n",
"2 -0.021607 -0.019721 -0.006067 -0.035070 ... -0.017047 -0.018341 -0.006733 \n",
"3 0.009257 -0.033540 0.010586 -0.028067 ... -0.016532 0.012388 0.020868 \n",
"4 0.023012 -0.015893 0.047041 0.050783 ... -0.024013 -0.009684 -0.035481 \n",
"\n",
" dim_505 dim_506 dim_507 dim_508 dim_509 dim_510 dim_511 \n",
"0 -0.010574 -0.018608 -0.013465 -0.020017 0.086240 -0.029653 0.035949 \n",
"1 0.000172 -0.014442 -0.014647 -0.016840 0.100285 0.013794 0.015046 \n",
"2 -0.007040 -0.008368 0.009755 -0.045662 0.116601 -0.000572 -0.000985 \n",
"3 -0.012635 0.010914 0.009203 -0.010078 0.063971 -0.038024 0.025840 \n",
"4 -0.024743 -0.009812 0.035214 -0.008957 0.124215 -0.012410 0.040907 \n",
"\n",
"[5 rows x 516 columns]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(df.head())"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"60844"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# wierdly all the timestamps are rounded to the nearest second o.O\n",
"(df[\"timestamp\"].apply(lambda x: x % 1) == 0).sum()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 1417/1417 [00:02<00:00, 626.05it/s]\n"
]
}
],
"source": [
"video_fps = {}\n",
"for video_id in tqdm(df.video_id.unique()):\n",
" video_path = f\"../data/videos/{video_id}.mp4\"\n",
" cap = cv2.VideoCapture(str(video_path))\n",
" num_video_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n",
" fps = cap.get(cv2.CAP_PROP_FPS)\n",
" video_fps[video_id] = fps"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def correct_timestamp(row):\n",
" # pd.Series([1, 2], index=['foo', 'bar'])\n",
" video_id = row[\"video_id\"]\n",
" frame_idx = row[\"frame_idx\"]\n",
" fps = video_fps[video_id]\n",
" timestamp = frame_idx / fps\n",
" return timestamp\n",
"\n",
"\n",
"df[\"timestamp\"] = df.apply(correct_timestamp, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"df.to_parquet(df_path, index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "visual-content-search-over-videos",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|