@@ -257,51 +257,57 @@ def upload_video_to_project(
257257 video = cv2 .VideoCapture (str (video_path ), cv2 .CAP_FFMPEG )
258258 logger .info ("Video frame count is %s." , total_num_of_frames )
259259
260+ r = 1.0
260261 if target_fps is not None :
261262 video_fps = float (video .get (cv2 .CAP_PROP_FPS ))
262- logger .info (
263- "Video frame rate is %s. Target frame rate is %s." , video_fps ,
264- target_fps
265- )
266263 if target_fps >= video_fps :
267- target_fps = None
264+ logger .warning (
265+ "Video frame rate %s smaller than target frame rate %s. Cannot change frame rate." ,
266+ video_fps , target_fps
267+ )
268268 else :
269+ logger .info (
270+ "Changing video frame rate from %s to target frame rate %s." ,
271+ video_fps , target_fps
272+ )
269273 r = video_fps / target_fps
270- percent_to_drop = 1.0 - 1.0 / r
271- my_random = random .Random (122222 )
272274
273275 zero_fill_count = len (str (total_num_of_frames ))
274276 tempdir = tempfile .TemporaryDirectory ()
275277
276278 video_name = Path (video_path ).stem
277- frame_no = 1
279+ frame_no = 0
280+ frame_no_with_change = 1.0
281+ extracted_frame_no = 1
278282 logger .info ("Extracting frames from video to %s." , tempdir .name )
279283 while True :
280284 success , frame = video .read ()
281285 if not success :
282286 break
283- if target_fps is not None and my_random .random () < percent_to_drop :
287+ frame_no += 1
288+ if round (frame_no_with_change ) != frame_no :
284289 continue
290+ frame_no_with_change += r
285291 frame_time = video .get (cv2 .CAP_PROP_POS_MSEC ) / 1000.0
286- if frame_time < start_time :
287- continue
288292 if end_time is not None and frame_time > end_time :
293+ break
294+ if frame_time < start_time :
289295 continue
290296 if rotate_code is not None :
291297 frame = cv2 .rotate (frame , rotate_code )
292298 cv2 .imwrite (
293299 str (
294300 Path (tempdir .name ) / (
295- video_name + "_" + str ( frame_no ). zfill ( zero_fill_count ) +
296- ".jpg"
301+ video_name + "_" +
302+ str ( extracted_frame_no ). zfill ( zero_fill_count ) + ".jpg"
297303 )
298304 ), frame
299305 )
300- frame_no += 1
306+ extracted_frame_no += 1
301307
302308 logger .info (
303309 "Extracted %s frames from video. Now uploading to platform." ,
304- frame_no - 1
310+ extracted_frame_no - 1
305311 )
306312
307313 filenames = upload_images_from_folder_to_project (
0 commit comments