Spaces:
Sleeping
Sleeping
#coding=utf-8 | |
import os | |
import cv2 # OpenCV λΌμ΄λΈλ¬λ¦¬ | |
import numpy as np | |
from PIL import Image | |
# -- IO utils | |
# ν μ€νΈ λΌμΈ λΆλ¬μ€κΈ° | |
def read_txt_lines(filepath): | |
# νμΌμ΄ μλμ§ νμΈ, μμΌλ©΄ AssertionError λ©μμ§λ₯Ό λμ | |
assert os.path.isfile( filepath ), "Error when trying to read txt file, path does not exist: {}".format(filepath) # μνλ 쑰건μ λ³μκ°μ 보μ¦νκΈ° μν΄ μ¬μ© | |
# νμΌ λΆλ¬μ€κΈ° | |
with open( filepath ) as myfile: | |
content = myfile.read().splitlines() # λ¬Έμμ΄μ '\n' κΈ°μ€μΌλ‘ μͺΌκ° ν list μμ± | |
return content | |
# npz μ μ₯ | |
def save2npz(filename, data=None): | |
# λ°μ΄ν°κ° λΉμ΄μλμ§ νμΈ, μμΌλ©΄ AssertionError λ©μμ§λ₯Ό λμ | |
assert data is not None, "data is {}".format(data) | |
# νμΌ μμ κ²½μ° | |
if not os.path.exists(os.path.dirname(filename)): | |
os.makedirs(os.path.dirname(filename)) # λλ ν 리 μμ± | |
np.savez_compressed(filename, data=data) # μμΆλμ§ μμ .npz νμΌ νμ μΌλ‘ μ¬λ¬ λ°°μ΄ μ μ₯ | |
def save2npz(filename, data=None): | |
"""save2npz. | |
:param filename: str, the fileanme where the data will be saved. | |
:param data: ndarray, arrays to save to the file. | |
""" | |
assert data is not None, "data is {}".format(data) | |
if not os.path.exists(os.path.dirname(filename)): | |
os.makedirs(os.path.dirname(filename)) | |
np.savez_compressed(filename, data=data) | |
# λΉλμ€ λΆλ¬μ€κΈ° | |
def read_video(filename): | |
cap = cv2.VideoCapture(filename) # μμ κ°μ²΄(νμΌ) κ°μ Έμ€κΈ° | |
while(cap.isOpened()): # μμ νμΌ(μΉ΄λ©λΌ)μ΄ μ μμ μΌλ‘ μ΄λ Έλμ§(μ΄κΈ°νλμλμ§) μ¬λΆ | |
# ret: μ μμ μΌλ‘ μ½μ΄μλκ°? | |
# frame: ν μ₯μ μ΄λ―Έμ§(frame) κ°μ Έμ€κΈ° | |
ret, frame = cap.read() # BGR | |
if ret: # νλ μ μ 보λ₯Ό μ μμ μΌλ‘ μ½μ§ λͺ»νλ©΄ | |
yield frame # νλ μμ ν¨μ λ°κΉ₯μΌλ‘ μ λ¬νλ©΄μ μ½λ μ€νμ ν¨μ λ°κΉ₯μ μ보 | |
else: # νλ μ μ 보λ₯Ό μ μμ μΌλ‘ μ½μ§ λͺ»νλ©΄ | |
break # while λΉ μ Έλκ°κΈ° | |
cap.release() # μμ νμΌ(μΉ΄λ©λΌ) μ¬μ© μ’ λ£ | |
# Video μ 보 κ°μ Έμ€κΈ° | |
def get_video_info(infilename, is_print=False): | |
cap = cv2.VideoCapture(infilename) | |
if not cap.isOpened(): | |
print("could not open : ", infilename) | |
cap.release() | |
exit(0) | |
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
cap.release() | |
if is_print: | |
print('length : ', length) | |
print('width : ', width) | |
print('height : ', height) | |
print('fps : ', fps) | |
video_info = { | |
'length': length, | |
'width': width, | |
'height': height, | |
'fps': fps, | |
} | |
return video_info | |
# Video -> Numpy | |
# μ°Έκ³ κΉνλΈ μ½λ: https://github.com/khazit/Lip2Word/blob/master/lipReader.py#L22 | |
def videoToArray(video_pathname, is_gray=True) : | |
cap = cv2.VideoCapture(video_pathname) # μμ κ°μ²΄(νμΌ) κ°μ Έμ€κΈ° | |
# μμ νμΌ(μΉ΄λ©λΌ)μ΄ μ μμ μΌλ‘ μ΄λ¦¬μ§ μμ κ²½μ° | |
if not cap.isOpened(): | |
print("could not open : ", video_pathname) | |
cap.release() # μμ νμΌ(μΉ΄λ©λΌ) μ¬μ© μ’ λ£ | |
exit(0) # λΉ μ Έλκ°κΈ° | |
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # μμ νλ μ κ°μ | |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # μμ λλΉ | |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # μμ λμ΄ | |
fps = cap.get(cv2.CAP_PROP_FPS) # μμ FPS(Frames Per Second) | |
if is_gray: | |
video = np.zeros((n_frames, height, width)) # gray | |
else: | |
n_channels=3 | |
video = np.zeros((n_frames, height, width, n_channels)) # color | |
video = video.astype(np.uint8) | |
i = 0 | |
while True : | |
success, frame = cap.read() | |
if not success : | |
break | |
else : | |
# gray scale μ μ© | |
if is_gray: | |
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
video[i] = frame | |
i += 1 | |
cap.release() # μμ νμΌ(μΉ΄λ©λΌ) μ¬μ© μ’ λ£ | |
return video # μμ μ 보 μμ μμ νλ μ κ°μλ₯Ό μΆκ°ν numpy λ°ν | |
# Frame Sampling (νλ μ κ°μ λ§μΆκΈ°) | |
# μ°Έκ³ κΉνλΈ μ½λ: https://github.com/khazit/Lip2Word/blob/master/lipReader.py#L62 | |
def frameAdjust(video, target_frames=29): | |
n_frames = video.shape[0] # μμ νλ μ κ°μ | |
if target_frames == n_frames : | |
return video # μμ κ·Έλλ‘ λ°ν | |
else : | |
# μμ νλ μ κ°μ > μνλ νλ μ κ°μ | |
if n_frames > target_frames : | |
idx = np.linspace(0, n_frames-1, target_frames) # μ«μ μνμ€ μμ± # κ΅¬κ° μμμ , κ΅¬κ° λμ , κ΅¬κ° λ΄ μ«μ κ°μ | |
idx = np.around(idx, 0).astype(np.int32) # λ°μ¬λ¦Όνκ³ dtype μ μ μλ‘ λ³κ²½ | |
return video[idx] # μνλ νλ μ κ°μλ‘ sampling ν μμ | |
# μμ νλ μ κ°μ < μνλ νλ μ κ°μ | |
else : | |
output_video = np.zeros((target_frames, *video.shape[1:])).astype(np.uint8) # μνλ νλ μ κ°μμ λ§μΆ°μ 0μΌλ‘ μ΄κΈ°νν numpy μμ± | |
output_video[:n_frames] = video # μμ νλ μ κ°μκΉμ§ κ·Έλλ‘ μμ μ 보 μ μ₯ | |
# μνλ νλ μ κ°μλ§νΌ λ§μ§λ§ νλ μ 볡μ | |
for i in range(target_frames-n_frames+1) : | |
output_video[i+n_frames-1] = output_video[n_frames-1] | |
return output_video # μνλ νλ μ κ°μλ‘ sampling ν μμ | |