sradc commited on
Commit
7e356f1
β€’
1 Parent(s): 5a9c0da

nb to surgically fix the timestamps in the parquet file

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