1+ import os
2+
13import click
24import imageio .v3 as iio
35import re
@@ -46,12 +48,12 @@ def main(db_path, video_filename, prefix, callback=None):
4648
4749 By default, the `frames` table contains metadata about each frame.
4850 To include additional columns, supply a function in the `callback`
49- agrument .
51+ argument .
5052
5153 `callback` is an optional argument for running additional
5254 processing on each frame of the video.
53- If supplied, the function should accept a numpy ndarray of the frame as
54- the single argument , and return a dict.
55+ If supplied, the function should accept a numpy ndarray of the frame,
56+ and a dict containing metadata for the given frame , and return a dict.
5557 Keys in the returned dict will become columns in the database, and values
5658 will be that column's value for the frame.
5759
@@ -96,12 +98,13 @@ def parse_video(video_filename, callback=None):
9698 if frame ['pkt_dts' ] == 'N/A' :
9799 continue # last frame doesn't get displayed or something?
98100
99- frame ['filename' ] = video_filename
101+ frame ['filename' ] = os . path . basename ( video_filename )
100102 processed_frames .append (frame )
101103
102104 if callback :
103- for frame , frame_metadata in zip (iio .imiter (video_filename ), processed_frames ):
104- frame_metadata .update (callback (frame ))
105+ for i , (frame , frame_metadata ) in enumerate (zip (iio .imiter (video_filename ), processed_frames )):
106+ frame_metadata ['frame_no' ] = i
107+ frame_metadata .update (callback (frame , frame_metadata ))
105108
106109 return metadata , processed_frames
107110
@@ -121,14 +124,17 @@ def save_to_db(db, video_filename, metadata, frames, prefix):
121124 data ,
122125 pk = 'filename' ,
123126 replace = True ,
127+ alter = True ,
124128 )
125129
126130 frame_tracker = TypeTracker ()
127131 db [f'{ prefix } frames' ].insert_all (
128- frame_tracker .wrap ([ dict ( frame , ** { 'frame_no' : i }) for i , frame in enumerate ( frames )] ),
132+ frame_tracker .wrap (frames ),
129133 pk = ('frame_no' , 'filename' ),
130134 foreign_keys = [
131135 ('filename' , f'{ prefix } videos' , 'filename' ),
132136 ],
133- replace = True )
137+ replace = True ,
138+ alter = True ,
139+ )
134140 db [f'{ prefix } frames' ].transform (types = frame_tracker .types )
0 commit comments