當前位置:
首頁 > 知識 > 使用Python進行面部合成

使用Python進行面部合成


Linux編程

點擊右側關注,免費入門到精通!



作者丨chestnut_egg


https://www.cnblogs.com/chestnut-egg




完整代碼已上傳至GitHub:


https://github.com/chestnut-egg/Face 



一、準備工作




1. 此程序使用的是 Face++ 的API,所以需要去Face++官網註冊賬號:




https://www.faceplusplus.com.cn/




2. 創建應用,獲取 key 和 secret






3. 下載 simplejson 模塊 ,使用pip就可以下載了


pip install simplejson




二、程序思路




1. 使用 decect 介面,獲取人臉關鍵點




介面詳細文檔:




https://console.faceplusplus.com.cn/documents/4888373




* return_landmark 參數 不能為 0 不然不會返回人臉關鍵點






核心代碼:



def

 

find_face

(imgpath)

:


    print(

"finding"

)
    http_url = 

"https://api-cn.faceplusplus.com/facepp/v3/detect"


    data = {

"api_key"

: key, 

"api_secret"

: secret, 

"image_url"

: imgpath, 

"return_landmark"

1

}
    files = {

"image_file"

: open(imgpath, 

"rb"

)}
    response = requests.post(http_url, data=data, files=files)
    req_con = response.content.decode(

"utf-8"

)
    req_dict = JSONDecoder().decode(req_con)

    this_json = simplejson.dumps(req_dict)
    this_json2 = simplejson.loads(this_json)

    faces = this_json2[

"faces"

]
    list0 = faces[

0

]
    rectangle = list0[

"face_rectangle"

]
    

# print(rectangle)


    

return

 rectangle




2. 使用 mergeface 介面,合成臉部圖像




介面詳細文檔:




https://console.faceplusplus.com.cn/documents/20813963




* 注意圖片文件大小不超過 2 MB




核心代碼:



# 模板圖片地址 合成圖片地址 生成圖片地址 合成指數

0

-100


def add_face(image_url_1,image_url_2,image_url,number):

    ff1 = find_face(image_url_1)
    ff2 = find_face(image_url_2)

    rectangle1 = str(str(ff1[

"top"

]) + 

","

 + str(ff1[

"left"

]) + 

","

 + str(ff1[

"width"

]) + 

","

 + str(ff1[

"height"

]))
    rectangle2 = str(ff2[

"top"

]) + 

","

 + str(ff2[

"left"

]) + 

","

 + str(ff2[

"width"

]) + 

","

 + str(ff2[

"height"

])

    # 

print

(rectangle1)
    # 

print

(rectangle2)

    url_add = 

"https://api-cn.faceplusplus.com/imagepp/v1/mergeface"

    f1 = 

open

(image_url_1, 

"rb"

)
    f1_64 = base64.b64encode(f1.

read

())
    f1.

close

()
    f2 = 

open

(image_url_2, 

"rb"

)
    f2_64 = base64.b64encode(f2.

read

())
    f2.

close

()

    data = {

"api_key"

: key, 

"api_secret"

: secret, 

"template_base64"

: f1_64, 

"template_rectangle"

: rectangle1,
            

"merge_base64"

: f2_64, 

"merge_rectangle"

: rectangle2, 

"merge_rate"

: number}

    response = requests.post(url_add, data=data)
    req_con = response.content.decode(

"utf-8"

)
    req_dict = JSONDecoder().decode(req_con)
    

print

(req_dict)
    result = req_dict[

"result"

]
    imgdata = base64.b64decode(result)
    file = 

open

(image_url, 

"wb"

)
    file.

write

(imgdata)
    file.

close

()





3. 示例運行代碼



# 單獨兩張照片的合成示例

image_url_1 = 

r"C:Users.jpg"


image_url_2 = 

r"C:Users.jpg"


image_url = 

r"C:Users
esult.jpg"


add_face(image_url_1,image_url_2,image_url,

50

)




4. 封裝一個多張照片的合成函數




用 列表List 儲存圖片地址,先以最開始的兩張進行合成,然後將合成後的圖片與列表中的其他圖像依次合成




* 程序沒有做List的長度驗證,注意邊界特殊情況



def

 

add_many

(list_face)

:

    print(

"正在合成第1-2張"

)
    image_now = 

r"C:Users
ow.jpg"


    add_face(list_face[

0

], list_face[

1

], image_now, 

50

)

    

for

 index 

in

 range(

2

,len(list_face)):
        print(

"正在合成第"

+str(index+

1

)+

"張"

)
        add_face(image_now, list_face[index], image_now, 

50

)




5. 成果展示




素材1:







素材2:







合成結果:





 


完整代碼:




https://github.com/chestnut-egg/Face




 推薦↓↓↓ 






??

16個技術公眾號

】都在這裡!


涵蓋:程序員大咖、源碼共讀、程序員共讀、數據結構與演算法、黑客技術和網路安全、大數據科技、編程前端、Java、Python、Web編程開發、Android、iOS開發、Linux、資料庫研發、幽默程序員等。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Python開發 的精彩文章:

真是瘋了!3毫米厚的黑科技面料,竟能抗住-196℃極寒!

TAG:Python開發 |