YANGYYYY commited on
Commit
e567105
1 Parent(s): 7de1788

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +11 -79
inference.py CHANGED
@@ -164,89 +164,21 @@ class Predictor:
164
  fname = fname.replace(f'.{ext}', '')
165
  cv2.imwrite(os.path.join(dest_dir, f'{fname}.jpg'), anime_img[..., ::-1])
166
 
167
- def transform_video_yuan(self, input_path, output_path, batch_size=4, start=0, end=0):
168
-
169
- '''
170
- Transform a video to animation version
171
- https://github.com/lengstrom/fast-style-transfer/blob/master/evaluate.py#L21
172
- '''
173
- # Force to None
174
- end = end or None
175
-
176
- if not os.path.isfile(input_path):
177
- raise FileNotFoundError(f'{input_path} does not exist')
178
-
179
- output_dir = "/".join(output_path.split("/")[:-1])
180
- os.makedirs(output_dir, exist_ok=True)
181
- is_gg_drive = '/drive/' in output_path
182
- temp_file = ''
183
- #output_file = open(output_path, 'wb')
184
-
185
- if is_gg_drive:
186
- # Writing directly into google drive can be inefficient
187
- temp_file = f'tmp_anime.{output_path.split(".")[-1]}'
188
-
189
- def transform_and_write(frames, count, writer):
190
- anime_images = self.transform(frames)
191
- for i in range(0, count):
192
- img = np.clip(anime_images[i], 0, 255)
193
- writer.write_frame(img)
194
-
195
- video_clip = VideoFileClip(input_path, audio=False)
196
- if start or end:
197
- video_clip = video_clip.subclip(start, end)
198
-
199
- video_writer = ffmpeg_writer.FFMPEG_VideoWriter(
200
- output_path,
201
- video_clip.size, video_clip.fps, codec="libx264",
202
- # preset="medium", bitrate="2000k",
203
- audiofile=input_path, threads=None,
204
- ffmpeg_params=None)
205
-
206
- total_frames = round(video_clip.fps * video_clip.duration)
207
- print(f'Transfroming video {input_path}, {total_frames} frames, size: {video_clip.size}')
208
-
209
- batch_shape = (batch_size, video_clip.size[1], video_clip.size[0], 3)
210
- frame_count = 0
211
- frames = np.zeros(batch_shape, dtype=np.float32)
212
- for frame in tqdm(video_clip.iter_frames()):
213
- try:
214
- frames[frame_count] = frame
215
- frame_count += 1
216
- if frame_count == batch_size:
217
- transform_and_write(frames, frame_count, video_writer)
218
- frame_count = 0
219
- except Exception as e:
220
- print(e)
221
- break
222
-
223
-
224
- # The last frames
225
- if frame_count != 0:
226
- transform_and_write(frames, frame_count, video_writer)
227
-
228
- if temp_file:
229
- # move to output path
230
- shutil.move(temp_file, output_path)
231
-
232
- print(f'Animation video saved to {output_path}')
233
- video_writer.close()
234
- #output_file.close()
235
 
236
 
237
  def transform_video(self, input_path, output_path, batch_size=4, start=0, end=0):
238
  end = end or None
239
 
240
- if not os.path.isfile(input_path):
241
- raise FileNotFoundError(f'{input_path} does not exist')
242
 
243
- output_dir = "/".join(output_path.split("/")[:-1])
244
- os.makedirs(output_dir, exist_ok=True)
245
- is_gg_drive = '/drive/' in output_path
246
- temp_file = ''
247
 
248
- if is_gg_drive:
249
- temp_file = f'tmp_anime.{output_path.split(".")[-1]}'
250
 
251
  def transform_and_write(frames, count, writer):
252
  anime_images = self.transform(frames)
@@ -291,10 +223,10 @@ class Predictor:
291
  video_capture.release()
292
  video_writer.release()
293
 
294
- if temp_file:
295
- shutil.move(temp_file, output_path)
296
 
297
- print(f'Animation video saved to {output_path}')
298
 
299
  def transform_video1(self, video, batch_size, start, end):
300
  #end = end or None
 
164
  fname = fname.replace(f'.{ext}', '')
165
  cv2.imwrite(os.path.join(dest_dir, f'{fname}.jpg'), anime_img[..., ::-1])
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
 
169
  def transform_video(self, input_path, output_path, batch_size=4, start=0, end=0):
170
  end = end or None
171
 
172
+ # if not os.path.isfile(input_path):
173
+ # raise FileNotFoundError(f'{input_path} does not exist')
174
 
175
+ # output_dir = "/".join(output_path.split("/")[:-1])
176
+ # os.makedirs(output_dir, exist_ok=True)
177
+ # is_gg_drive = '/drive/' in output_path
178
+ # temp_file = ''
179
 
180
+ # if is_gg_drive:
181
+ # temp_file = f'tmp_anime.{output_path.split(".")[-1]}'
182
 
183
  def transform_and_write(frames, count, writer):
184
  anime_images = self.transform(frames)
 
223
  video_capture.release()
224
  video_writer.release()
225
 
226
+ # if temp_file:
227
+ # shutil.move(temp_file, output_path)
228
 
229
+ # print(f'Animation video saved to {output_path}')
230
 
231
  def transform_video1(self, video, batch_size, start, end):
232
  #end = end or None