Spaces:
Running on Zero
Running on Zero
File size: 2,808 Bytes
89d702d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import os
import re
import math
import cv2
import numpy as np
import pandas as pd
import torchaudio
from PIL import Image
def string_to_list(value):
if isinstance(value, np.ndarray):
value = value.tolist()
if isinstance(value, list):
return value
if value == '' or pd.isna(value):
return []
value = str(value).strip()
if value.startswith('['):
value = value[1:]
if value.endswith(']'):
value = value[:-1]
return [item.strip() for item in re.split('[\'\",]', value)
if item.strip() not in ['', ',']]
def func_gain_videopath(video_root, vid_name):
for suffix in ('.mp4', '.avi'):
candidate = f"{video_root}/{vid_name}{suffix}"
if os.path.exists(candidate):
return candidate
return f"{video_root}/{vid_name}.mp4"
def func_gain_audiopath(video_root, vid_name):
return f"{video_root}/{vid_name}.wav"
def func_gain_name2trans(trans_path):
from toolkit.utils.read_files import func_read_key_from_csv
names = func_read_key_from_csv(trans_path, 'name')
chis = func_read_key_from_csv(trans_path, 'chinese')
return {name: chi for name, chi in zip(names, chis)}
def func_read_audio_second(audio_path):
waveform, sr = torchaudio.load(audio_path)
if len(waveform.shape) == 2:
return waveform.shape[1] / sr
if len(waveform.shape) == 1:
return len(waveform) / sr
raise ValueError('Unsupported waveform shape')
def func_opencv_to_image(img):
return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
def func_decord_to_image(img):
return Image.fromarray(img)
def func_opencv_to_decord(img):
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def func_discrte_label_distribution(labels):
unique, counts = np.unique(labels, return_counts=True)
return dict(zip(unique.tolist(), counts.tolist()))
def func_label_distribution(labels):
return func_discrte_label_distribution(labels)
def split_list_into_batch(items, split_num=None, batchsize=None):
"""Split a list into non-empty batches while preserving item order."""
if split_num is None and batchsize is None:
raise ValueError("Either split_num or batchsize must be provided.")
if batchsize is not None and batchsize <= 0:
raise ValueError("batchsize must be positive.")
if split_num is not None and split_num <= 0:
raise ValueError("split_num must be positive.")
if len(items) == 0:
return []
if split_num is None:
split_num = math.ceil(len(items) / batchsize)
batches = []
each_split = math.ceil(len(items) / split_num)
for idx in range(split_num):
batch = items[idx * each_split:(idx + 1) * each_split]
if batch:
batches.append(batch)
return batches
|