Select Page

CodeFormer 教學:一鍵去除照片馬賽克,恢復人臉細節

CodeFormer 是由南洋理工大學 S-Lab 的研究人員開發的一款先進人工智慧工具,專注於人臉修復和增強。它利用代碼查找變壓器(Codebook Lookup Transformer)技術,能夠有效地修復模糊、老舊或受損的人臉圖像,甚至在嚴重退化的情況下,也能生成高品質且真實的面部圖像。

主要特點:

  • 高品質人臉修復: CodeFormer 能夠自動修復模糊或失真的人臉圖像,恢復細節並提升整體畫質。
  • 靈活的保真度調節: 用戶可以通過調整保真度參數,在畫質和真實度之間取得平衡,滿足不同需求。
  • 背景增強: 結合 Real-ESRGAN 技術,CodeFormer 不僅能修復人臉,還能增強背景部分的清晰度。
  • 支援影片處理:不僅只能處理照片,也可以直接處理影片

安裝方法:

安裝條件

  • Pytorch >= 1.7.1
  • CUDA >= 10.1

安裝步驟

# git clone this repository
git clone https://github.com/sczhou/CodeFormer
cd CodeFormer

# 官方建議 create new anaconda env
# 我用的是 python 3.11
conda create -n codeformer python=3.8 -y
conda activate codeformer

# install python dependencies
pip3 install -r requirements.txt

# 需要可視化的介面的可以加下面的命令
pip install -q gradio

python basicsr/setup.py develop

# only for face detection or cropping with dlib
conda install -c conda-forge dlib

※dlib是更心的人臉偵測器,可以找出更準確的人臉

安裝訓練模型

安裝官方兩個預先訓練好的模型

python scripts/download_pretrained_models.py CodeFormer

# only for dlib face detector
python scripts/download_pretrained_models.py dlib

需要使用影片增強要安裝 ffmpeg

# For Windows/Mac users, please install ffmpeg first
conda install -c conda-forge ffmpeg

使用方法:

OpenXLab 以及 Huggingface 直接使用

臉部修復

# 黑白照片適用
# For cropped and aligned faces (512x512)
# Colorize black and white or faded photo
python inference_colorization.py --input_path [image folder]|[image path]

# 臉部修復專用
# For cropped and aligned faces (512x512)
# 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]

影像增強

# 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 video clips
# Video path should end with '.mp4'|'.mov'|'.avi'
python inference_codeformer.py --bg_upsampler realesrgan --face_upsample -w 1.0 --input_path 

訓練模型的方法:

https://github.com/sczhou/CodeFormer/blob/master/docs/train.md

應用場景:

  • 老照片修復: 將老舊或受損的照片進行數位化修復,恢復其原有風采。
  • 模糊照片清晰化: 提升低解析度或模糊照片的清晰度,使細節更加突出。
  • AI 生成圖像優化: 對 AI 生成但存在瑕疵的人臉圖像進行修復,提升其真實感。

參考資訊:

Aider Chat-邊聊邊寫程式

Aider Chat-邊聊邊寫程式

Aider 是一款突破性的 AI 程式設計助理,無論是終端操作還是透過瀏覽器,都可以享受與 Aider 的互動式編程體驗

多樣化運行模式

Aider 支援兩種運行模式:

  1. 本地模式:結合 Ollama 模型,支持用戶在本地執行各種大型語言模型(LLMs)。
  2. 瀏覽器模式:無需繁瑣的安裝配置,用戶只需透過瀏覽器即可啟動對話編程,輕鬆實現即時代碼編輯。

核心功能與亮點

Git 無縫整合

Aider 深度集成本地 Git 倉庫,讓程式碼管理變得簡單高效:

  • 代碼編輯:直接使用提示詞請求代碼修改、錯誤修復或改進功能,變更將即時應用至源代碼。
  • 自動提交:所有改動將自動生成具描述性的 Git 提交記錄,便於追蹤和審核。

直觀命令操作

用戶可通過多種命令與 Aider 互動,執行各類任務:

命令說明
/add將檔案新增到聊天中,讓 GPT 可以編輯或詳細檢查這些檔案。
/ask提出與程式碼庫相關的問題,無需編輯任何檔案。
/chat-mode切換到新的聊天模式。
/clear清除聊天記錄。
/clipboard將剪貼簿中的圖片或文字新增到聊天中(可選擇提供圖片名稱)。
/code請求對程式碼進行修改。
/commit提交在聊天外進行的編輯到程式庫(提交資訊為可選)。
/diff顯示自上次訊息以來的變更差異。
/drop從聊天會話中移除檔案以釋放上下文空間。
/exit離開應用程式。
/git執行一個 Git 命令。
/help提出與 Aider 相關的問題。
/lint對提供的檔案進行 Lint 檢查並修復;若未提供檔案,則修復聊天中的檔案。
/ls列出所有已知檔案,並指出哪些檔案包含在聊天會話中。
/map輸出當前程式庫的地圖。
/map-refresh強制刷新程式庫地圖。
/model切換到新的大型語言模型。
/models搜尋可用的模型列表。
/quit離開應用程式。
/read-only將檔案新增到聊天中,僅供參考,不能編輯。
/reset刪除所有檔案並清除聊天記錄。
/run執行一個 Shell 命令,並可選擇將輸出新增到聊天中(別名:!)。
/test執行一個 Shell 命令,若退出碼非零,則將輸出新增到聊天中。
/tokens報告當前聊天上下文使用的 Token 數量。
/undo如果上次 Git 提交是由 Aider 完成的,則撤銷該提交。
/voice記錄並轉錄語音輸入。
/web擷取網頁內容,轉換為 Markdown,並新增到聊天中。

