Advertisement
Guest User

SimSwap_Installer

a guest
Jul 7th, 2021
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.52 KB | None | 0 0
  1. <# :
  2. @echo off
  3. setlocal enabledelayedexpansion
  4. set "POWERSHELL_STAGE=SHOW_PS_VERSION"
  5. powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  6. if not exist downloads\ mkdir downloads
  7. if not exist anaconda3\ mkdir anaconda3
  8. if not exist simswap\ mkdir simswap
  9. if not exist anaconda3\_conda.exe (
  10.     set "POWERSHELL_STAGE=DOWNLOAD_ANACONDA"
  11.     powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  12.     if !errorlevel! equ 1 (
  13.         pause
  14.         exit /b
  15.     )
  16.     for %%i in (downloads\anaconda3*exe) do (
  17.         echo Installing %%~ni...
  18.         start /wait "" %%i /InstallationType=JustMe /AddToPath=1 /RegisterPython=0 /NoRegistry=1 /S /D=%~dp0anaconda3
  19.         echo Done!
  20.         goto INSTALL_SIMSWAP_MODELS
  21.     )
  22.     exit /b
  23. )
  24. :INSTALL_SIMSWAP_MODELS
  25. if not exist simswap\arcface_model\arcface_checkpoint.tar (
  26.     set "POWERSHELL_STAGE=SETUP_SIMSWAP_MODELS"
  27.     powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  28. )
  29. if exist "simswap\README.md" (
  30.     set "POWERSHELL_STAGE=SETUP_SIMSWAP"
  31.     powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  32. )
  33. if not exist "simswap\Drop_videos.bat" (
  34.     set "POWERSHELL_STAGE=EXPORT_BATCH_FILES"
  35.     powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"
  36. )
  37. call anaconda3\Scripts\activate.bat %~dp0anaconda3
  38. cd simswap
  39. call conda create -n simswap python=3.6 --yes
  40. call conda activate simswap
  41. call conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=10.2 -c pytorch --yes
  42. call pip install --ignore-installed imageio
  43. call pip install insightface==0.2.1 onnxruntime moviepy
  44. exit /b
  45. : #>
  46. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  47. $ProgressPreference = "SilentlyContinue"
  48. $simswap_path = "simswap\"
  49.  
  50. if ($env:POWERSHELL_STAGE -eq "SHOW_PS_VERSION") {
  51.     Write-Host "PowerShell version:"
  52.     $PSVersionTable.PSVersion
  53. }
  54.  
  55. if ($env:POWERSHELL_STAGE -eq "DOWNLOAD_ANACONDA") {
  56.     $target_hash = "93db42390444019e98b442ab281e1091671b6dce64daf08928d337ffc83cf3d2"
  57.     $url = "https://repo.anaconda.com/archive/Anaconda3-2021.05-Windows-x86_64.exe"
  58.     $file = "downloads\$($url.split('/') | Select -Last 1)"
  59.     if (!(Test-Path $file)) {
  60.         Write-Host "Downloading $file..."
  61.         Invoke-WebRequest $url -OutFile $file
  62.         Write-Host "Downloaded!"
  63.     }
  64.     Write-Host "Verifying the installer data integrity..."
  65.     $file_hash = Get-FileHash $file -Algorithm "SHA256"
  66.     if ($file_hash.Hash -ne $target_hash) {
  67.         Write-Host "Hash mismatch! Try again!" $file_hash.Hash $target_hash
  68.         Remove-Item $file
  69.         exit 1
  70.     }
  71.     Write-Host "Done!"
  72. }
  73.  
  74. if ($env:POWERSHELL_STAGE -eq "SETUP_SIMSWAP_MODELS") {
  75.     $url = "https://drive.google.com/uc?export=download&id=1rm9Ea_8P25-bU0NGjqncWBqbkvX1ZtPx"
  76.     $file = "downloads\SimSwap-main-cpu.7z"
  77.     if (!(Test-Path $file)) {
  78.         Write-Host "Downloading $file..."
  79.         $response = Invoke-WebRequest $url -SessionVariable "session"
  80.         if ($response -match "confirm=(?<confirm_key>.*?)&amp") {
  81.             $url = $url.replace("export=download", "export=download&confirm=$($Matches["confirm_key"])")
  82.             Invoke-WebRequest $url -OutFile $file -WebSession $session
  83.             Write-Host "Downloaded!"
  84.         } else {
  85.             Write-Host "Something went wrong..."
  86.             exit 1
  87.         }
  88.     }
  89.     Write-Host "Unzipping $file..."
  90.     if (!(Get-Module -ListAvailable -Name "7Zip4PowerShell")) {
  91.         Write-Host "Have to instal 7zip module first..."
  92.         Install-PackageProvider -Name "NuGet" -MinimumVersion "2.8.5.201" -Scope "CurrentUser" -Force
  93.         Set-PSRepository -Name "PSGallery" -SourceLocation "https://www.powershellgallery.com/api/v2" -InstallationPolicy "Trusted"
  94.         Install-Module -Name "7Zip4PowerShell" -Scope "CurrentUser" -Force
  95.         Write-Host "Unzipping..."
  96.     }
  97.     Expand-7Zip $file -TargetPath $simswap_path
  98.     Set-Location $simswap_path
  99.     Get-ChildItem "SimSwap-main-cpu" | Move-Item -Destination ".\"
  100.     Remove-Item "SimSwap-main-cpu"
  101.     Set-Location "..\"
  102.     Write-Host "Done!"
  103. }
  104.  
  105. if ($env:POWERSHELL_STAGE -eq "SETUP_SIMSWAP") {
  106.     $url = "https://github.com/neuralchen/SimSwap/archive/refs/heads/main.zip"
  107.     $file = "downloads\SimSwap-main.zip"
  108.     if (!(Test-Path $file)) {
  109.         Write-Host "Downloading $file..."
  110.         Invoke-WebRequest $url -OutFile $file -WebSession $session
  111.         Write-Host "Downloaded!"
  112.     }
  113.     Write-Host "Unzipping $file..."
  114.     Expand-Archive $file -DestinationPath $simswap_path
  115.     Set-Location $simswap_path
  116.     Get-ChildItem "SimSwap-main" -Directory -Recurse -Name | where { !(Test-Path("$_")) } | % { Move-Item -Path "\SimSwap-main\$_" -Destination ".\$_" }
  117.     Get-ChildItem "SimSwap-main" -File -Recurse -Name | % { Move-Item -Path "SimSwap-main\$_" -Destination ".\$_" -Force }
  118.     Remove-Item "SimSwap-main" -Recurse
  119.     Write-Host "Done!"
  120.     Write-Host "Applying patches..."
  121.     $search_replace = @{
  122.         "if len(self.opt.gpu_ids)" = "if torch.cuda.is_available() and len(self.opt.gpu_ids)";
  123.         "torch.device(`"cuda:0`")" = "torch.device(`'cuda:0`' if torch.cuda.is_available() else `'cpu`')";
  124.         "torch.load(netArc_checkpoint)" = "torch.load(netArc_checkpoint) if torch.cuda.is_available() else torch.load(netArc_checkpoint, map_location=torch.device(`'cpu`'))";
  125.         ".cuda()" = ".to(torch.device(`'cuda:0`' if torch.cuda.is_available() else `'cpu`'))";
  126.         ".to('cuda')" = ".to(torch.device(`'cuda:0`' if torch.cuda.is_available() else `'cpu`'))";
  127.     }
  128.     ForEach ($File in (Get-ChildItem -Path ".\*.py" -Recurse -File)) {
  129.         $content = (Get-Content $File)
  130.         if ($content.length -gt 0) {
  131.             ForEach ($search in $search_replace.Keys) {
  132.                 $content = $content.replace("$search", "$($search_replace[$search])")
  133.             }
  134.             Set-Content $File $content
  135.         }
  136.     }
  137.     Remove-Item "README.md"
  138.     Set-Location "..\"
  139.     Write-Host "Done!"
  140. }
  141.  
  142. if ($env:POWERSHELL_STAGE -eq "EXPORT_BATCH_FILES") {
  143.     Set-Location $simswap_path
  144.     Set-Content "Drop_picture_or_video.bat" @"
  145. `@echo off
  146.  
  147. set "FACE_IMG="
  148. set "VIDEO="
  149. set "OUTPUT_FILE=output\"
  150. set "FILE=%~2"
  151.  
  152. if "%~x1"==".jpg" set "FACE_IMG=%~1"
  153. if "%~x1"==".jpeg" set "FACE_IMG=%~1"
  154. if "%~x1"==".png" set "FACE_IMG=%~1"
  155.  
  156. if [%~1] neq [] (
  157.     echo %~1
  158.     if [%FILE%] equ [] (
  159.         if defined FACE_IMG (echo Path to video file:) else (echo Path to face image file:)
  160.         set /p "FILE="
  161.     )
  162. )
  163. for %%f in ("%FILE%") do set "FILENAME=%%~nf"
  164. if defined FACE_IMG (
  165.     set "VIDEO=%FILE%"
  166.     set "OUTPUT_FILE=%OUTPUT_FILE%%~n1_%FILENAME%.mp4"
  167. ) else (
  168.     set "VIDEO=%~1"
  169.     set "FACE_IMG=%FILE%"
  170.     set "OUTPUT_FILE=%OUTPUT_FILE%%FILENAME%_%~n1.mp4"
  171. )
  172.  
  173. if not defined VIDEO goto wrong_args
  174. if not defined FACE_IMG goto wrong_args
  175.  
  176. echo Face image file: %FACE_IMG%
  177. echo Video file: %VIDEO%
  178. call %userprofile%\anaconda3\Scripts\activate.bat %userprofile%\anaconda3
  179. cd %~dp0
  180. call activate simswap
  181. call python test_video_swapmulti.py --isTrain false --name people --Arc_path arcface_model/arcface_checkpoint.tar --temp_path ./temp_results ^
  182.         --pic_a_path %FACE_IMG% ^
  183.         --video_path %VIDEO% ^
  184.         --output_path ./%OUTPUT_FILE%
  185. if exist %~dp0%OUTPUT_FILE% call explorer /select,%~dp0%OUTPUT_FILE%
  186. goto :eof
  187.  
  188. :wrong_args
  189. echo No video or face image provided^!
  190. echo Args: %~1 %FILE%
  191. "@
  192.  
  193.     Set-Content "Drop_videos.bat" @"
  194. `@echo off
  195.  
  196. set "FACE_IMG="
  197. set "VIDEOS="
  198. set "OUTPUT_DIR=output\"
  199.  
  200. if "%~1"=="" (
  201.     echo Pass at least one video file!
  202.     pause
  203.     goto :eof
  204. )
  205.  
  206. echo Path to face image:
  207. set /p "FACE_IMG="
  208.  
  209. for %%i in ("%FACE_IMG%") do (
  210.     if "%%~xi"==".jpg" set "FACE_IMG=%%i"
  211.     if "%%~xi"==".jpeg" set "FACE_IMG=%%i"
  212.     if "%%~xi"==".png" set "FACE_IMG=%%i"
  213. )
  214.  
  215. if not defined FACE_IMG goto wrong_args
  216. echo Face image file is: %FACE_IMG%
  217. for %%i in ("%FACE_IMG%") do set "FACE_NAME=%%~ni"
  218.  
  219. call %userprofile%\anaconda3\Scripts\activate.bat %userprofile%\anaconda3
  220. cd %~dp0
  221. call activate simswap
  222. :swap_video
  223. echo Swapping video: %~nx1
  224. call python test_video_swapmulti.py --isTrain false --name people --Arc_path arcface_model/arcface_checkpoint.tar --temp_path ./temp_results ^
  225.         --pic_a_path %FACE_IMG% ^
  226.         --video_path %~1 ^
  227.         --output_path "./%OUTPUT_DIR%%FACE_NAME%_%~n1.mp4"
  228. )
  229. shift
  230. if not "%~1"=="" goto :swap_video
  231. if exist %~dp0%OUTPUT_DIR% call explorer /select,%~dp0%OUTPUT_DIR%
  232. goto :eof
  233.  
  234. :wrong_args
  235. echo No face image provided^!
  236. echo Args: %~1 %VIDEO%
  237. "@
  238.     Set-Location "..\"
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement