svjack commited on
Commit
1b38e97
·
verified ·
1 Parent(s): b786d2d

Upload colorize_video.py

Browse files
Files changed (1) hide show
  1. colorize_video.py +74 -0
colorize_video.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ python colorize_video.py xiangling_skt.mp4 xiangling_clr.mp4 --model ECCV16
3
+ '''
4
+
5
+ import os
6
+ import tempfile
7
+ import time
8
+ import argparse
9
+
10
+ import cv2
11
+ import moviepy.editor as mp
12
+ import numpy as np
13
+ from tqdm import tqdm
14
+
15
+ from models.deep_colorization.colorizers import eccv16
16
+ from utils import colorize_frame, change_model
17
+
18
+ def colorize_video(input_path, output_path, model_name):
19
+ # Load the model
20
+ loaded_model = change_model("None", model_name)
21
+
22
+ # Open the video using cv2.VideoCapture
23
+ video = cv2.VideoCapture(input_path)
24
+
25
+ # Get video information
26
+ fps = video.get(cv2.CAP_PROP_FPS)
27
+ total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
28
+
29
+ # Colorize video frames and store in a list
30
+ output_frames = []
31
+
32
+ for _ in tqdm(range(total_frames), unit='frame', desc="Progress"):
33
+ ret, frame = video.read()
34
+ if not ret:
35
+ break
36
+
37
+ colorized_frame = colorize_frame(frame, loaded_model)
38
+ output_frames.append((colorized_frame * 255).astype(np.uint8))
39
+
40
+ # Merge frames to video
41
+ frame_size = output_frames[0].shape[:2]
42
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for MP4 video
43
+ out = cv2.VideoWriter(output_path, fourcc, fps, (frame_size[1], frame_size[0]))
44
+
45
+ for frame in output_frames:
46
+ frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
47
+ out.write(frame_bgr)
48
+
49
+ out.release()
50
+
51
+ # Close the video capture
52
+ video.release()
53
+
54
+ # Convert the output video to a format compatible with Streamlit
55
+ clip = mp.VideoFileClip(output_path)
56
+
57
+ # Write the video file
58
+ #clip.write_videofile(output_path, codec="libx264", audio=audio is not None)
59
+ clip.write_videofile(output_path)
60
+
61
+ print(f"Colorized video saved to {output_path}")
62
+
63
+ def main():
64
+ parser = argparse.ArgumentParser(description="Colorize a black and white video.")
65
+ parser.add_argument("input_path", type=str, help="Path to the input video file.")
66
+ parser.add_argument("output_path", type=str, help="Path to save the colorized video file.")
67
+ parser.add_argument("--model", type=str, default="ECCV16", choices=["ECCV16", "SIGGRAPH17"], help="Model to use for colorization.")
68
+
69
+ args = parser.parse_args()
70
+
71
+ colorize_video(args.input_path, args.output_path, args.model)
72
+
73
+ if __name__ == "__main__":
74
+ main()