騰訊的老照片修復算法,我把它搬到網上,隨便玩


大家好,之前向大家介紹並跑通了騰訊開源的老照片修復算法(AI 黑科技,老照片修復,模糊變高清),同時我也提到官方提供的3個線上試玩版體驗都不好。所以我微調了一下官方 Demo,使用最新的V1.3預訓練模型且輸出whole-image,大家先感受一下

GFPGAN + Gradio + Huggingface 這三者我都是剛接觸,揉在一起還是挺好玩的。

下面我就將整個實現過程詳細介紹一下

克隆官方Demo

GFPGAN 的官方 Demo 就屬 Huggingface 體驗還行,缺點是只輸出人臉且使用的是老模型。

https://huggingface.co/spaces/akhaliq/GFPGAN

clone 之前先安裝git lfs

LFS是Large File Storage的縮寫,用了幫助git管理大的文件

sudo apt-get install git-lfs
git init # 這一步必須
sudo git lfs install 
# 安裝完成克隆GFPGAN
git clone https://huggingface.co/spaces/akhaliq/GFPGAN

克隆后我直接運行了一下 python app.py,出現了兩個報錯,大家如果有遇到可以參考一下。
報錯1:ERROR: Exception in ASGI application
解決pip install aiofiles

報錯2:There is no current event loop in thread 'Thread-2'
解決:參考這這篇文章
https://www.cnblogs.com/SunshineKimi/p/12053914.html
具體就是在uvicorn的server.py 加上一句 new_loop = asyncio.new_event_loop()

Gradio

Huggingface 上的 GFPGAN Demo 是用 Gradio 實現的。

Gradio是MIT的開源項目,使用gradio,只需在原有的代碼中增加幾行,就能自動化生成交互式web頁面,並支持多種輸入輸出格式,比如圖像分類中的圖>>標簽,超分辨率中的圖>>圖等。同時還支持生成能外部網絡訪問的鏈接,能夠迅速讓他人體驗你的算法。

Gradio 的定位類似於 Streamlit,但是更輕量(一行代碼),因為它推薦的應用場景都是對“單個函數”進行調用的應用,並且不需要對組件進行回調。

https://www.gradio.app

順便提一下,我之前已對 Streamlit 有詳細介紹並開發了幾個小東西:

我也是第一次接觸 gradio ,它的安裝很簡單:pip install gradio

從零學起我只看了官方文檔,用法也只看了 Interface ,耗時半個小時。

# getting_started
https://www.gradio.app/getting_started/
# docs
https://www.gradio.app/docs/
# github
https://github.com/gradio-app/gradio

代碼修改

官方Demo的代碼我只修改已下幾處:

  • 修改model_path,直接將下載好的V1.3預訓練模型放到了experiments/pretrained_models下。
  • 修改 inference(img),展現restored_img
restorer = GFPGANer(
    model_path='experiments/pretrained_models/GFPGANv1.3.pth',
    upscale=2,
    arch='clean',
    channel_multiplier=2,
    bg_upsampler=bg_upsampler)

def inference(img):
    input_img = cv2.imread(img, cv2.IMREAD_COLOR)
    cropped_faces, restored_faces, restored_img = restorer.enhance(
        input_img, has_aligned=False, only_center_face=False, paste_back=True)
    return Image.fromarray(restored_img[:, :, ::-1])

改完后可以python app.py先看一下效果:

它會自動生成一個本地靜態交互頁面,瀏覽器會自動跳轉到 http://127.0.0.1:7860

那個gradio.app的鏈接可以分享,有效期 72 小時。

上傳到Huggingface

step1:注冊Huggingface賬號

step2:創建Space,SDK記得選擇Gradio

step3:克隆新建的space代碼,然后將改好的代碼push上去

git lfs install 
git add .
git commit -m "commit from $beihai"
git push

push的時候會讓輸入用戶名(就是你的注冊郵箱)和密碼,解決git總輸入用戶名和密碼的問題:git config --global credential.helper store

push完成就大功告成了,回到你的space頁對應項目,就可以看到效果了。

不過Huggingface也時常500,大家多試試吧,畢竟免費。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM