Created
September 9, 2021 14:57
-
-
Save rohan-paul/576ab92b7bbc93f03c3ad023c9d6f73d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Clone GFPGAN and enter the GFPGAN folder | |
| %cd /content | |
| !rm -rf GFPGAN | |
| !git clone https://github.com/TencentARC/GFPGAN.git | |
| %cd GFPGAN | |
| # Set up the environment | |
| # Install basicsr - https://github.com/xinntao/BasicSR | |
| # We use BasicSR for both training and inference | |
| !pip install basicsr | |
| # Install facexlib - https://github.com/xinntao/facexlib | |
| # We use face detection and face restoration helper in the facexlib package | |
| !pip install facexlib | |
| # Install other depencencies | |
| !pip install -r requirements.txt | |
| !python setup.py develop | |
| !pip install realesrgan # used for enhancing the background (non-face) regions | |
| # Download the pre-trained model | |
| !wget https://github.com/TencentARC/GFPGAN/releases/download/v0.2.0/GFPGANCleanv1-NoCE-C2.pth -P experiments/pretrained_models | |
| # upload your own images | |
| import os | |
| from google.colab import files | |
| import shutil | |
| upload_folder = 'inputs/upload' | |
| if os.path.isdir(upload_folder): | |
| shutil.rmtree(upload_folder) | |
| os.mkdir(upload_folder) | |
| # upload images | |
| uploaded = files.upload() | |
| for filename in uploaded.keys(): | |
| dst_path = os.path.join(upload_folder, filename) | |
| print(f'move {filename} to {dst_path}') | |
| shutil.move(filename, dst_path) | |
| # Now we use the GFPGAN to restore the above low-quality images | |
| # We use [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN) for enhancing the background (non-face) regions | |
| !rm -rf results | |
| !python inference_gfpgan.py --upscale 2 --test_path inputs/upload --save_root results --model_path experiments/pretrained_models/GFPGANCleanv1-NoCE-C2.pth --bg_upsampler realesrgan | |
| !ls results/cmp | |
| # We first visualize the cropped faces | |
| # The left are the inputs images; the right are the results of GFPGAN | |
| import cv2 | |
| import matplotlib.pyplot as plt | |
| def display(img1, img2): | |
| fig = plt.figure(figsize=(25, 10)) | |
| ax1 = fig.add_subplot(1, 2, 1) | |
| plt.title('Input image', fontsize=16) | |
| ax1.axis('off') | |
| ax2 = fig.add_subplot(1, 2, 2) | |
| plt.title('GFPGAN output', fontsize=16) | |
| ax2.axis('off') | |
| ax1.imshow(img1) | |
| ax2.imshow(img2) | |
| def imread(img_path): | |
| img = cv2.imread(img_path) | |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| return img | |
| # display each image in the upload folder | |
| import os | |
| import glob | |
| input_folder = 'results/cropped_faces' | |
| result_folder = 'results/restored_faces' | |
| input_list = sorted(glob.glob(os.path.join(input_folder, '*'))) | |
| output_list = sorted(glob.glob(os.path.join(result_folder, '*'))) | |
| for input_path, output_path in zip(input_list, output_list): | |
| img_input = imread(input_path) | |
| img_output = imread(output_path) | |
| display(img_input, img_output) | |
| # download the result | |
| !ls results | |
| print('Download results') | |
| os.system('zip -r download.zip results') | |
| files.download("download.zip") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment