top of page
Writer's pictureHackers Realm

Revive your Old Images and Videos with DeOldify | Deep Learning | Project Tutorial

Updated: May 30, 2023

Revive your old images and videos with colorization is a project to colorize old images and videos using DeOldify . DeOldify is an open-source software tool that uses deep learning to colorize and restore old black-and-white images and videos. It uses a combination of convolutional neural networks (CNNs) and generative adversarial networks (GANs) to colorize and restore old media.


Colorize Images & Videos using Deoldify
Colorize Images & Videos using Deoldify

In this project, we have used pretrained DeOldify models that are designed for colorizing black and white images and videos. To colorize an image or video using DeOldify, the user feeds the black and white image or video to the colorization GAN, and the generator outputs a colorized version of the input. The output can then be saved as a new color image or video file.


You can watch the video-based tutorial with a step-by-step explanation down below.


Download Models


First we need to download the pretrained DeOldify models required for this project

!wget https://data.deepai.org/deoldify/ColorizeArtistic_gen.pth -O ./models/ColorizeArtistic_gen.pth
  • ColorizeArtistic_gen.pth - is a pre-trained DeOldify model that is specifically designed for artistic colorization of images. Using this model, you can easily add color to your old photos and create stunning artistic effects that capture the beauty of the original images in a whole new way.


!wget https://data.deepai.org/deoldify/ColorizeVideo_gen.pth -O ./models/ColorizeVideo_gen.pth
  • ColorizeVideo_gen.pth - is a pre-trained DeOldify model that is specifically designed for colorizing videos. Using this model , you can easily add color to your old black and white videos and bring them to life in a way that captures the beauty and detail of the original scenes.



Import Modules


from deoldify import device
from deoldify.device_id import DeviceId

device.set(device=DeviceId.GPU0)
  • deoldify - used to colorize old images and videos.

  • deoldify.device_id - is a setting in the DeOldify software tool that specifies the ID of the GPU device that will be used for running the colorization process. This setting allows the user to specify which GPU device should be used if multiple GPUs are available on their system.


from deoldify.visualize import *
import warnings
warnings.filterwarnings('ignore')
  • deoldify.visualize - used to visualize the colorization process and compare the results of different models or settings.

  • warnings - used to control and suppress warning messages that may be generated by the Python interpreter or third-party libraries during the execution of a Python program.


Colorizing black and white images


Next we will import downloaded pretrained model for colorization of images

colorizer = get_image_colorizer(artistic=True)
  • get_image_colorizer() will import the ColorizeArtistic_gen.pth model that we have downloaded earlier.

  • This function takes one optional argument artistic which is a Boolean value that determines which colorization model to use - if True, the function returns a colorizer object that uses an artistic model, while if False, the function returns a colorizer object that uses a more modern, "2020" model.


Next we will feed the black and white image that has to be colorized

render_factor = 35
image_path = colorizer.plot_transformed_image(path='/content/bw_test1.jpg', render_factor=render_factor, compare=True)
show_image_in_notebook(image_path)
  • First, we have to set the render factor to a optimal value. This value controls the size of the output image during the colorization process. If the input image is very large and the user wants a high-quality output image, a larger render factor value may be appropriate. On the other hand, if the input image is small and the user wants a quick result, a smaller render factor value can be set. Here, we have set the render factor value to 35.

  • We have to provide path of black & white image which needs to be colorized. Here we have provided image from local machine .

  • plot_transformed_image() allows to provide a local image file path . User can also provide a image URL instead of a local file that can be done using plot_transformed_image_from_url().

  • This will transform the black and white image to color image.


Input b/w image :

Boats Black & White Image
Boats Black & White Image


Transformed image :

Boats Colorized Image
Boats Colorized Image


Next we will test the code with few more black and white images to check the accuracy and performance of the model .

render_factor = 35
image_path = colorizer.plot_transformed_image(path='/content/bw_test2.jpg', render_factor=render_factor, compare=True)
show_image_in_notebook(image_path)

Input b/w Image :

Forest path Gray scale Image
Forest path Gray scale Image


Transformed Image :

Forest path Colorized Image
Forest path Colorized Image


render_factor = 35
image_path = colorizer.plot_transformed_image(path='/content/bw_test3.jpg', render_factor=render_factor, compare=True)
show_image_in_notebook(image_path)

Input b/w image :

City view B/W Image
City view B/W Image


Transformed Image :

City view Colorized Image
City view Colorized Image

Colorizing black and white videos


Next we will import downloaded pretrained model for colorization of Videos

video_colorizer = get_video_colorizer()
  • get_video_colorizer() will import the ColorizeVideo_gen.pth model that we have downloaded earlier.



Next we will feed the black and white video that has to be colorized

render_factor = 35
video_path = video_colorizer.colorize_from_file_name(file_name='/content/bw_test_video.mp4', render_factor=render_factor)
show_video_in_notebook(video_path)
  • First, we have to set the render factor as we did it for coloring black and white images.

  • This will process the video frame by frame and will render the frames.

  • We have to provide a black & white video path which needs to be colorized. Here we have provided video path saved on local machine.

  • colorize_from_file_name() allows to provide a local video file path . User can also provide a URL instead of a local file that can be done using colorize_from_url().

  • This will transform the black and white video to color video.


Input b/w video :



Transformed video :



Final Thoughts

  • DeOldify is a powerful and easy-to-use software tool for colorizing old images and videos. Its pre-trained models, including an artistic model and a stable model, can produce high-quality and visually pleasing results with minimal effort.

  • The tool also provides various configuration options to fine-tune the colorization process, such as the render factor and the watermark.

  • However, it is important to note that the quality of the colorization output depends on the quality of the input images and videos, as well as the appropriate selection of the pre-trained model and configuration options.


In this project tutorial, we have explored the DeOldify software for colorizing old images and videos and help bring new life to historical artifacts and memories.

Get the project notebook from here Thanks for reading the article!!! Check out more project videos from the YouTube channel Hackers Realm

Komentáre


bottom of page