Spaces:
Running
Running
Upload app/utils.py with huggingface_hub
Browse files- app/utils.py +64 -0
app/utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from typing import List
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
|
| 7 |
+
# Disable all GPUS
|
| 8 |
+
tf.config.set_visible_devices([], 'GPU')
|
| 9 |
+
|
| 10 |
+
vocab = [x for x in "abcdefghijklmnopqrstuvwxyz'?!123456789 "]
|
| 11 |
+
char_to_num = tf.keras.layers.StringLookup(vocabulary=vocab, oov_token="")
|
| 12 |
+
num_to_char = tf.keras.layers.StringLookup(
|
| 13 |
+
vocabulary=char_to_num.get_vocabulary(), oov_token="", invert=True
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def load_video(path: str) -> List[float]:
|
| 17 |
+
cap = cv2.VideoCapture(path)
|
| 18 |
+
frames = []
|
| 19 |
+
for _ in range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))):
|
| 20 |
+
ret, frame = cap.read()
|
| 21 |
+
if not ret or frame is None:
|
| 22 |
+
break
|
| 23 |
+
frame = tf.image.rgb_to_grayscale(tf.cast(frame, tf.float32))
|
| 24 |
+
frames.append(frame[190:236, 80:220, :])
|
| 25 |
+
cap.release()
|
| 26 |
+
if not frames:
|
| 27 |
+
raise ValueError(f"No frames were read from video: {path}")
|
| 28 |
+
mean = tf.math.reduce_mean(frames)
|
| 29 |
+
std = tf.math.reduce_std(tf.cast(frames, tf.float32))
|
| 30 |
+
return tf.cast((frames - mean), tf.float32) / std
|
| 31 |
+
|
| 32 |
+
def load_alignments(path: str) -> List[str]:
|
| 33 |
+
with open(path, 'r') as f:
|
| 34 |
+
lines = f.readlines()
|
| 35 |
+
tokens = []
|
| 36 |
+
for line in lines:
|
| 37 |
+
line = line.split()
|
| 38 |
+
if len(line) < 3:
|
| 39 |
+
continue
|
| 40 |
+
if line[2] != 'sil':
|
| 41 |
+
tokens = [*tokens, ' ', line[2]]
|
| 42 |
+
return char_to_num(
|
| 43 |
+
tf.reshape(tf.strings.unicode_split(tokens, input_encoding='UTF-8'), (-1))
|
| 44 |
+
)[1:]
|
| 45 |
+
|
| 46 |
+
def load_data(path: str):
|
| 47 |
+
path = bytes.decode(path.numpy())
|
| 48 |
+
file_name = os.path.splitext(os.path.basename(path))[0]
|
| 49 |
+
|
| 50 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 51 |
+
data_dir = os.path.abspath(os.path.join(BASE_DIR, 'data', 's1'))
|
| 52 |
+
alignment_dir = os.path.abspath(os.path.join(BASE_DIR, 'data', 'alignments', 's1'))
|
| 53 |
+
|
| 54 |
+
video_path = os.path.join(data_dir, f'{file_name}.mpg')
|
| 55 |
+
alignment_path = os.path.join(alignment_dir, f'{file_name}.align')
|
| 56 |
+
|
| 57 |
+
if not os.path.exists(video_path):
|
| 58 |
+
raise FileNotFoundError(f"Video file {video_path} does not exist.")
|
| 59 |
+
if not os.path.exists(alignment_path):
|
| 60 |
+
raise FileNotFoundError(f"Alignment file {alignment_path} does not exist.")
|
| 61 |
+
|
| 62 |
+
frames = load_video(video_path)
|
| 63 |
+
alignments = load_alignments(alignment_path)
|
| 64 |
+
return frames, alignments
|