Skip to content

Instantly share code, notes, and snippets.

@maulvi
Created July 30, 2023 11:36
Show Gist options
  • Save maulvi/7df55a564ff3e38ef56b27a30a790dc9 to your computer and use it in GitHub Desktop.
Save maulvi/7df55a564ff3e38ef56b27a30a790dc9 to your computer and use it in GitHub Desktop.
SimSwap_videos
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "0Qzzx2UpDkqw"
},
"source": [
"## Prepare code"
]
},
{
"cell_type": "code",
"metadata": {
"id": "VA_4CeWZCHLP"
},
"source": [
"%cd /content\n",
"!rm -rf /content/sample_data/\n",
"!git clone https://github.com/neuralchen/SimSwap.git\n",
"%cd /content/SimSwap/"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Y5K4au_UCkKn"
},
"source": [
"!pip install -q torch==1.13.1 torchvision==0.14.1 torchtext==0.14.1 torchaudio==0.13.1 torchdata==0.5.1\n",
"!pip install -q insightface==0.2.1 onnxruntime moviepy > /dev/null\n",
"!pip install -q googledrivedownloader > /dev/null\n",
"!pip install -q onnxruntime-gpu==1.13.1\n",
"!wget -q https://gist.github.com/maulvi/e8e845d87a0a88abe89a3bc465c56e33/raw/5342a5650e6450882b7e28158802bce4018d38ae/model_zoo.py -O /usr/local/lib/python3.10/dist-packages/insightface/model_zoo/model_zoo.py"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ATb7G4P8qC4X"
},
"source": [
"%cd /content/SimSwap\n",
"from google_drive_downloader import GoogleDriveDownloader\n",
"\n",
"!wget -P ./arcface_model https://github.com/woctezuma/SimSwap-colab/releases/download/1.0/arcface_checkpoint.tar\n",
"!wget https://github.com/neuralchen/SimSwap/releases/download/1.0/checkpoints.zip\n",
"!wget -P ./parsing_model/checkpoint https://github.com/neuralchen/SimSwap/releases/download/1.0/79999_iter.pth\n",
"!wget https://github.com/neuralchen/SimSwap/releases/download/512_beta/512.zip\n",
"!wget --no-check-certificate https://sh23tw.dm.files.1drv.com/y4mmGiIkNVigkSwOKDcV3nwMJulRGhbtHdkheehR5TArc52UjudUYNXAEvKCii2O5LAmzGCGK6IfleocxuDeoKxDZkNzDRSt4ZUlEt8GlSOpCXAFEkBwaZimtWGDRbpIGpb_pz9Nq5jATBQpezBS6G_UtspWTkgrXHHxhviV2nWy8APPx134zOZrUIbkSF6xnsqzs3uZ_SEX_m9Rey0ykpx9w -O antelope.zip\n",
"!unzip ./checkpoints.zip -d ./checkpoints\n",
"!unzip 512.zip -d ./checkpoints\n",
"!unzip antelope.zip -d ./insightface_func/models/\n",
"%cd /content\n",
"#!wget https://i.imgur.com/iQtmj1N.png -O photo.png\n",
"#!wget http://i.imgur.com/yCD2Uxm.gif -O video.gif"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!mkdir /content/mnt && apt install fuse3 nfs-client davfs2 -y"
],
"metadata": {
"id": "GeiMAMwRGS0v"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!mount -t davfs http://216.181.107.189:52272/ /content/mnt"
],
"metadata": {
"id": "fa7q2EBKHXnx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!mount -t cifs -o vers=3.0,username=labrax,password=lol///,uid=$(id -u),gid=$(id -g) //216.181.107.189/share /content/samba"
],
"metadata": {
"id": "smyDjU-JBiQY"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!cp /content/mnt/ss.mkv /content"
],
"metadata": {
"id": "q7g0iG6qCa3e"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RVcsLmEwrUhO"
},
"source": [
"## Prepare models"
]
},
{
"cell_type": "code",
"source": [
"!cd /content && wget \"https://v14.erome.com/782/z4iFPGoz/ULN3aE0y_720p.mp4\" --referer \"https://www.erome.com/\""
],
"metadata": {
"id": "7POtzy0EUHDb"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"input_image_fname = '/content/photo.png'\n",
"input_video_fname = '/content/video.gif'"
],
"metadata": {
"id": "Cp_vX7JXs6-u"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"input_image_fname = '/content/1660831518661.jpg'\n",
"input_video_fname = '/content/bn.mp4'"
],
"metadata": {
"id": "oWixcnF1Dw1E"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i /content/drive/MyDrive/Ssis-372.mp4 -c:v h264_nvenc -filter:v fps=30 -preset slow /content/o.mp4"
],
"metadata": {
"id": "1C3tKL2ZQRAm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content/SimSwap/\n",
"!python /content/SimSwap/test_wholeimage_swapsingle.py --crop_size 224 --no_simswaplogo --use_mask --name people --Arc_path arcface_model/arcface_checkpoint.tar --pic_a_path $input_image_fname --pic_b_path $input_video_fname --output_path /content"
],
"metadata": {
"id": "b_5lK0fZwFZ4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!rm -rf /content/tmp/*"
],
"metadata": {
"id": "5eyZYdpBWkJk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content/SimSwap/\n",
"!python /content/SimSwap/test_video_swapsingle.py --crop_size 224 --use_mask --no_simswaplogo --name people --Arc_path /content/SimSwap/arcface_model/arcface_checkpoint.tar --pic_a_path $input_image_fname --video_path $input_video_fname --output_path /content/simswapout.mp4 --temp_path /content/tmp"
],
"metadata": {
"id": "KxpJSwu2aXas"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "PfSsND36EMvn"
},
"source": [
"%cd /content/SimSwap/\n",
"import cv2\n",
"import torch\n",
"import fractions\n",
"import numpy as np\n",
"from PIL import Image\n",
"import torch.nn.functional as F\n",
"from torchvision import transforms\n",
"from models.models import create_model\n",
"from options.test_options import TestOptions\n",
"from insightface_func.face_detect_crop_multi import Face_detect_crop\n",
"from util.videoswap import video_swap\n",
"from util.add_watermark import watermark_image\n",
"transformer = transforms.Compose([\n",
" transforms.ToTensor(),\n",
" #transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n",
" ])\n",
"\n",
"transformer_Arcface = transforms.Compose([\n",
" transforms.ToTensor(),\n",
" transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n",
" ])\n",
"\n",
"detransformer = transforms.Compose([\n",
" transforms.Normalize([0, 0, 0], [1/0.229, 1/0.224, 1/0.225]),\n",
" transforms.Normalize([-0.485, -0.456, -0.406], [1, 1, 1])\n",
" ])\n",
"# If the algorithm misses some faces, you could lower the detection threshold.\n",
"# Reference: https://github.com/neuralchen/SimSwap/issues/39#issuecomment-873758730\n",
"\n",
"det_thresh = 0.4\n",
"\n",
"# You could also decrease the image size used for face detection:\n",
"\n",
"det_size = (512,512)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "rxSbZ2EDNDlf"
},
"source": [
"# Either 224 (okay) or 512 (experimental)\n",
"crop_size = 224"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "wwJOwR9LNKRz"
},
"source": [
"%cd /content/SimSwap/\n",
"start_epoch, epoch_iter = 1, 0\n",
"\n",
"opt = TestOptions()\n",
"opt.initialize()\n",
"opt.parser.add_argument('-f') ## dummy arg to avoid bug\n",
"opt = opt.parse()\n",
"opt.pic_a_path = input_image_fname ## or replace it with image from your own google drive\n",
"opt.video_path = input_video_fname ## or replace it with video from your own google drive\n",
"opt.output_path = '/content/simswapout.mp4'\n",
"opt.temp_path = '/content/tmp'\n",
"opt.Arc_path = './arcface_model/arcface_checkpoint.tar'\n",
"opt.isTrain = False\n",
"opt.no_simswaplogo = True\n",
"opt.use_mask = True\n",
"opt.crop_size = crop_size\n",
"\n",
"if crop_size == 512:\n",
" opt.which_epoch = 550000\n",
" opt.name = '512'\n",
" mode = 'ffhq'\n",
"else:\n",
" mode = 'None'\n",
"\n",
"torch.nn.Module.dump_patches = True\n",
"model = create_model(opt)\n",
"model.eval()\n",
"\n",
"\n",
"app = Face_detect_crop(name='antelope', root='./insightface_func/models')\n",
"app.prepare(ctx_id= 0, det_thresh=det_thresh, det_size=det_size, mode=mode)\n",
"\n",
"pic_a = opt.pic_a_path\n",
"# img_a = Image.open(pic_a).convert('RGB')\n",
"img_a_whole = cv2.imread(pic_a)\n",
"img_a_align_crop, _ = app.get(img_a_whole,crop_size)\n",
"img_a_align_crop_pil = Image.fromarray(cv2.cvtColor(img_a_align_crop[0],cv2.COLOR_BGR2RGB))\n",
"img_a = transformer_Arcface(img_a_align_crop_pil)\n",
"img_id = img_a.view(-1, img_a.shape[0], img_a.shape[1], img_a.shape[2])\n",
"\n",
"# convert numpy to tensor\n",
"img_id = img_id.cuda()\n",
"\n",
"#create latent id\n",
"img_id_downsample = F.interpolate(img_id, size=(112,112))\n",
"latend_id = model.netArc(img_id_downsample)\n",
"latend_id = F.normalize(latend_id, p=2, dim=1)\n",
"\n",
"try:\n",
" video_swap(opt.video_path, latend_id, model, app, opt.output_path,temp_results_dir=opt.temp_path,\\\n",
" no_simswaplogo=opt.no_simswaplogo,use_mask=opt.use_mask,crop_size=crop_size)\n",
"except IndexError:\n",
" print('[error] This is most likely due to the absence of audio from a GIF input.')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"session = onnxruntime.InferenceSession(self.onnx_file, providers=[ 'CUDAExecutionProvider', 'CPUExecutionProvider'])"
],
"metadata": {
"id": "a7RLb65sMCPK"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!wget -q https://gist.github.com/maulvi/e8e845d87a0a88abe89a3bc465c56e33/raw/5342a5650e6450882b7e28158802bce4018d38ae/model_zoo.py -O /usr/local/lib/python3.10/dist-packages/insightface/model_zoo/model_zoo.py"
],
"metadata": {
"id": "KD0BCBmN1nhL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!cd /content/SimSwap/arcface_model && { curl -O http://216.181.107.189:50800/vggface2_crop_arcfacealign_224.tar ; cd -; }"
],
"metadata": {
"id": "GSxiRhS5XiOR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"id": "TtQoI4EHL7dZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install ninja\n",
"%cd /content\n",
"!git clone https://github.com/yangxy/GPEN.git\n",
"\n",
"# Remove the example input\n",
"%rm -rf /content/GPEN/examples\n",
"%mkdir -p /content/GPEN/examples/imgs"
],
"metadata": {
"id": "mig0Wl88nsZw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!mkdir -p /content/GPEN/weights\n",
"%cd /content/GPEN/weights\n",
"!wget https://huggingface.co/akhaliq/RetinaFace-R50/resolve/main/RetinaFace-R50.pth -O RetinaFace-R50.pth\n",
"!wget https://huggingface.co/akhaliq/GPEN-BFR-512/resolve/main/GPEN-BFR-512.pth -O GPEN-BFR-512.pth\n",
"!wget https://huggingface.co/akhaliq/realesrnet_x2/resolve/main/realesrnet_x2.pth -O realesrnet_x2.pth\n",
"!wget https://huggingface.co/akhaliq/ParseNet-latest/resolve/main/ParseNet-latest.pth -O ParseNet-latest.pth\n",
"\n",
"#!wget https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/realesrnet_x1.pth -O /content/GPEN/weights/realesrnet_x1.pth"
],
"metadata": {
"id": "_HTBkjXvAtF3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!rm -rf /content/outs-bfr/*"
],
"metadata": {
"id": "WfVGGwOVClGR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content/GPEN\n",
"!python demo.py --task FaceEnhancement --model GPEN-BFR-512 --in_size 512 --channel_multiplier 2 --narrow 1 --use_sr --sr_scale 2 --use_cuda --indir /content/tmp --outdir /content/outs-bfr"
],
"metadata": {
"id": "PDSwxZY2MLz4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!rm -rf /content/outs-bfr/*COMP.jpg"
],
"metadata": {
"id": "4u_5-iTWuUMF"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!ffmpeg -i /content/simswapout.mp4 -vn -acodec copy /content/output-audio.aac -loglevel quiet"
],
"metadata": {
"id": "zEl82Fsa6XVL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!ffmpeg -f image2 -framerate 30 -i /content/outs-bfr/frame_%07d_GPEN.jpg -c:v hevc_nvenc -preset slow -qp 8 /content/o.mp4 -loglevel quiet"
],
"metadata": {
"id": "9ckszLlFYNHk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!ffmpeg -i /content/o.mp4 -i /content/output-audio.aac -c:v copy -map 0:v -map 1:a -y /content/outputHD.mp4 -loglevel quiet"
],
"metadata": {
"id": "CRfWWnkn6mEi"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!rm /content/output-audio.aac /content/o.mp4 /content/outputHD.mp4"
],
"metadata": {
"id": "C2DpBw1d_-fv"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install gdown && gdown --fuzzy https://drive.google.com/file/d/19pWvdEHS-CEG6tW3PdxdtZ5QEymVjImc/view?usp=sharing"
],
"metadata": {
"id": "bPJG0uiVupP4"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment