Daniel Verdu commited on
Commit
608b708
1 Parent(s): 9e08039

first commit in hf_spaces

Browse files
Files changed (1) hide show
  1. deoldify/visualize.py +12 -220
deoldify/visualize.py CHANGED
@@ -1,51 +1,21 @@
1
- from fastai.core import *
2
- from fastai.vision import *
3
- from matplotlib.axes import Axes
4
- from matplotlib.figure import Figure
5
- from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
6
- from .filters import IFilter, MasterFilter, ColorizerFilter
7
- from .generators import gen_inference_deep, gen_inference_wide
8
- # from tensorboardX import SummaryWriter
9
- from scipy import misc
10
- from PIL import Image
11
- # import ffmpeg
12
- # import youtube_dl
13
  import gc
14
  import requests
15
  from io import BytesIO
16
  import base64
17
- # from IPython import display as ipythondisplay
18
- # from IPython.display import HTML
19
- # from IPython.display import Image as ipythonimage
20
- import cv2
 
 
 
 
 
21
 
 
 
22
 
23
- # # adapted from https://www.pyimagesearch.com/2016/04/25/watermarking-images-with-opencv-and-python/
24
- # def get_watermarked(pil_image: Image) -> Image:
25
- # try:
26
- # image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
27
- # (h, w) = image.shape[:2]
28
- # image = np.dstack([image, np.ones((h, w), dtype="uint8") * 255])
29
- # pct = 0.05
30
- # full_watermark = cv2.imread(
31
- # './resource_images/watermark.png', cv2.IMREAD_UNCHANGED
32
- # )
33
- # (fwH, fwW) = full_watermark.shape[:2]
34
- # wH = int(pct * h)
35
- # wW = int((pct * h / fwH) * fwW)
36
- # watermark = cv2.resize(full_watermark, (wH, wW), interpolation=cv2.INTER_AREA)
37
- # overlay = np.zeros((h, w, 4), dtype="uint8")
38
- # (wH, wW) = watermark.shape[:2]
39
- # overlay[h - wH - 10 : h - 10, 10 : 10 + wW] = watermark
40
- # # blend the two images together using transparent overlays
41
- # output = image.copy()
42
- # cv2.addWeighted(overlay, 0.5, output, 1.0, 0, output)
43
- # rgb_image = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
44
- # final_image = Image.fromarray(rgb_image)
45
- # return final_image
46
- # except:
47
- # # Don't want this to crash everything, so let's just not watermark the image for now.
48
- # return pil_image
49
 
50
 
51
  class ModelImageVisualizer:
@@ -245,184 +215,6 @@ class ModelImageVisualizer:
245
  return rows, columns
246
 
247
 
