File size: 8,014 Bytes
8bfbed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d4096e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bfbed4
8c79f36
 
4d4096e
8c79f36
 
 
 
 
 
 
 
 
4d4096e
8c79f36
 
be4c1a1
8c79f36
 
8bfbed4
8c79f36
8bfbed4
8c79f36
 
8bfbed4
 
 
 
 
8c79f36
 
4d4096e
8bfbed4
4d4096e
 
 
 
 
 
 
 
 
 
 
 
8c79f36
8bfbed4
8c79f36
 
 
8bfbed4
4d4096e
a25c576
 
 
 
 
 
572791b
8bfbed4
 
8c79f36
 
8bfbed4
 
8c79f36
8bfbed4
 
 
a25c576
8c79f36
 
 
 
8bfbed4
8c79f36
8bfbed4
 
8c79f36
 
 
8bfbed4
 
 
4d4096e
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# import streamlit as st
# import os
# from utils.demo import load_video, ctc_decode
# from utils.two_stream_infer import load_model
# import os
# from scripts.extract_lip_coordinates import generate_lip_coordinates
# import options as opt

# st.set_page_config(layout="wide")

# model = load_model()

# st.title("Lipreading final year project Demo")

# st.info(
#     "The inference speed is very slow on Huggingface spaces due to it being processed entirely on CPU ",
#     icon="ℹ️",
# )
# # Generating a list of options or videos
# options = os.listdir(os.path.join("app_input"))
# selected_video = st.selectbox("Choose video", options)

# col1, col2 = st.columns(2)


# with col1:
#     file_path = os.path.join("app_input", selected_video)
#     video_name = selected_video.split(".")[0]
#     os.system(f"ffmpeg -i {file_path} -vcodec libx264 {video_name}.mp4 -y")

#     # Rendering inside of the app
#     video = open(f"{video_name}.mp4", "rb")
#     video_bytes = video.read()
#     st.video(video_bytes)


# with col1, st.spinner("Splitting video into frames"):
#     video, img_p, files = load_video(f"{video_name}.mp4", opt.device)
#     prediction_video = video
#     st.markdown(f"Frames Generated:\n{files}")
#     frames_generated = True
# with col1, st.spinner("Generating Lip Landmark Coordinates"):
#     coordinates = generate_lip_coordinates(f"{video_name}_samples")
#     prediction_coordinates = coordinates
#     st.markdown(f"Coordinates Generated:\n{coordinates}")
#     coordinates_generated = True

# with col2:
#     st.info("Ready to make prediction!")
#     generate = st.button("Generate")
#     if generate:
#         with col2, st.spinner("Generating..."):
#             y = model(
#                 prediction_video[None, ...].to(opt.device),
#                 prediction_coordinates[None, ...].to(opt.device),
#             )
#             txt = ctc_decode(y[0])
#             st.text(txt[-1])
            
# st.info("Author ©️ : wissem karous ")
# st.info("Made with ❤️  ")

///////////////////
# import streamlit as st
# import os
# from utils.demo import load_video, ctc_decode
# from utils.two_stream_infer import load_model
# from scripts.extract_lip_coordinates import generate_lip_coordinates
# import options as opt

# st.set_page_config(layout="wide")

# model = load_model()

# st.title("Lipreading final year project Demo")

# st.info(
#     "The inference speed is very slow on Huggingface spaces due to it being processed entirely on CPU ",
#     icon="ℹ️",
# )

# # Generating a list of options or videos
# options = sorted(os.listdir(os.path.join("app_input")))  # Ensure the list is sorted
# selected_video = st.selectbox("Choose video", options)

# # Find the index of the selected video and calculate the index of the next video
# selected_index = options.index(selected_video)
# next_video_index = (selected_index + 1) % len(options)  # Ensures looping back to start
# next_video = options[next_video_index]

# col1, col2 = st.columns(2)

# # Function to display video in a column
# def display_video(column, video_path, video_name):
#     os.system(f"ffmpeg -i {video_path} -vcodec libx264 {video_name}.mp4 -y")
#     video = open(f"{video_name}.mp4", "rb")
#     video_bytes = video.read()
#     column.video(video_bytes)

