-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
83 lines (61 loc) · 2.55 KB
/
utils.py
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
import os
import tensorflow as tf
import cv2
from typing import List
import numpy as np
# Vocabulary setup
vocab = [x for x in "abcdefghijklmnopqrstuvwxyz'?!123456789 "]
char_to_num = tf.keras.layers.StringLookup(vocabulary=vocab, oov_token="")
num_to_char = tf.keras.layers.StringLookup(
vocabulary=char_to_num.get_vocabulary(), oov_token="", invert=True
)
def load_video(path:str) -> List[float]:
cap = cv2.VideoCapture(path)
frames = []
for _ in range(int(cap.get((cv2.CAP_PROP_FRAME_COUNT)))):
ret, frame = cap.read()
frame = tf.image.rgb_to_grayscale(frame)
frames.append(frame[190:236, 80:220, :])
cap.release()
mean = tf.math.reduce_mean(frames)
std = tf.math.reduce_std(tf.cast(frames, tf.float32))
return tf.cast((frames - mean), tf.float32)/std
# def load_video(path:str) -> List[float]:
# cap = cv2.VideoCapture(path)
# frames = []
# for _ in range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))):
# ret, frame = cap.read()
# # frame = tf.image.rgb_to_grayscale(frame)
# frame = tf.cast(tf.image.rgb_to_grayscale(frame), tf.float32)
# frames.append(frame[190:236,80:220,:])
# cap.release()
# mean = tf.math.reduce_mean(frames)
# std = tf.math.reduce_std(tf.cast(frames, tf.float32))
# return tf.cast((frames - mean), tf.float32) / std
def load_alignments(path:str) -> List[str]:
with open(path, 'r') as f:
lines = f.readlines()
tokens = []
for line in lines:
line = line.split()
if line[2] != 'sil':
tokens = [*tokens, ' ', line[2]]
return char_to_num(tf.reshape(tf.strings.unicode_split(tokens, input_encoding='UTF-8'), (-1)))[1:]
def load_data(path: str):
path = bytes.decode(path.numpy())
# print(f'This is the path: {path}')
# file_name = path.split('\\')[-1].split('.')[0]
# Extract the file name without extension
file_name = os.path.splitext(os.path.basename(path))[0]
# Construct video and alignment paths
video_path = os.path.join('data', 's1', f'{file_name}.mpg')
alignment_path = os.path.join('data', 'alignments', 's1', f'{file_name}.align')
# Ensure files exist
if not os.path.exists(video_path):
raise FileNotFoundError(f"Video file not found: {video_path}")
if not os.path.exists(alignment_path):
raise FileNotFoundError(f"Alignment file not found: {alignment_path}")
# Load video frames and alignments
frames = load_video(video_path)
alignments = load_alignments(alignment_path)
return frames, alignments