@@ -210,16 +210,14 @@ def _get_video_frames_count(video_path):
210210 Get video frames count
211211 """
212212 video = cv2 .VideoCapture (str (video_path ), cv2 .CAP_FFMPEG )
213- total_num_of_frames = int (video .get (cv2 .CAP_PROP_FRAME_COUNT ))
214- if total_num_of_frames < 0 :
215- total_num_of_frames = 0
216- flag = True
217- while flag :
218- flag , _ = video .read ()
219- if flag :
220- total_num_of_frames += 1
221- else :
222- break
213+ total_num_of_frames = 0
214+ flag = True
215+ while flag :
216+ flag , _ = video .read ()
217+ if flag :
218+ total_num_of_frames += 1
219+ else :
220+ break
223221 return total_num_of_frames
224222
225223
@@ -281,16 +279,25 @@ def _get_video_rotate_code(video_path):
281279
282280
283281def _extract_frames_from_video (
284- start_time , end_time , ratio , video , video_path , tempdir , limit , rotate_code ,
285- total_num_of_frames
282+ start_time , end_time , video_path , tempdir , limit , target_fps
286283):
284+ video = cv2 .VideoCapture (str (video_path ), cv2 .CAP_FFMPEG )
285+ if not video .isOpened ():
286+ raise SABaseException (0 , "Couldn't open video file " + str (video_path ))
287+ total_num_of_frames = _get_video_frames_count (video_path )
288+ logger .info ("Video frame count is %s." , total_num_of_frames )
289+ ratio = 1.0
290+ if target_fps :
291+ ratio = _get_video_fps_ration (target_fps , video , ratio )
292+ rotate_code = _get_video_rotate_code (video_path )
287293 video_name = Path (video_path ).stem
288294 frame_no = 0
289295 frame_no_with_change = 1.0
290296 extracted_frame_no = 1
291297 logger .info ("Extracting frames from video to %s." , tempdir .name )
292298 zero_fill_count = len (str (total_num_of_frames ))
293- while extracted_frame_no < (limit + 1 ):
299+ extracted_frames_paths = []
300+ while len (extracted_frames_paths ) < limit :
294301 success , frame = video .read ()
295302 if not success :
296303 break
@@ -305,16 +312,16 @@ def _extract_frames_from_video(
305312 continue
306313 if rotate_code :
307314 frame = cv2 .rotate (frame , rotate_code )
308- cv2 .imwrite (
309- str (
310- Path (tempdir .name ) / (
311- video_name + "_" +
312- str (extracted_frame_no ).zfill (zero_fill_count ) + ".jpg"
313- )
314- ), frame
315+ path = str (
316+ Path (tempdir .name ) / (
317+ video_name + "_" +
318+ str (extracted_frame_no ).zfill (zero_fill_count ) + ".jpg"
319+ )
315320 )
321+ cv2 .imwrite (path , frame )
322+ extracted_frames_paths .append (path )
316323 extracted_frame_no += 1
317- return extracted_frame_no - 1
324+ return extracted_frames_paths
318325
319326
320327def upload_video_to_project (
@@ -361,24 +368,13 @@ def upload_video_to_project(
361368 "The function does not support projects containing images attached with URLs"
362369 )
363370 logger .info ("Uploading from video %s." , str (video_path ))
364- rotate_code = _get_video_rotate_code (video_path )
365- video = cv2 .VideoCapture (str (video_path ), cv2 .CAP_FFMPEG )
366- if not video .isOpened ():
367- raise SABaseException (0 , "Couldn't open video file " + str (video_path ))
368-
369- total_num_of_frames = _get_video_frames_count (video_path )
370- logger .info ("Video frame count is %s." , total_num_of_frames )
371- ratio = 1.0
372- if target_fps :
373- ratio = _get_video_fps_ration (target_fps , video , ratio )
374371 tempdir = tempfile .TemporaryDirectory ()
375- extracted_frame_no = _extract_frames_from_video (
376- start_time , end_time , ratio , video , video_path , tempdir , limit ,
377- rotate_code , total_num_of_frames
372+ extracted_frames = _extract_frames_from_video (
373+ start_time , end_time , video_path , tempdir , limit , target_fps
378374 )
379375 logger .info (
380376 "Extracted %s frames from video. Now uploading to platform." ,
381- extracted_frame_no
377+ len ( extracted_frames )
382378 )
383379 filenames = upload_images_from_folder_to_project (
384380 (project , folder ),
@@ -917,7 +913,7 @@ def upload_images_to_project(
917913 prefix = res ['filePath' ]
918914 limit = res ['availableImageCount' ]
919915 images_to_upload = img_paths [:limit ]
920- images_to_skip = img_paths [limit :]
916+ images_to_skip = [ str ( path ) for path in img_paths [limit :] ]
921917 chunksize = int (math .ceil (len (images_to_upload ) / _NUM_THREADS ))
922918
923919 tqdm_thread = threading .Thread (
@@ -2353,4 +2349,4 @@ def clone_project(
23532349 if project_description is not None :
23542350 metadata ["description" ] = project_description
23552351
2356- return create_project_from_metadata (metadata )
2352+ return create_project_from_metadata (metadata )
0 commit comments