ledetele commited on
Commit
00afff7
·
1 Parent(s): e4c9e3b

Rename pages/02_🎥_Input_Youtube_Link.py to pages/02_Input_Youtube_Link.py

Browse files
pages/02_Input_Youtube_Link.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import cv2
4
+ import moviepy.editor as mp
5
+ import numpy as np
6
+ import streamlit as st
7
+ from pytube import YouTube
8
+ from streamlit_lottie import st_lottie
9
+ from tqdm import tqdm
10
+
11
+ from models.deep_colorization.colorizers import eccv16
12
+ from utils import colorize_frame, format_time
13
+ from utils import load_lottieurl, change_model
14
+
15
+ #st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")
16
+
17
+
18
+ loaded_model = eccv16(pretrained=True).eval()
19
+ current_model = "None"
20
+
21
+ st.title("Image & Video Colorizer")
22
+
23
+ st.write("""
24
+ ##### Input a YouTube black and white video link and get a colorized version of it.
25
+ ###### ➠ This space is using CPU Basic so it might take a while to colorize a video.
26
+ ###### ➠ If you want more models and GPU available please support this space by donating.""")
27
+
28
+ @st.cache_data()
29
+ def download_video(link):
30
+ yt = YouTube(link)
31
+ video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename="video.mp4")
32
+ return video
33
+
34
+
35
+ def main():
36
+ model = st.selectbox(
37
+ "Select Model (Both models have their pros and cons, I recommend trying both and keeping the best for you task)",
38
+ ["ECCV16", "SIGGRAPH17"], index=0)
39
+
40
+ loaded_model = change_model(current_model, model)
41
+ st.write(f"Model is now {model}")
42
+
43
+ link = st.text_input("YouTube Link (The longer the video, the longer the processing time)")
44
+ if st.button("Colorize"):
45
+ if link is not "":
46
+ print(link)
47
+ yt_video = download_video(link)
48
+ print(yt_video)
49
+ col1, col2 = st.columns([0.5, 0.5])
50
+ with col1:
51
+ st.markdown('<p style="text-align: center;">Before</p>', unsafe_allow_html=True)
52
+ st.video(yt_video)
53
+ with col2:
54
+ st.markdown('<p style="text-align: center;">After</p>', unsafe_allow_html=True)
55
+ with st.spinner("Colorizing frames..."):
56
+ # Colorize video frames and store in a list
57
+ output_frames = []
58
+
59
+ audio = mp.AudioFileClip("video.mp4")
60
+ video = cv2.VideoCapture("video.mp4")
61
+
62
+ total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
63
+ fps = video.get(cv2.CAP_PROP_FPS)
64
+
65
+ progress_bar = st.progress(0) # Create a progress bar
66
+ start_time = time.time()
67
+ time_text = st.text("Time Remaining: ") # Initialize text value
68
+
69
+ for _ in tqdm(range(total_frames), unit='frame', desc="Progress"):
70
+ ret, frame = video.read()
71
+ if not ret:
72
+ break
73
+
74
+ colorized_frame = colorize_frame(frame, loaded_model)
75
+ output_frames.append((colorized_frame * 255).astype(np.uint8))
76
+
77
+ elapsed_time = time.time() - start_time
78
+ frames_completed = len(output_frames)
79
+ frames_remaining = total_frames - frames_completed
80
+ time_remaining = (frames_remaining / frames_completed) * elapsed_time
81
+
82
+ progress_bar.progress(frames_completed / total_frames) # Update progress bar
83
+
84
+ if frames_completed < total_frames:
85
+ time_text.text(f"Time Remaining: {format_time(time_remaining)}") # Update text value
86
+ else:
87
+ time_text.empty() # Remove text value
88
+ progress_bar.empty()
89
+
90
+ with st.spinner("Merging frames to video..."):
91
+ frame_size = output_frames[0].shape[:2]
92
+ output_filename = "output.mp4"
93
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for MP4 video
94
+ out = cv2.VideoWriter(output_filename, fourcc, fps, (frame_size[1], frame_size[0]))
95
+
96
+ # Display the colorized video using st.video
97
+ for frame in output_frames:
98
+ frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
99
+
100
+ out.write(frame_bgr)
101
+
102
+ out.release()
103
+
104
+ # Convert the output video to a format compatible with Streamlit
105
+ converted_filename = "converted_output.mp4"
106
+ clip = mp.VideoFileClip(output_filename)
107
+ clip = clip.set_audio(audio)
108
+
109
+ clip.write_videofile(converted_filename, codec="libx264")
110
+
111
+ # Display the converted video using st.video()
112
+ st.video(converted_filename)
113
+ st.balloons()
114
+
115
+ # Add a download button for the colorized video
116
+ st.download_button(
117
+ label="Download Colorized Video",
118
+ data=open(converted_filename, "rb").read(),
119
+ file_name="colorized_video.mp4"
120
+ )
121
+
122
+ # Close and delete the temporary file after processing
123
+ video.release()
124
+ else:
125
+ st.warning('Please Type a link', icon="⚠️")
126
+
127
+
128
+ if __name__ == "__main__":
129
+ main()
130
+
pages/02_🎥_Input_Youtube_Link.py DELETED
@@ -1,138 +0,0 @@
1
- import time
2
-
3
- import cv2
4
- import moviepy.editor as mp
5
- import numpy as np
6
- import streamlit as st
7
- from pytube import YouTube
8
- from streamlit_lottie import st_lottie
9
- from tqdm import tqdm
10
-
11
- from models.deep_colorization.colorizers import eccv16
12
- from utils import colorize_frame, format_time
13
- from utils import load_lottieurl, change_model
14
-
15
- st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")
16
-
17
-
18
- loaded_model = eccv16(pretrained=True).eval()
19
- current_model = "None"
20
-
21
-
22
- col1, col2 = st.columns([1, 3])
23
- with col1:
24
- lottie = load_lottieurl("https://assets5.lottiefiles.com/packages/lf20_RHdEuzVfEL.json")
25
- st_lottie(lottie)
26
-
27
- with col2:
28
- st.write("""
29
- ## B&W Videos Colorizer
30
- ##### Input a YouTube black and white video link and get a colorized version of it.
31
- ###### ➠ This space is using CPU Basic so it might take a while to colorize a video.
32
- ###### ➠ If you want more models and GPU available please support this space by donating.""")
33
-
34
-
35
- @st.cache_data()
36
- def download_video(link):
37
- yt = YouTube(link)
38
- video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename="video.mp4")
39
- return video
40
-
41
-
42
- def main():
43
- model = st.selectbox(
44
- "Select Model (Both models have their pros and cons, I recommend trying both and keeping the best for you task)",
45
- ["ECCV16", "SIGGRAPH17"], index=0)
46
-
47
- loaded_model = change_model(current_model, model)
48
- st.write(f"Model is now {model}")
49
-
50
- link = st.text_input("YouTube Link (The longer the video, the longer the processing time)")
51
- if st.button("Colorize"):
52
- yt_video = download_video(link)
53
- print(yt_video)
54
- col1, col2 = st.columns([0.5, 0.5])
55
- with col1:
56
- st.markdown('<p style="text-align: center;">Before</p>', unsafe_allow_html=True)
57
- st.video(yt_video)
58
- with col2:
59
- st.markdown('<p style="text-align: center;">After</p>', unsafe_allow_html=True)
60
- with st.spinner("Colorizing frames..."):
61
- # Colorize video frames and store in a list
62
- output_frames = []
63
-
64
- audio = mp.AudioFileClip("video.mp4")
65
- video = cv2.VideoCapture("video.mp4")
66
-
67
- total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
68
- fps = video.get(cv2.CAP_PROP_FPS)
69
-
70
- progress_bar = st.progress(0) # Create a progress bar
71
- start_time = time.time()
72
- time_text = st.text("Time Remaining: ") # Initialize text value
73
-
74
- for _ in tqdm(range(total_frames), unit='frame', desc="Progress"):
75
- ret, frame = video.read()
76
- if not ret:
77
- break
78
-
79
- colorized_frame = colorize_frame(frame, loaded_model)
80
- output_frames.append((colorized_frame * 255).astype(np.uint8))
81
-
82
- elapsed_time = time.time() - start_time
83
- frames_completed = len(output_frames)
84
- frames_remaining = total_frames - frames_completed
85
- time_remaining = (frames_remaining / frames_completed) * elapsed_time
86
-
87
- progress_bar.progress(frames_completed / total_frames) # Update progress bar
88
-
89
- if frames_completed < total_frames:
90
- time_text.text(f"Time Remaining: {format_time(time_remaining)}") # Update text value
91
- else:
92
- time_text.empty() # Remove text value
93
- progress_bar.empty()
94
-
95
- with st.spinner("Merging frames to video..."):
96
- frame_size = output_frames[0].shape[:2]
97
- output_filename = "output.mp4"
98
- fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for MP4 video
99
- out = cv2.VideoWriter(output_filename, fourcc, fps, (frame_size[1], frame_size[0]))
100
-
101
- # Display the colorized video using st.video
102
- for frame in output_frames:
103
- frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
104
-
105
- out.write(frame_bgr)
106
-
107
- out.release()
108
-
109
- # Convert the output video to a format compatible with Streamlit
110
- converted_filename = "converted_output.mp4"
111
- clip = mp.VideoFileClip(output_filename)
112
- clip = clip.set_audio(audio)
113
-
114
- clip.write_videofile(converted_filename, codec="libx264")
115
-
116
- # Display the converted video using st.video()
117
- st.video(converted_filename)
118
- st.balloons()
119
-
120
- # Add a download button for the colorized video
121
- st.download_button(
122
- label="Download Colorized Video",
123
- data=open(converted_filename, "rb").read(),
124
- file_name="colorized_video.mp4"
125
- )
126
-
127
- # Close and delete the temporary file after processing
128
- video.release()
129
-
130
-
131
- if __name__ == "__main__":
132
- main()
133
- st.markdown(
134
- "###### Made with :heart: by [Clément Delteil](https://www.linkedin.com/in/clementdelteil/) [![this is an "
135
- "image link](https://i.imgur.com/thJhzOO.png)](https://www.buymeacoffee.com/clementdelteil)")
136
- st.markdown(
137
- "###### [Blog post of the project](https://medium.com/geekculture/creating-a-web-app-to-colorize-images-and-youtube-videos-80f5be2d0f68)"
138
- )