original_dir ="src/videos/original"path = os.path.join(original_dir, "Penguins.mp4")video = api.video.upload_path( dataset.id, name="Penguins", path=path)print(f'Video "{video.name}" uploaded to Supervisely with ID:{video.id}')
Output:
# Video "Penguins" uploaded to Supervisely platform with ID:17539140
Upload list of videos.
✅ Supervisely API allows uploading multiple videos in a single request. The code sample below sends fewer requests and it leads to a significant speed-up of our original code.
Source code:
names = ["Flamingo.mp4","Swans.mp4","Toucan.mp4"]paths = [os.path.join(original_dir, name)for name in names]upload_info = api.video.upload_paths(dataset.id, names, paths)print(f"{len(upload_info)} videos successfully uploaded to Supervisely platform")
Output:
# 3 videos successfully uploaded to Supervisely platform
Get information about videos
Single video.
Get information about video from Supervisely by id.
You can also get information about video from Supervisely by name.
Source code:
video_info_by_name = api.video.get_info_by_name(dataset.id, video.name)print(f"Video name - '{video_info_by_name.name}'")
Output:
# Video name - 'Penguins'
Get all videos from dataset.
Get information about video from Supervisely by id.
Source code:
video_info_list = api.video.get_list(dataset.id)print(f"{len(video_info_list)} videos information received.")
Output:
# 4 videos information received.
Download video
Download video from Supervisely to local directory by id. Source code:
save_path = os.path.join(result_dir, f"{video_info.name}.mp4")api.video.download_path(video_info.id, save_path)print(f"Video has been successfully downloaded to '{save_path}'")
Output:
# Video has been successfully downloaded to 'src/videos/result/Penguins.mp4'
Download single frame of video as image from Supervisely to local directory.
Source code:
frame_idx =15file_name ="frame.png"save_path = os.path.join(result_dir, file_name)api.video.frame.download_path(video_info.id, frame_idx, save_path)print(f"Video frame has been successfully downloaded as image to '{save_path}'")
Output:
# Video frame has been successfully downloaded as image to 'src/videos/result/frame.png'
Download multiple frames in a single requests from Supervisely and save as images to local directory.
Source code:
frame_indexes = [5,10,20,30,45]save_paths = [os.path.join(result_dir, f"frame_{idx}.png")for idx in frame_indexes]api.video.frame.download_paths(video_info.id, frame_indexes, save_paths)print(f"{len(frame_indexes)} images has been successfully downloaded to '{save_path}'")
Output:
# 5 images has been successfully downloaded to 'src/videos/result/frame.png'
Download video frames as RGB NumPy matrix
You can also download video frame as RGB NumPy matrix.
# Video (ID: 17536607) has been successfully removed.
Remove list of videos.
Remove list of videos from Supervisely by ids.
Source code:
videos_to_remove = api.video.get_list(dataset.id)remove_ids = [video.id for video in videos_to_remove]api.video.remove_batch(remove_ids)print(f"{len(videos_to_remove)} videos successfully removed.")
Output:
# 3 videos have been successfully removed.
Information about available codecs, extensions, and containers.
Note: Only basic video codecs are available in the Community Edition, for additional video codecs you can try the Enterprise Edition.
The Enterprise Edition allows you to use the full range of extensions, containers and codecs listed below without any limits.
In the Community Edition, it is recommended to use vp9, h264 codecs with mp4 container.
How to exract frames from videos correctly using the OpenCV library.
In case you need to exract frames from videos, you should be aware of one important detail of the OpenCV library. According to issue #15499, different versions of the OpenCV library have different values of the CAP_PROP_ORIENTATION_AUTO flag. This may cause that VideoCapture to ignore video orientation metadata. To avoid incorrect extraction of frames from videos, it is recommended to directly define flag CAP_PROP_ORIENTATION_AUTO:
cap = cv2.VideoCapture(filepath)# set CAP_PROP_ORIENTATION_AUTO flag for VideoCapturecap.set(cv2.CAP_PROP_ORIENTATION_AUTO, 1)while cap.isOpened(): success, frame = cap.read()ifnot success:print("Can't receive frame (stream end?). Exiting ...")break# cv2.imshow(f"{frame.shape[:2]}", frame)if cv2.waitKey(1)&0xFF==ord("q"):break# Release everything if job is finishedcap.release()# cv2.destroyAllWindows()