Skip to content

Instantly share code, notes, and snippets.

View khuangaf's full-sized avatar
:octocat:
Focusing

Kung-Hsiang Steeve Huang khuangaf

:octocat:
Focusing
View GitHub Profile
def second2timecode(time):
'''Convert second into time code for srt.'''
hours = time // 3600
time -= 3600 * hours
minutes = time // 60
time -= 60 * minutes
secs = np.floor(time % 60)
msecs = (time % 60 - secs) * 1000
timecode = "%02d:%02d:%02d,%03d" % (hours, minutes, secs, msecs)
return timecode
def same_subtitle(current_subtitle, next_subtitle):
'''Return true if the two given subtitles are the same (but can tolerate a bit difference)'''
# convert the two subtitle into set e.g. '我很乖' -> {'我','很','乖'}
current_set = set(current_subtitle)
next_set = set(next_subtitle)
current_set_len = len(current_set)
next_set_len = len(next_set)
intersect_set = current_set & next_set
intersect_set_len = len(intersect_set)
df = pd.DataFrame(columns=['id', 'prediction', 'confidence'])
df['id'] = image_ids
df['prediction'] = predictions
df['confidence'] = confidences
df.to_csv(f'{args.results_dir}/{video_id}.csv', index=None)
# the endpoint of the api
ENDPOINT_URL = 'https://vision.googleapis.com/v1p1beta1/images:annotate'
# the api key
os.environ['GOOGLE_APPLICATION_CREDENTIALS']='your/path/to/credentials.json'
# init api client
client = vision.ImageAnnotatorClient()
with io.open(YOUR_IMAGE_PATH, 'rb') as f:
content = f.read()
image = vision.types.Image(content=content)
class SubtitleConfig(Config):
"""Configuration for training on the nucleus segmentation dataset."""
# Give the configuration a recognizable name
NAME = "subtitle"
# Adjust depending on your GPU memory
IMAGES_PER_GPU = 4
# Number of classes (including background)
NUM_CLASSES = 1 + 1 # Background + subtitle
def get_multi_masks(img):
'''
Get connected components (multi-masks)
args:
img: np.ndarray
return:
np.ndarray with same dimension as the input
'''
# Threshold the image to make it become either 0 or 1
def video2frames(video_id, video_path, processed_videos_dir, frame_path, sample_rate):
"""
Execute shell command which calls ffmpeg to split video into frames.
Parameters
----------
video_id : str
The video id of a video
video_path: str
The directory storing the videos
processed_video_path: str
def extract_audio(video_id,videos_dir, audios_dir):
"""
Download the videos
Parameters
----------
video_id : str
A Youtube video id.
videos_dir: str
The directory which stores videos.
audios_dir: str
def download_video(video_id, path="videos", verbose=True):
"""
Download the videos
Parameters
----------
video_id : str
A Youtube video id.
path: str
The directory which stores videos.
def get_video_ids(playlist_id):
"""
Get the video ids given a playlist id.
Parameters
----------
playlist_id : str
A Youtube playlist id. (up to 50 results)
Returns
-------
video_ids: list(str)