# # Displaying the selected video in the first column
# with col1:
#     file_path = os.path.join("app_input", selected_video)
#     video_name = selected_video.split(".")[0]
#     display_video(col1, file_path, video_name)
# # Displaying the next video in the second column
# with col2:
#     st.info("Expected Result !")
#     next_file_path = os.path.join("app_input", next_video)
#     next_video_name = next_video.split(".")[0]
#     display_video(col2, next_file_path, next_video_name)


# # Assuming further processing (like generating predictions) is only intended for the first (selected) video
# with col1, st.spinner("Processing video..."):
#     video, img_p, files = load_video(f"{video_name}.mp4", opt.device)
#     coordinates = generate_lip_coordinates(f"{video_name}_samples")
#     # Assuming 'frames_generated' and 'coordinates_generated' are used for control flow or further processing
#     frames_generated = True
#     coordinates_generated = True
#     if frames_generated and coordinates_generated:
#         st.markdown(f"Frames Generated for {video_name}:\n{files}")
#         st.markdown(f"Coordinates Generated for {video_name}:\n{coordinates}")

# with col2:
#     st.info("Ready to make prediction!")
#     generate = st.button("Generate")
#     if generate:
#         with st.spinner("Generating..."):
#             y = model(
#                 video[None, ...].to(opt.device),
#                 coordinates[None, ...].to(opt.device),
#             )
#             txt = ctc_decode(y[0])
#             st.text(txt[-1])

# st.info("Author ©️ : Wissem Karous ")
# st.info("Made with ❤️")
/////////////////////

import streamlit as st
import os
import cv2
from utils.demo import load_video, ctc_decode
from utils.two_stream_infer import load_model
from scripts.extract_lip_coordinates import generate_lip_coordinates
import options as opt

st.set_page_config(layout="wide")

model = load_model()

st.title("Lipreading Final Year Project Demo")

st.info(
    "The inference speed is very slow on Huggingface spaces due to it being processed entirely on CPU ",
    icon="ℹ️",
)

# Generating a list of options or videos
options = sorted(os.listdir(os.path.join("app_input")))  # Ensure the list is sorted
selected_video = st.selectbox("Choose video", options)

# Find the index of the selected video and calculate the index of the next video
selected_index = options.index(selected_video)
next_video_index = (selected_index + 1) % len(options)  # Ensures looping back to start
next_video = options[next_video_index]

col1, col2 = st.columns(2)

# Function to display video in a column with resizing
def display_video(column, video_path, video_name):
    cap = cv2.VideoCapture(video_path)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    new_width = 320  # Adjust this value for desired width
    new_height = int((new_width / width) * height)
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        frame = cv2.resize(frame, (new_width, new_height))
        column.image(frame, channels="BGR")
    cap.release()

# Displaying the selected video in the first column
with col1:
    file_path = os.path.join("app_input", selected_video)
    video_name = selected_video.split(".")[0]
    display_video(col1, file_path, video_name)

# Displaying the next video in the second column
with col2:
    st.info("Expected Result !")
    next_file_path = os.path.join("app_input", next_video)
    next_video_name = next_video.split(".")[0]
    display_video(col2, next_file_path, next_video_name)

# Assuming further processing (like generating predictions) is only intended for the first (selected) video
with col1, st.spinner("Processing video..."):
    video, img_p, files = load_video(f"{video_name}.mp4", opt.device)
    coordinates = generate_lip_coordinates(f"{video_name}_samples")
    # Assuming 'frames_generated' and 'coordinates_generated' are used for control flow or further processing
    frames_generated = True
    coordinates_generated = True
    if frames_generated and coordinates_generated:
        st.markdown(f"Frames Generated for {video_name}:\n{files}")
        st.markdown(f"Coordinates Generated for {video_name}:\n{coordinates}")

with col2:
    st.info("Ready to make prediction!")
    generate = st.button("Generate")
    if generate:
        with st.spinner("Generating..."):
            y = model(
                video[None, ...].to(opt.device),
                coordinates[None, ...].to(opt.device),
            )
            txt = ctc_decode(y[0])
            st.text(txt[-1])

st.info("Author ©️ : Wissem Karous ")
st.info("Made with ❤️")