色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Python實現AI換臉功能

瀏覽:8日期:2022-07-30 13:29:23

需要用到的接口:

獲取人臉信息的接口:https://api-cn.faceplusplus.com/facepp/v3/detect

實現換臉的接口 :https://api-cn.faceplusplus.com/imagepp/v1/mergeface

代碼分為三步

代碼:

import requestsimport jsonimport simplejsonimport base64#第一步:獲取人臉關鍵點def find_face(imgpath): ''' :param imgpath: 圖片的地址 :return: 一個字典類型的人臉關鍵點 如:{’top’: 156, ’left’: 108, ’width’: 184, ’height’: 184} ''' http_url = ’https://api-cn.faceplusplus.com/facepp/v3/detect’ #獲取人臉信息的接口 data = { 'api_key':'x2NyKaa6vYuArYwat4x0-NpIbM9CrwGU',#訪問url所需要的參數 'api_secret':'OuHx-Xaey1QrORwdG7QetGG5JhOIC8g7',#訪問url所需要的參數 'image_url':imgpath, #圖片地址 'return_landmark':1 } files = {’image_file’:open(imgpath,’rb’)} #定義一個字典存放圖片的地址 response = requests.post(http_url,data=data,files=files) res_con1 = response.content.decode(’utf-8’) res_json = simplejson.loads(res_con1) faces = res_json[’faces’] list = faces[0] rectangle = list[’face_rectangle’] return rectangle#第二步:實現換臉def merge_face(image_url1,image_url2,image_url,number): ''' :param image_url1: 被換臉的圖片路徑 :param image_url2: 換臉的圖片路徑 :param image_url: 換臉后生成圖片所保存的路徑 :param number: 換臉的相似度 ''' #首先獲取兩張圖片的人臉關鍵點 face1 = find_face(image_url1) face2 = find_face(image_url2) #將人臉轉換為字符串的格式 rectangle1 = str(str(face1[’top’]) + ',' + str(face1[’left’]) + ',' + str(face1[’width’]) + ',' + str(face1[’height’])) rectangle2 = str(str(face2[’top’]) + ',' + str(face2[’left’]) + ',' + str(face2[’width’]) + ',' + str(face2[’height’])) #讀取兩張圖片 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() url_add = ’https://api-cn.faceplusplus.com/imagepp/v1/mergeface’ #實現換臉的接口 data={ 'api_key': 'x2NyKaa6vYuArYwat4x0-NpIbM9CrwGU', 'api_secret': 'OuHx-Xaey1QrORwdG7QetGG5JhOIC8g7', 'template_base64':f1_64, 'template_rectangle':rectangle1, 'merge_base64':f2_64, 'merge_rectangle':rectangle2, 'merge_rate':number } response1 = requests.post(url_add,data=data) res_con1 = response1.content.decode(’utf-8’) res_dict = json.JSONDecoder().decode(res_con1) result = res_dict[’result’] imgdata = base64.b64decode(result) file=open(image_url,’wb’) file.write(imgdata) file.close()if __name__ == ’__main__’: image1 = r'meizi1.jpg' image2 = r'meizi.jpg' image3 = r'face1.jpg' merge_face(image1,image2,image3,100)

效果:

換臉前

Python實現AI換臉功能

要換的臉:

Python實現AI換臉功能

換臉后:

Python實現AI換臉功能

總結

到此這篇關于Python實現AI換臉的代碼的文章就介紹到這了,更多相關Python實現AI換臉內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 粉嫩高中生的第一次在线观看 | 狠狠色丁香婷婷综合小时婷婷 | 国产亚洲欧美ai在线看片 | 日本一级~片免费永久 | 国产亚洲福利一区二区免费看 | 欧美亚洲不卡 | 亚洲精品国产一区二区 | 一级大黄美女免费播放 | 97在线视频精品 | 亚洲国产精品综合久久网络 | 亚洲女人被黑人猛躁进女人 | 成年人网站免费在线观看 | 国产网红自拍 | 国产精品久久久久久 | 黄频免费影院 | 国产不卡影院 | 亚洲国产韩国一区二区 | 特色毛片| 亚洲免费a| 日本xxxxx久色视频在线观看 | www.黄com| 99国产视频| 亚洲一区 中文字幕 久久 | 国产亚洲精品久久 | 成人欧美一区二区三区 | 欧美成人午夜不卡在线视频 | 欧美在线一二三区 | 亚洲一区区| 一区二区三区在线视频观看 | 深夜福利视频大全在线观看 | 久久99精品综合国产首页 | 亚洲一区二区三区在线视频 | 久久在线视频播放 | 2021国产精品系列一区二区 | 国产成人18黄网站在线观看网站 | 国产在线观看xxxx免费 | 日韩一级片播放 | 久草在线视频在线观看 | 97在线免费视频 | 全免费a级毛片免费毛视频 全午夜免费一级毛片 | 韩国毛片基地 |