Skip to content

Commit f65d821

Browse files
committed
Video frame rate chane
1 parent f39518d commit f65d821

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

superannotate/db/projects.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,51 +271,57 @@ def upload_video_to_project(
271271
video = cv2.VideoCapture(str(video_path), cv2.CAP_FFMPEG)
272272
logger.info("Video frame count is %s.", total_num_of_frames)
273273

274+
r = 1.0
274275
if target_fps is not None:
275276
video_fps = float(video.get(cv2.CAP_PROP_FPS))
276-
logger.info(
277-
"Video frame rate is %s. Target frame rate is %s.", video_fps,
278-
target_fps
279-
)
280277
if target_fps >= video_fps:
281-
target_fps = None
278+
logger.warning(
279+
"Video frame rate %s smaller than target frame rate %s. Cannot change frame rate.",
280+
video_fps, target_fps
281+
)
282282
else:
283+
logger.info(
284+
"Changing video frame rate from %s to target frame rate %s.",
285+
video_fps, target_fps
286+
)
283287
r = video_fps / target_fps
284-
percent_to_drop = 1.0 - 1.0 / r
285-
my_random = random.Random(122222)
286288

287289
zero_fill_count = len(str(total_num_of_frames))
288290
tempdir = tempfile.TemporaryDirectory()
289291

290292
video_name = Path(video_path).stem
291-
frame_no = 1
293+
frame_no = 0
294+
frame_no_with_change = 1.0
295+
extracted_frame_no = 1
292296
logger.info("Extracting frames from video to %s.", tempdir.name)
293297
while True:
294298
success, frame = video.read()
295299
if not success:
296300
break
297-
if target_fps is not None and my_random.random() < percent_to_drop:
301+
frame_no += 1
302+
if round(frame_no_with_change) != frame_no:
298303
continue
304+
frame_no_with_change += r
299305
frame_time = video.get(cv2.CAP_PROP_POS_MSEC) / 1000.0
300-
if frame_time < start_time:
301-
continue
302306
if end_time is not None and frame_time > end_time:
307+
break
308+
if frame_time < start_time:
303309
continue
304310
if rotate_code is not None:
305311
frame = cv2.rotate(frame, rotate_code)
306312
cv2.imwrite(
307313
str(
308314
Path(tempdir.name) / (
309-
video_name + "_" + str(frame_no).zfill(zero_fill_count) +
310-
".jpg"
315+
video_name + "_" +
316+
str(extracted_frame_no).zfill(zero_fill_count) + ".jpg"
311317
)
312318
), frame
313319
)
314-
frame_no += 1
320+
extracted_frame_no += 1
315321

316322
logger.info(
317323
"Extracted %s frames from video. Now uploading to platform.",
318-
frame_no - 1
324+
extracted_frame_no - 1
319325
)
320326

321327
filenames = upload_images_from_folder_to_project(

tests/test_video.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
PROJECT_NAME2 = "test video upload2"
1111

1212

13-
def test_image_quality_setting1(tmpdir):
13+
def test_video(tmpdir):
1414
tmpdir = Path(tmpdir)
1515

1616
projects = sa.search_projects(PROJECT_NAME1, return_metadata=True)

0 commit comments

Comments
 (0)