Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

update get_info()

pxy514511282db2
diff --git a/tools/datasets/datautil.py b/tools/datasets/datautil.py
index d4d52a9..68bf5be 100644
--- a/tools/datasets/datautil.py
+++ b/tools/datasets/datautil.py
@@ -8,6 +8,7 @@ from functools import partial
from glob import glob
import cv2
+from PIL import Image
import numpy as np
import pandas as pd
import torchvision
@@ -48,7 +49,7 @@ def get_video_length(cap, method="header"):
return length
-def get_info(path):
+def get_info_old(path):
try:
ext = os.path.splitext(path)[1].lower()
if ext in IMG_EXTENSIONS:
@@ -72,21 +73,78 @@ def get_info(path):
return 0, 0, 0, np.nan, np.nan, np.nan
-def get_video_info(path):
+def get_info(path):
try:
- vframes, _, infos = torchvision.io.read_video(filename=path, pts_unit="sec", output_format="TCHW")
- num_frames, height, width = vframes.shape[0], vframes.shape[2], vframes.shape[3]
- aspect_ratio = height / width
- if "video_fps" in infos:
- fps = infos["video_fps"]
+ ext = os.path.splitext(path)[1].lower()
+ if ext in IMG_EXTENSIONS:
+ return get_image_info(path)
else:
- fps = np.nan
- resolution = height * width
- return num_frames, height, width, aspect_ratio, fps, resolution
+ return get_video_info(path)
except:
return 0, 0, 0, np.nan, np.nan, np.nan
+def get_image_info(path, backend='pillow'):
+ if backend == 'pillow':
+ try:
+ with open(path, "rb") as f:
+ img = Image.open(f)
+ img = img.convert("RGB")
+ width, height = img.size
+ num_frames, fps = 1, np.nan
+ hw = height * width
+ aspect_ratio = height / width if width > 0 else np.nan
+ return num_frames, height, width, aspect_ratio, fps, hw
+ except:
+ return 0, 0, 0, np.nan, np.nan, np.nan
+ elif backend == 'cv2':
+ try:
+ im = cv2.imread(path)
+ if im is None:
+ return 0, 0, 0, np.nan, np.nan, np.nan
+ height, width = im.shape[:2]
+ num_frames, fps = 1, np.nan
+ hw = height * width
+ aspect_ratio = height / width if width > 0 else np.nan
+ return num_frames, height, width, aspect_ratio, fps, hw
+ except:
+ return 0, 0, 0, np.nan, np.nan, np.nan
+ else:
+ raise ValueError
+
+
+def get_video_info(path, backend='torchvision'):
+ if backend == 'torchvision':
+ try:
+ vframes, _, infos = torchvision.io.read_video(filename=path, pts_unit="sec", output_format="TCHW")
+ num_frames, height, width = vframes.shape[0], vframes.shape[2], vframes.shape[3]
+ if "video_fps" in infos:
+ fps = infos["video_fps"]
+ else:
+ fps = np.nan
+ hw = height * width
+ aspect_ratio = height / width if width > 0 else np.nan
+ return num_frames, height, width, aspect_ratio, fps, hw
+ except:
+ return 0, 0, 0, np.nan, np.nan, np.nan
+ elif backend == 'cv2':
+ try:
+ cap = cv2.VideoCapture(path)
+ num_frames, height, width, fps = (
+ get_video_length(cap, method="header"),
+ int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
+ int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
+ float(cap.get(cv2.CAP_PROP_FPS)),
+ )
+ hw = height * width
+ aspect_ratio = height / width if width > 0 else np.nan
+ return num_frames, height, width, aspect_ratio, fps, hw
+ except:
+ return 0, 0, 0, np.nan, np.nan, np.nan
+ else:
+ raise ValueError
+
+
# ======================================================
# --refine-llm-caption
# ======================================================