CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ai-forever

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: ai-forever/sber-swap
Path: blob/main/SberSwapInference.ipynb
Views: 792
Kernel: facesh
import cv2 import torch import time import os from utils.inference.image_processing import crop_face, get_final_image, show_images from utils.inference.video_processing import read_video, get_target, get_final_video, add_audio_from_another_video, face_enhancement from utils.inference.core import model_inference from network.AEI_Net import AEI_Net from coordinate_reg.image_infer import Handler from insightface_func.face_detect_crop_multi import Face_detect_crop from arcface_model.iresnet import iresnet100 from models.pix2pix_model import Pix2PixModel from models.config_sr import TestOptions
/home/jovyan/conda/envs/faceshift_env/lib/python3.8/site-packages/kornia/augmentation/augmentation.py:1830: DeprecationWarning: GaussianBlur is no longer maintained and will be removed from the future versions. Please use RandomGaussianBlur instead. warnings.warn(

Load Models

app = Face_detect_crop(name='antelope', root='./insightface_func/models') app.prepare(ctx_id= 0, det_thresh=0.6, det_size=(640,640)) # main model for generation G = AEI_Net(backbone='unet', num_blocks=2, c_id=512) G.eval() G.load_state_dict(torch.load('weights/G_unet_2blocks.pth', map_location=torch.device('cpu'))) G = G.cuda() G = G.half() # arcface model to get face embedding netArc = iresnet100(fp16=False) netArc.load_state_dict(torch.load('arcface_model/backbone.pth')) netArc=netArc.cuda() netArc.eval() # model to get face landmarks handler = Handler('./coordinate_reg/model/2d106det', 0, ctx_id=0, det_size=640) # model to make superres of face, set use_sr=True if you want to use super resolution or use_sr=False if you don't use_sr = False if use_sr: os.environ['CUDA_VISIBLE_DEVICES'] = '0' torch.backends.cudnn.benchmark = True opt = TestOptions() #opt.which_epoch ='10_7' model = Pix2PixModel(opt) model.netG.train()
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)
input mean and std: 127.5 127.5 find model: ./insightface_func/models/antelope/glintr100.onnx recognition find model: ./insightface_func/models/antelope/scrfd_10g_bnkps.onnx detection set det-size: (640, 640) loading ./coordinate_reg/model/2d106det 0 input mean and std: 127.5 127.5 find model: ./insightface_func/models/antelope/glintr100.onnx recognition find model: ./insightface_func/models/antelope/scrfd_10g_bnkps.onnx detection set det-size: (640, 640)

Set here path to source image and video for faceswap

image_to_image = True
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)
""" choose not really long videos, coz it can take a lot of time othervise choose source image as a photo -- preferable a selfie of a person """ if image_to_image: path_to_target = 'examples/images/beckham.jpg' else: path_to_video = 'examples/videos/random_gif.gif' source_full = cv2.imread('examples/images/elon_musk.jpg') OUT_VIDEO_NAME = "examples/results/result.mp4" crop_size = 224 # don't change this BS = 60
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)
# check, if we can detect face on the source image try: source = crop_face(source_full, app, crop_size)[0] source = [source[:, :, ::-1]] print("Everything is ok!") except TypeError: print("Bad source images")
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)
Everything is ok!
# read video and find target image in the video that contains at least 1 face if image_to_image: target_full = cv2.imread(path_to_target) full_frames = [target_full] else: full_frames, fps = read_video(path_to_video) target = get_target(full_frames, app, crop_size)
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)

Model Inference

START_TIME = time.time()
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)
final_frames_list, crop_frames_list, full_frames, tfm_array_list = model_inference(full_frames, source, target, netArc, G, app, set_target = False, crop_size=crop_size, BS=BS)
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code) 100%|██████████| 1/1 [00:00<00:00, 85.92it/s] 1it [00:00, 4056.39it/s] 100%|██████████| 1/1 [00:00<00:00, 20.14it/s] 100%|██████████| 1/1 [00:00<00:00, 20068.44it/s]
if use_sr: final_frames_list = face_enhancement(final_frames_list, model)
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)
if image_to_image: result = get_final_image(final_frames_list, crop_frames_list, full_frames[0], tfm_array_list, handler) show_images([source[0][:, :, ::-1], target_full, result], ['Source Image', 'Target Image', 'Swapped Image'], figsize=(20, 15)) else: get_final_video(final_frames_list, crop_frames_list, full_frames, tfm_array_list, OUT_VIDEO_NAME, fps, handler) add_audio_from_another_video(path_to_video, OUT_VIDEO_NAME, "audio")
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)
Image in a Jupyter notebook
print(f'Full pipeline took {time.time() - START_TIME}') if not image_to_image: print(f"Video saved with path {OUT_VIDEO_NAME}")
Full pipeline took 1.514739751815796
/home/jovyan/.local/lib/python3.8/site-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above. and should_run_async(code)