Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

format

Shen-Chenhuibb835944b4bc
diff --git a/tools/datasets/datautil.py b/tools/datasets/datautil.py
index db073e9..7db70d2 100644
--- a/tools/datasets/datautil.py
+++ b/tools/datasets/datautil.py
@@ -12,11 +12,11 @@ import numpy as np
import pandas as pd
from PIL import Image
from tqdm import tqdm
-import torchvision
-from .utils import IMG_EXTENSIONS
from opensora.datasets.read_video import read_video
+from .utils import IMG_EXTENSIONS
+
tqdm.pandas()
try:
@@ -472,7 +472,7 @@ def read_data(input_paths):
input_name += os.path.basename(input_path).split(".")[0]
if i != len(input_list) - 1:
input_name += "+"
- print(f"Loaded {len(data[-1])} samples from \'{input_path}\'.")
+ print(f"Loaded {len(data[-1])} samples from '{input_path}'.")
if len(data) == 0:
print(f"No samples to process. Exit.")
exit()
@@ -564,6 +564,10 @@ def main(args):
if args.lang is not None:
assert "text" in data.columns
data = data[data["text"].progress_apply(detect_lang)] # cannot parallelize
+ if args.remove_empty_path:
+ assert "path" in data.columns
+ data = data[data["path"].str.len() > 0]
+ data = data[~data["path"].isna()]
if args.remove_empty_caption:
assert "text" in data.columns
data = data[data["text"].str.len() > 0]
@@ -704,6 +708,11 @@ def parse_args():
parser.add_argument(
"--path-subset", type=str, default=None, help="extract a subset data containing the given `path-subset` value"
)
+ parser.add_argument(
+ "--remove-empty-path",
+ action="store_true",
+ help="remove rows with empty path", # caused by transform, cannot read path
+ )
# caption filtering
parser.add_argument(
@@ -778,6 +787,8 @@ def get_output_path(args, input_name):
name += "_relpath"
if args.abspath is not None:
name += "_abspath"
+ if args.remove_empty_path:
+ name += "_noemptypath"
# caption filtering
if args.remove_empty_caption:
diff --git a/tools/datasets/transform.py b/tools/datasets/transform.py
index 94a4276..bffb191 100644
--- a/tools/datasets/transform.py
+++ b/tools/datasets/transform.py
@@ -37,16 +37,19 @@ def resize(path, length, input_dir, output):
ext = os.path.splitext(path)[1].lower()
assert ext in IMG_EXTENSIONS
img = cv2.imread(path)
- h, w = img.shape[:2]
- if min(h, w) > length:
- if h > w:
- new_h = length
- new_w = int(w * new_h / h)
- else:
- new_w = length
- new_h = int(h * new_w / w)
- img = cv2.resize(img, (new_w, new_h))
- cv2.imwrite(path_new, img)
+ if img is not None:
+ h, w = img.shape[:2]
+ if min(h, w) > length:
+ if h > w:
+ new_h = length
+ new_w = int(w * new_h / h)
+ else:
+ new_w = length
+ new_h = int(h * new_w / w)
+ img = cv2.resize(img, (new_w, new_h))
+ cv2.imwrite(path_new, img)
+ else:
+ path_new = ""
return path_new
@@ -55,18 +58,21 @@ def rand_crop(path, input_dir, output):
path_new = get_new_path(path, input_dir, output)
assert ext in IMG_EXTENSIONS
img = cv2.imread(path)
- h, w = img.shape[:2]
- width, height, _ = img.shape
- pos = random.randint(0, 3)
- if pos == 0:
- img_cropped = img[: width // 2, : height // 2]
- elif pos == 1:
- img_cropped = img[width // 2 :, : height // 2]
- elif pos == 2:
- img_cropped = img[: width // 2, height // 2 :]
+ if img is not None:
+ h, w = img.shape[:2]
+ width, height, _ = img.shape
+ pos = random.randint(0, 3)
+ if pos == 0:
+ img_cropped = img[: width // 2, : height // 2]
+ elif pos == 1:
+ img_cropped = img[width // 2 :, : height // 2]
+ elif pos == 2:
+ img_cropped = img[: width // 2, height // 2 :]
+ else:
+ img_cropped = img[width // 2 :, height // 2 :]
+ cv2.imwrite(path_new, img_cropped)
else:
- img_cropped = img[width // 2 :, height // 2 :]
- cv2.imwrite(path_new, img_cropped)
+ path_new = ""
return path_new
@@ -74,8 +80,10 @@ def main(args):
data = pd.read_csv(args.input)
if args.method == "img_rand_crop":
data["path"] = apply(data["path"], lambda x: rand_crop(x, args.input_dir, args.output))
+ output_csv = args.input.replace(".csv", f"_rand_crop.csv")
elif args.method == "img_resize":
data["path"] = apply(data["path"], lambda x: resize(x, args.length, args.input_dir, args.output))
+ output_csv = args.input.replace(".csv", f"_resized{args.length}.csv")
elif args.method == "vid_frame_extract":
points = args.points if args.points is not None else args.points_index
data = pd.DataFrame(np.repeat(data.values, 3, axis=0), columns=data.columns)
@@ -87,8 +95,8 @@ def main(args):
else:
data.loc[i::num_points, "point"] = data.loc[i::num_points, "num_frames"] * point
data["path"] = apply(data, lambda x: extract_frames(x["path"], args.input_dir, args.output, x["point"]), axis=1)
+ output_csv = args.input.replace(".csv", f"_vid_frame_extract.csv")
- output_csv = args.input.replace(".csv", f"_resized{args.length}.csv")
data.to_csv(output_csv, index=False)
print(f"Saved to {output_csv}")