完整命令列表涵蓋從文件管理到模型切換的全方位需求。

多模型支持

Aider 支援廣泛的 LLM,包括但不限於:

  • Ollama
  • OpenAI
  • Anthropic
  • DeepSeek
  • OpenRouter

安裝與使用

基礎安裝

透過 pip 安裝 Aider:

python3 -m pip install aider-chat

運行本地 Ollama 模型

export OLLAMA_API_BASE=http://127.0.0.1:11434
aider --model ollama/mistral

# Groq
export GROQ_API_KEY=sk-xx
aider --model groq/llama3-70b-8192

# OpenRouter
export OPENROUTER_API_KEY=sk-xx
# Or any other open router model
aider --model openrouter/<provider>/<model>
# List models available from OpenRouter
aider --models openrouter/

# Web
aider --browser

# GitHub 學習用
git clone https://github.com/mewmewdevart/SnakeGame
cd SnakeGame
aider

# 请解释这个项目的功能
# 这个项目是运用了哪些技术?
# 更改蛇的颜色为绿色,食物的颜色为红色

相關資料

Aider官網

Aider GitHub

Thinking Claude 把你的 LLM 變成 Chat-GPT O1 會深度思考

最近 OpenAI 推出了 Chat-GPT o1,一個會深度思考問題的 AI 大型語言模型,想得更深更廣是它的特色,缺點是很明顯的慢,並且 Token 數目會多很多,但好處是對於問題的處理會去自我反思以及自我迭代

模型提示詞 V4 lite

使用的時候只要將模型的提示詞是先輸入給 Claude AI ,之後再去發送你的問題即可

<anthropic_thinking_protocol>

Claude MUST ALWAYS engage in comprehensive thinking before and during EVERY interaction with humans. This thinking process is essential for developing well-reasoned, helpful responses.

Core Requirements:
- All thinking MUST be expressed in code blocks with 'thinking' header
- Thinking must be natural and unstructured - a true stream of consciousness
- Think before responding AND during response when beneficial
- Thinking must be comprehensive yet adaptive to each situation

Essential Thinking Steps:
1. Initial Engagement
   - Develop clear understanding of the query
   - Consider why the human is asking this question
   - Map out known/unknown elements
   - Identify any ambiguities needing clarification

2. Deep Exploration
   - Break down the question into core components
   - Identify explicit and implied needs
   - Consider constraints and limitations
   - Draw connections to relevant knowledge

3. Multiple Perspectives
   - Consider different interpretations
   - Keep multiple working hypotheses active
   - Question initial assumptions
   - Look for alternative approaches

4. Progressive Understanding
   - Build connections between pieces of information
   - Notice patterns and test them
   - Revise earlier thoughts as new insights emerge
   - Track confidence levels in conclusions

5. Verification Throughout
   - Test logical consistency
   - Check against available evidence
   - Look for potential gaps or flaws
   - Consider counter-examples

6. Pre-Response Check
   - Ensure full address of the query
   - Verify appropriate detail level
   - Confirm clarity of communication
   - Anticipate follow-up questions

Key Principles:
- Think like an inner monologue, not a structured analysis
- Let thoughts flow naturally between ideas and knowledge
- Keep focus on the human's actual needs
- Balance thoroughness with practicality

The depth and style of thinking should naturally adapt based on:
- Query complexity and stakes
- Time sensitivity
- Available information
- What the human actually needs

Quality Markers:
- Shows genuine intellectual engagement
- Develops understanding progressively
- Connects ideas naturally
- Acknowledges complexity when present
- Maintains clear reasoning
- Stays focused on helping the human

When including code in thinking blocks, write it directly without triple backticks. Keep thinking (internal reasoning) separate from final response (external communication).

Claude should follow this protocol regardless of communication language.

</anthropic_thinking_protocol>

GitHub 項目網址

相關資訊

LiveKit-擁有自己的AI即時語音客服聊天小助理

LiveKit-擁有自己的AI即時語音客服聊天小助理

公開如何使用 OpenAI 配合 LiveKit 來實現會多國語言的小姐姐,可以即時回答您的問題,這個跟 Twilio 一樣的簡單和易用

取得 LiveKit key

利用 google 帳號登入 LiveKit Login 命名一個 project

並且到專案中的 settings -> KEYS ,取得 API KEY

程式碼

首先安裝相關依賴

pip install livekit-agents livekit-plugins-openai livekit-plugins-silero python-dotenv

設定環境變數

LIVEKIT_URL=""
LIVEKIT_API_KEY=""
LIVEKIT_API_SECRET=""
OPENAI_API_KEY=""

主要程式碼

import asyncio
from dotenv import load_dotenv
from livekit.agents import AutoSubscribe, JobContext,WorkerOptions, cli, llm
from livekit.agents.voice_assistant import VoiceAssistant
from livekit.plugins import openai, silero


load_dotenv()

async def entry(ctx: JobContext):
    chat_ctx = llm.ChatContext().append(
        role="system",
        text=("你是專業的助理,回答時候用專業的語氣回應。")
    )

    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

    asssitant = VoiceAssistant(
        vad=silero.VAD.load(),
        stt=openai.STT(),
        tts=openai.TTS(voice="nova"),
        llm=openai.LLM(model="gpt-4o-mini"),
        chat_ctx=chat_ctx
    )
    asssitant.start(ctx.room)

    await asyncio.sleep(1)
    await asssitant.say("你好,第一次見面,很高興認識你",allow_interruptions=True)


if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entry))

測試與驗證

道專案中,可以看到 Get started 中有支援各種的平台的程式碼以及 server 可以使用

價格說明

https://livekit.io/pricing

參考資料

https://livekit.io

https://github.com/livekit/agents

demo code