目前人工智慧(AI)已有越來越多的應用,而CodeFormer是一種人工智慧數位臉部修復工具,一般可以在其官網上傳照片,它會修復後讓你下載,但傳過一次曠日費時,還好它是開源軟體,於是將其安裝到自己的電腦中,便可以批次處理,不過事涉大量運算,有獨顯速度會快些,沒有的就只能用CPU硬解。
如果原稿臉部夠大張(最好512×512像素以上),其還原程度相當不錯,有針對臉部一般修復、精細修復、上色、破碎還原等,個人覺得蠻自然傳神的。它也可以針對影片做臉部修復,但更耗資源我就不試了,有興趣的可以將你的照片丟到網站上試試看。
CodeFormer專案:
https://github.com/sczhou/CodeFormer
- 先 https://repo.anaconda.com/archive/ 下載 anaconda 2024.06-1 版 (挑選 Linux x86_64)**CodeFormer 建議 Python 3.8~3.11,太新的可能會出問題!!
- 安裝必要的軟體:
sudo apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
3. 安裝Anaconda(使用者為emer)
chmod +x Anaconda3-2020.11-Linux-x86_64.shbash ./Anaconda3-2020.11-Linux-x86_64.sh
[Enter] 觀看版權聲明,[空白鍵]翻頁,按[q]離開
Do you accept the license terms? [yes|no] -> [yes]
Anaconda3 will now be installed into this location:/home/emer/anaconda3 -> [Enter]
Do you wish to update your shell profile to automatically initialize conda? -> [no]
在 /etc/profile 新增 export PATH=/home/emer/anaconda3/bin:$PATH 到最後一行
sudo pluma /etc/profile
使環境變數生效及初始化、更新:
source /etc/profile
conda init
conda config --set auto_activate_base false
sudo $(which conda) update -n base -c defaults conda
安裝 PyTorch: https://pytorch.org/get-started/locally/ 如果有獨顯可以自行挑選,沒有獨顯只能用CPU運算就挑選如下圖,執行紅框內指令:
pip3 install torch torchvision torchaudio (獨顯)
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu (CPU)
4. 安裝 CodeFormer
git clone https://github.com/sczhou/CodeFormer
cd CodeFormer
pip3 install -r requirements.txt
python basicsr/setup.py develop
python scripts/download_pretrained_models.py facelib
python scripts/download_pretrained_models.py CodeFormer
使用 CodeFormer:
將一個批次檔(ex: aiphoto)放置於 /usr/local/bin 內容如下:
cd $HOME/CodeFormer
source /etc/profile
python inference_codeformer.py -w 0.7 --face_upsample --bg_upsampler realesrgan --input_path $HOME/old
mv $HOME/CodeFormer/results/old_0.7/final_results/* $HOME/桌面
rm $HOME/old/*
在家目錄中開一個 old 目錄,將要優化的照片丟到裡面,然後執行 aiphoto 即可,完成後優化照片會移到桌面上。(使用內建指令 ai1photo, ai2photo, ai3photo 優化細節越來越高,完成後照片放置於 aiphoto 資料夾)
如果有 Nvidia 顯卡,運算速度將會加快許多,方法如下:(以使用 LDS1.x 為例),注意!核心要使用 Debian 標準版核心以免編譯失敗。
sudo apt update
sudo apt upgrade
sudo apt install nvidia-detect nvidia-driver firmware-misc-nonfree
作業系統會自動將 Nvidia 支援加入核心中,重新開機即可生效,再跑看看會發現速度加快非常多。
For cropped and aligned faces (512×512)
python inference_codeformer.py -w 0.5 --has_aligned --input_path [image folder]|[image path]
For whole image
Add ‘–bg_upsampler realesrgan’ to enhance the background regions with Real-ESRGAN
Add ‘–face_upsample’ to further upsample restorated face with Real-ESRGAN
python inference_codeformer.py -w 0.7 --input_path [image folder]|[image path]
For Windows/Mac users, please install ffmpeg first
conda install -c conda-forge ffmpeg
For video clips
Video path should end with ‘.mp4’|’.mov’|’.avi’
python inference_codeformer.py --bg_upsampler realesrgan --face_upsample -w 1.0 --input_path [video path]
For cropped and aligned faces (512×512)
Colorize black and white or faded photo
python inference_colorization.py --input_path [image folder]|[image path]
For cropped and aligned faces (512×512)
Inputs could be masked by white brush using an image editing app (e.g., Photoshop)
(check out the examples in inputs/masked_faces)
python inference_inpainting.py --input_path [image folder]|[image path]
留言
張貼留言