248
- # class VideoColorizer:
249
- # def __init__(self, vis: ModelImageVisualizer):
250
- # self.vis = vis
251
- # workfolder = Path('./video')
252
- # self.source_folder = workfolder / "source"
253
- # self.bwframes_root = workfolder / "bwframes"
254
- # self.audio_root = workfolder / "audio"
255
- # self.colorframes_root = workfolder / "colorframes"
256
- # self.result_folder = workfolder / "result"
257
-
258
- # def _purge_images(self, dir):
259
- # for f in os.listdir(dir):
260
- # if re.search('.*?\.jpg', f):
261
- # os.remove(os.path.join(dir, f))
262
-
263
- # def _get_fps(self, source_path: Path) -> str:
264
- # probe = ffmpeg.probe(str(source_path))
265
- # stream_data = next(
266
- # (stream for stream in probe['streams'] if stream['codec_type'] == 'video'),
267
- # None,
268
- # )
269
- # return stream_data['avg_frame_rate']
270
-
271
- # def _download_video_from_url(self, source_url, source_path: Path):
272
- # if source_path.exists():
273
- # source_path.unlink()
274
-
275
- # ydl_opts = {
276
- # 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
277
- # 'outtmpl': str(source_path),
278
- # 'retries': 30,
279
- # 'fragment-retries': 30
280
- # }
281
- # with youtube_dl.YoutubeDL(ydl_opts) as ydl:
282
- # ydl.download([source_url])
283
-
284
- # def _extract_raw_frames(self, source_path: Path):
285
- # bwframes_folder = self.bwframes_root / (source_path.stem)
286
- # bwframe_path_template = str(bwframes_folder / '%5d.jpg')
287
- # bwframes_folder.mkdir(parents=True, exist_ok=True)
288
- # self._purge_images(bwframes_folder)
289
- # ffmpeg.input(str(source_path)).output(
290
- # str(bwframe_path_template), format='image2', vcodec='mjpeg', qscale=0
291
- # ).run(capture_stdout=True)
292
-
293
- # def _colorize_raw_frames(
294
- # self, source_path: Path, render_factor: int = None, post_process: bool = True,
295
- # watermarked: bool = True,
296
- # ):
297
- # colorframes_folder = self.colorframes_root / (source_path.stem)
298
- # colorframes_folder.mkdir(parents=True, exist_ok=True)
299
- # self._purge_images(colorframes_folder)
300
- # bwframes_folder = self.bwframes_root / (source_path.stem)
301
-
302
- # for img in progress_bar(os.listdir(str(bwframes_folder))):
303
- # img_path = bwframes_folder / img
304
-
305
- # if os.path.isfile(str(img_path)):
306
- # color_image = self.vis.get_transformed_image(
307
- # str(img_path), render_factor=render_factor, post_process=post_process,watermarked=watermarked
308
- # )
309
- # color_image.save(str(colorframes_folder / img))
310
-
311
- # def _build_video(self, source_path: Path) -> Path:
312
- # colorized_path = self.result_folder / (
313
- # source_path.name.replace('.mp4', '_no_audio.mp4')
314
- # )
315
- # colorframes_folder = self.colorframes_root / (source_path.stem)
316
- # colorframes_path_template = str(colorframes_folder / '%5d.jpg')
317
- # colorized_path.parent.mkdir(parents=True, exist_ok=True)
318
- # if colorized_path.exists():
319
- # colorized_path.unlink()
320
- # fps = self._get_fps(source_path)
321
-
322
- # ffmpeg.input(
323
- # str(colorframes_path_template),
324
- # format='image2',
325
- # vcodec='mjpeg',
326
- # framerate=fps,
327
- # ).output(str(colorized_path), crf=17, vcodec='libx264').run(capture_stdout=True)
328
-
329
- # result_path = self.result_folder / source_path.name
330
- # if result_path.exists():
331
- # result_path.unlink()
332
- # # making copy of non-audio version in case adding back audio doesn't apply or fails.
333
- # shutil.copyfile(str(colorized_path), str(result_path))
334
-
335
- # # adding back sound here
336
- # audio_file = Path(str(source_path).replace('.mp4', '.aac'))
337
- # if audio_file.exists():
338
- # audio_file.unlink()
339
-
340
- # os.system(
341
- # 'ffmpeg -y -i "'
342
- # + str(source_path)
343
- # + '" -vn -acodec copy "'
344
- # + str(audio_file)
345
- # + '"'
346
- # )
347
-
348
- # if audio_file.exists:
349
- # os.system(
350
- # 'ffmpeg -y -i "'
351
- # + str(colorized_path)
352
- # + '" -i "'
353
- # + str(audio_file)
354
- # + '" -shortest -c:v copy -c:a aac -b:a 256k "'
355
- # + str(result_path)
356
- # + '"'
357
- # )
358
- # print('Video created here: ' + str(result_path))
359
- # return result_path
360
-
361
- # def colorize_from_url(
362
- # self,
363
- # source_url,
364
- # file_name: str,
365
- # render_factor: int = None,
366
- # post_process: bool = True,
367
- # watermarked: bool = True,
368
-
369
- # ) -> Path:
370
- # source_path = self.source_folder / file_name
371
- # self._download_video_from_url(source_url, source_path)
372
- # return self._colorize_from_path(
373
- # source_path, render_factor=render_factor, post_process=post_process,watermarked=watermarked
374
- # )
375
-
376
- # def colorize_from_file_name(
377
- # self, file_name: str, render_factor: int = None, watermarked: bool = True, post_process: bool = True,
378
- # ) -> Path:
379
- # source_path = self.source_folder / file_name
380
- # return self._colorize_from_path(
381
- # source_path, render_factor=render_factor, post_process=post_process,watermarked=watermarked
382
- # )
383
-
384
- # def _colorize_from_path(
385
- # self, source_path: Path, render_factor: int = None, watermarked: bool = True, post_process: bool = True
386
- # ) -> Path:
387
- # if not source_path.exists():
388
- # raise Exception(
389
- # 'Video at path specfied, ' + str(source_path) + ' could not be found.'
390
- # )
391
- # self._extract_raw_frames(source_path)
392
- # self._colorize_raw_frames(
393
- # source_path, render_factor=render_factor,post_process=post_process,watermarked=watermarked
394
- # )
395
- # return self._build_video(source_path)
396
-
397
-
398
- # def get_video_colorizer(render_factor: int = 21) -> VideoColorizer:
399
- # return get_stable_video_colorizer(render_factor=render_factor)
400
-
401
-
402
- # def get_artistic_video_colorizer(
403
- # root_folder: Path = Path('./'),
404
- # weights_name: str = 'ColorizeArtistic_gen',
405
- # results_dir='result_images',
406
- # render_factor: int = 35
407
- # ) -> VideoColorizer:
408
- # learn = gen_inference_deep(root_folder=root_folder, weights_name=weights_name)
409
- # filtr = MasterFilter([ColorizerFilter(learn=learn)], render_factor=render_factor)
410
- # vis = ModelImageVisualizer(filtr, results_dir=results_dir)
411
- # return VideoColorizer(vis)
412
-
413
-
414
- # def get_stable_video_colorizer(
415
- # root_folder: Path = Path('./'),
416
- # weights_name: str = 'ColorizeVideo_gen',
417
- # results_dir='result_images',
418
- # render_factor: int = 21
419
- # ) -> VideoColorizer:
420
- # learn = gen_inference_wide(root_folder=root_folder, weights_name=weights_name)
421
- # filtr = MasterFilter([ColorizerFilter(learn=learn)], render_factor=render_factor)
422
- # vis = ModelImageVisualizer(filtr, results_dir=results_dir)
423
- # return VideoColorizer(vis)
424
-
425
-
426
  def get_image_colorizer(
427
  root_folder: Path = Path('./'), render_factor: int = 35, artistic: bool = True
428
  ) -> ModelImageVisualizer:
 
1
+ import cv2
 
 
 
 
 
 
 
 
 
 
 
2
  import gc
3
  import requests
4
  from io import BytesIO
5
  import base64
6
+ from scipy import misc
7
+ from PIL import Image
8
+ from matplotlib.axes import Axes
9
+ from matplotlib.figure import Figure
10
+ from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
11
+
12
+ import torch
13
+ from fastai.core import *
14
+ from fastai.vision import *
15
 
16
+ from .filters import IFilter, MasterFilter, ColorizerFilter
17
+ from .generators import gen_inference_deep, gen_inference_wide
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  class ModelImageVisualizer:
 
215
  return rows, columns
216
 
217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  def get_image_colorizer(
219
  root_folder: Path = Path('./'), render_factor: int = 35, artistic: bool = True
220
  ) -> ModelImageVisualizer: