1515from tqdm import tqdm
1616from paddle import fluid
1717from model import build_model
18- from paddle .incubate . hapi .download import get_path_from_url
18+ from paddle .utils .download import get_path_from_url
1919
2020parser = argparse .ArgumentParser (description = 'DeOldify' )
21- parser .add_argument ('--input' , type = str , default = 'none' , help = 'Input video' )
22- parser .add_argument ('--output' , type = str , default = 'output' , help = 'output dir' )
23- parser .add_argument ('--weight_path' , type = str , default = 'none' , help = 'Path to the reference image directory' )
21+ parser .add_argument ('--input' , type = str , default = 'none' , help = 'Input video' )
22+ parser .add_argument ('--output' , type = str , default = 'output' , help = 'output dir' )
23+ parser .add_argument ('--weight_path' ,
24+ type = str ,
25+ default = 'none' ,
26+ help = 'Path to the reference image directory' )
2427
2528DeOldify_weight_url = 'https://paddlegan.bj.bcebos.com/applications/DeOldify_stable.pdparams'
2629
30+
2731def frames_to_video_ffmpeg (framepath , videopath , r ):
2832 ffmpeg = ['ffmpeg ' , ' -loglevel ' , ' error ' ]
2933 cmd = ffmpeg + [
@@ -56,9 +60,9 @@ def __init__(self, input, output, batch_size=1, weight_path=None):
5660 def norm (self , img , render_factor = 32 , render_base = 16 ):
5761 target_size = render_factor * render_base
5862 img = img .resize ((target_size , target_size ), resample = Image .BILINEAR )
59-
63+
6064 img = np .array (img ).transpose ([2 , 0 , 1 ]).astype ('float32' ) / 255.0
61-
65+
6266 img_mean = np .array ([0.485 , 0.456 , 0.406 ]).reshape ((3 , 1 , 1 ))
6367 img_std = np .array ([0.229 , 0.224 , 0.225 ]).reshape ((3 , 1 , 1 ))
6468
@@ -69,13 +73,13 @@ def norm(self, img, render_factor=32, render_base=16):
6973 def denorm (self , img ):
7074 img_mean = np .array ([0.485 , 0.456 , 0.406 ]).reshape ((3 , 1 , 1 ))
7175 img_std = np .array ([0.229 , 0.224 , 0.225 ]).reshape ((3 , 1 , 1 ))
72-
76+
7377 img *= img_std
7478 img += img_mean
7579 img = img .transpose ((1 , 2 , 0 ))
7680
7781 return (img * 255 ).clip (0 , 255 ).astype ('uint8' )
78-
82+
7983 def post_process (self , raw_color , orig ):
8084 color_np = np .asarray (raw_color )
8185 orig_np = np .asarray (orig )
@@ -86,11 +90,11 @@ def post_process(self, raw_color, orig):
8690 final = cv2 .cvtColor (hires , cv2 .COLOR_YUV2BGR )
8791 final = Image .fromarray (final )
8892 return final
89-
93+
9094 def run_single (self , img_path ):
9195 ori_img = Image .open (img_path ).convert ('LA' ).convert ('RGB' )
9296 img = self .norm (ori_img )
93- x = paddle .to_tensor (img [np .newaxis ,...])
97+ x = paddle .to_tensor (img [np .newaxis , ...])
9498 out = self .model (x )
9599
96100 pred_img = self .denorm (out .numpy ()[0 ])
@@ -118,20 +122,20 @@ def run(self):
118122
119123 frames = sorted (glob .glob (os .path .join (out_path , '*.png' )))
120124
121-
122125 for frame in tqdm (frames ):
123126 pred_img = self .run_single (frame )
124127
125128 frame_name = os .path .basename (frame )
126129 pred_img .save (os .path .join (pred_frame_path , frame_name ))
127-
130+
128131 frame_pattern_combined = os .path .join (pred_frame_path , '%08d.png' )
129-
130- vid_out_path = os .path .join (output_path , '{}_deoldify_out.mp4' .format (base_name ))
131- frames_to_video_ffmpeg (frame_pattern_combined , vid_out_path , str (int (fps )))
132132
133- return frame_pattern_combined , vid_out_path
133+ vid_out_path = os .path .join (output_path ,
134+ '{}_deoldify_out.mp4' .format (base_name ))
135+ frames_to_video_ffmpeg (frame_pattern_combined , vid_out_path ,
136+ str (int (fps )))
134137
138+ return frame_pattern_combined , vid_out_path
135139
136140
137141def dump_frames_ffmpeg (vid_path , outpath , r = None , ss = None , t = None ):
@@ -147,21 +151,8 @@ def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):
147151
148152 if ss is not None and t is not None and r is not None :
149153 cmd = ffmpeg + [
150- ' -ss ' ,
151- ss ,
152- ' -t ' ,
153- t ,
154- ' -i ' ,
155- vid_path ,
156- ' -r ' ,
157- r ,
158-
159- ' -qscale:v ' ,
160- ' 0.1 ' ,
161- ' -start_number ' ,
162- ' 0 ' ,
163-
164- outformat
154+ ' -ss ' , ss , ' -t ' , t , ' -i ' , vid_path , ' -r ' , r , ' -qscale:v ' ,
155+ ' 0.1 ' , ' -start_number ' , ' 0 ' , outformat
165156 ]
166157 else :
167158 cmd = ffmpeg + [' -i ' , vid_path , ' -start_number ' , ' 0 ' , outformat ]
@@ -177,11 +168,13 @@ def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):
177168 return out_full_path
178169
179170
180- if __name__ == '__main__' :
171+ if __name__ == '__main__' :
181172 paddle .enable_imperative ()
182173 args = parser .parse_args ()
183174
184- predictor = DeOldifyPredictor (args .input , args .output , weight_path = args .weight_path )
175+ predictor = DeOldifyPredictor (args .input ,
176+ args .output ,
177+ weight_path = args .weight_path )
185178 frames_path , temp_video_path = predictor .run ()
186179
187- print ('output video path:' , temp_video_path )
180+ print ('output video path:' , temp_video_path )
0 commit comments