溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

發(fā)布時(shí)間:2022-01-04 10:29:45 來源:億速云 閱讀:523 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

    環(huán)境部署

    下面進(jìn)入最坑的部分,首先我們看一下項(xiàng)目給出的安裝說明。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    其實(shí)只有兩條,conda導(dǎo)入environment/environment.yaml環(huán)境,下載II2S圖片。就這兩句話讓我裝了一晚上,天知道我經(jīng)歷了啥。坑踩了一邊,可以按照我的流程裝,應(yīng)該問題不大。

    1、導(dǎo)入environment/environment.yaml環(huán)境

    先創(chuàng)建conda虛擬環(huán)境,使用一下命令:

    conda env create -f environment/environment.yaml

    2、安裝pytorch

    這里有不會的話,可以看我另一篇文章,有詳細(xì)的搭建方法:機(jī)器學(xué)習(xí)基礎(chǔ)環(huán)境部署 

    3、依賴庫安裝

    我就不提我踩了多少坑了,下面我把需要的項(xiàng)目沒提供的依賴,總結(jié)發(fā)在下面。

    dlib

    Ninja

    gdown

    scikit-image

    IPython

    opencv-python

    版本可以選擇最新的,因?yàn)槲矣米钚碌呐芷饋砹恕?/p>

    4、 cl.exe環(huán)境變量配置

    需要安裝一下Microsoft Visual Studio,我選擇的版本是2019版本。

    提供一下下載地址:下載 Visual Studio Tools 

    這里需要注意安裝的時(shí)候需要把C++選上。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    按照自己的機(jī)器位數(shù),選擇路徑添加到環(huán)境變量路徑里。下面是我的路徑。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    添加環(huán)境變量

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    5、模型下載

    項(xiàng)目在運(yùn)行的時(shí)候其實(shí)是會自己下載模型的,但是從google云盤經(jīng)常失敗,而且該項(xiàng)目要的模型特別多,我放在百度云盤上分享出來,大家可以下我的。

    模型文件鏈接  提取碼:tuan

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    首先人臉68特征數(shù)據(jù)放到項(xiàng)目的cache目錄內(nèi)。其次另外的6個pt文件放到項(xiàng)目的pretrained_models目錄內(nèi)。

    6、發(fā)型數(shù)據(jù)下載

    按照項(xiàng)目給出的地址從云盤下載到input/face目錄內(nèi)。這邊我還是提供百度云盤的分享鏈接,下不下來可以用我的。

    鏈接  提取碼:TUAN

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    7、代碼調(diào)整

    項(xiàng)目align_face.py文件中,不會檢查模型是否存在,還是會下載,需要調(diào)整一下。如下圖:

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    項(xiàng)目驗(yàn)證

    OK,環(huán)境如果你按照我上面說的部署成功了,恭喜。下面我來驗(yàn)證一下效果。

    先看看我準(zhǔn)備的照片。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    把照片放到unprocessed目錄下,注意編號需要和input/face內(nèi)的文件編號不同。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    1、預(yù)處理照片

    首先我們執(zhí)行命令預(yù)處理unprocessed目錄內(nèi)的照片。命令如下:

    python align_face.py
    (Barbershop) C:\Users\yi\PycharmProjects\Barbershop>python align_face.py
    Downloading Shape Predictor
    126.jpg: Number of faces detected: 1

    可以看到input/face目錄多了一個文件,預(yù)處理把原照片的人臉處理出來了。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    2、換發(fā)型

    下面看一下我準(zhǔn)備換的發(fā)型是哪張。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    換個短發(fā)。執(zhí)行命令如下:

    python main.py --im_path2 126.png --im_path3 94.png --im_path4 94.png --sign realistic --smooth 5

    看下執(zhí)行結(jié)果

    (Barbershop) C:\Users\yi\PycharmProjects\Barbershop>python main.py --im_path2 126.png --im_path3 94.png --im_path4 94.png --sign realistic --smooth 5
    Loading StyleGAN2 from checkpoint: pretrained_models/ffhq.pt
    Setting up Perceptual loss...
    Loading model from: C:\Users\yi\PycharmProjects\Barbershop\losses\lpips\weights\v0.1\vgg.pth
    ...[net-lin [vgg]] initialized
    ...Done
    Number of images: 2
    Images: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [10:39<00:00, 319.74s/it]
    Number of images: 2
    Images: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [02:08<00:00, 64.04s/it]
    Loading StyleGAN2 from checkpoint: pretrained_models/ffhq.pt
    C:\Users\yi\PycharmProjects\Barbershop\models\Alignment.py:97: UserWarning: where received a uint8 condition tensor. This behavior is deprecated and will be removed in a
     future version of PyTorch. Use a boolean condition instead. (Triggered internally at  ..\aten\src\ATen\native\TensorCompare.cpp:328.)
      new_target_final = torch.where(OB_region, torch.from_numpy(new_target_inpainted), new_target)
    Align Step 2:   0%|                                                                                                                             | 0/100 [00:00<?, ?it/s]E
    :\ProgramData\Anaconda3\envs\Barbershop\lib\site-packages\torch\nn\functional.py:3680: UserWarning: The default behavior for interpolate/upsample with float scale_factor
     changed in 1.6.0 to align with other frameworks/libraries, and now uses scale_factor directly, instead of relying on the computed output size. If you wish to restore th
    e old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details.
      "The default behavior for interpolate/upsample with float scale_factor changed "
    E:\ProgramData\Anaconda3\envs\Barbershop\lib\site-packages\torch\nn\functional.py:3635: UserWarning: Default upsampling behavior when mode=bicubic is changed to align_co
    rners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
      "See the documentation of nn.Upsample for details.".format(mode)
    Loading StyleGAN2 from checkpoint: pretrained_models/ffhq.pt
    Setting up Perceptual loss...
    Loading model from: C:\Users\yi\PycharmProjects\Barbershop\losses\masked_lpips\weights\v0.1\vgg.pth
    ...[net-lin [vgg]] initialized
    ...Done
    Setting up Perceptual loss...
    Loading model from: C:\Users\yi\PycharmProjects\Barbershop\losses\masked_lpips\weights\v0.1\vgg.pth
    ...[net-lin [vgg]] initialized
    ...Done

    等了很長時(shí)間,結(jié)果出來了。最后的效果圖如下

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    對比一下看看,還是很不錯的吧,我這么長的安裝時(shí)間沒有白費(fèi)。

    Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能

    以上是“Python中Barbershop如何實(shí)現(xiàn)照片換發(fā)型功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI