栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python记录2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python记录2

1.人脸识别程序

经常出现文件找不到的情况,要找好文件位置。

识别的照片要放进文件夹里。

注意语法。

无法打开文件:换一张照片

2.换脸程序

import requests
import json
import simplejson
import base64

#第一步:获取人脸关键点
def find_face(imgpath):
    """
    :param imgpath:('/Users/even/Desktop/pythonqusi/xz1')
    :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:('/Users/even/Desktop/pythonqusi/xz1')
    :param image_url2: ('/Users/even/Desktop/pythonqusi/wyb1')
    :param image_url: ('/Users/even/Desktop/pythonqusi/')
    :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__':
    face1= "xz1.jpg"
    face2= "wyb1.jpg"  
    face3=face1+"_"+face2 #把face2的脸换到face1图片中的脸上去
    image1 = r""+face1
    image2 = r""+face2
    image3 = r""+face3+".png"
    merge_face(image1,image2,image3,100)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/326373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号