日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

100行的python作品详解_不到 100 行 Python 代码徐峥变葛优

發布時間:2025/4/16 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 100行的python作品详解_不到 100 行 Python 代码徐峥变葛优 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

給照片換臉大家應該都見過,本文我們來介紹一下如何通過 Python 實現換臉。

功能實現

實現換臉功能,我們大致可以分為兩種:一種是所有功能都通過自己編碼來實現,另一種是借助于第三方 API 來實現,第一種方式可能需要我們進行大量的編碼才能實現,而第二種方式我們只需進行少量的編碼即可實現。

本文我們使用更簡單的第二種方式來實現,我們用到的 API 接口提供方是 Face++,首先我們需要到該網站注冊一個自己的賬號,注冊地址為:https://console.faceplusplus.com.cn/register,打開后如下所示:

我們可以通過手機號和郵箱兩種方式來注冊,注冊好賬號之后,我們再到登錄地址 https://console.faceplusplus.com.cn/login 進行登錄,登錄之后,我們會發現網站已經為我們創建好了應用,如下圖所示:

我們需要用到的是上圖中的 API Key 和 API Secret 的值,下面來看一下具體實現代碼:

# 獲取人臉關鍵點 def find_face(imgpath):print("正在查找……")http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"data = {"api_key": "自己的 api_key","api_secret": "自己的 api_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 = json.JSONDecoder().decode(req_con)this_json = simplejson.dumps(req_dict)this_json2 = simplejson.loads(this_json)print(this_json2)faces = this_json2['faces']list0 = faces[0]rectangle = list0['face_rectangle']# print(rectangle)return rectangle# 換臉,圖片的大小應不超過 2M,number 表示換臉的相似度 def merge_face(image_url1, image_url2, image_url, number):ff1 = find_face(image_url1)ff2 = find_face(image_url2)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(rectangle2)url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"f1 = open(image_url1, 'rb')f1_64 = base64.b64encode(f1.read())f1.close()f2 = open(image_url2, 'rb')f2_64 = base64.b64encode(f2.read())f2.close()data = {"api_key": "自己的 api_key","api_secret": "自己的 api_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_con1 = response.content.decode('utf-8')req_dict = json.JSONDecoder().decode(req_con1)result = req_dict['result']imgdata = base64.b64decode(result)file = open(image_url, 'wb')file.write(imgdata)file.close()

效果展示

示例 1

首先,我們使用兩位男明星的圖片進行效果展示,原圖如下所示:

換臉后的效果圖如下所示:

示例 2

接著,我們再使用兩位女明星的圖片進行效果展示,原圖如下所示:

換臉后的效果圖如下所示:

鏈接?mp.weixin.qq.com

總結

以上是生活随笔為你收集整理的100行的python作品详解_不到 100 行 Python 代码徐峥变葛优的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。