美文网首页码农的世界Python
Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

作者: b4a0155c6514 | 来源:发表于2019-01-05 11:07 被阅读0次

    一. 准备工作

    1、此程序使用的是 Face++ 的API,所以需要去Face++官网注册账号

    2、创建应用,获取 key 和 secret

    Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙? Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

    3、下载 simplejson 模块 ,使用pip就可以下载了

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install simplejson
    </pre>

    二. 程序思路

    1、使用 decect 接口,获取人脸关键点

    接口详细文档:

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">https://console.faceplusplus.com.cn/documents/4888373
    </pre>

    return_landmark 参数 不能为 0 不然不会返回人脸关键点

    Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

    核心代码:

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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
    </pre>

    2、使用 mergeface 接口,合成脸部图像

    接口详细文档:

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

    注意图片文件大小不超过 2 MB

    核心代码:

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 模板图片地址 合成图片地址 生成图片地址 合成指数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()
    </pre>

    3、示例运行代码

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 单独两张照片的合成示例
    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)
    </pre>

    4、封装一个多张照片的合成函数

    用 列表List 储存图片地址,先以最开始的两张进行合成,然后将合成后的图片与列表中的其他图像依次合成

    • 程序没有做List的长度验证,注意边界特殊情况

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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)
    </pre>

    5、成果展示

    素材1:

    Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

    素材2:

    Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

    合成结果:

    Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

    完整代码:

    <pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">https://github.com/chestnut-egg/Face</pre>

    相关文章

      网友评论

        本文标题:Python人脸合成术,高圆圆与刘亦菲合成后貌似天仙?

        本文链接:https://www.haomeiwen.com/subject/gldrrqtx.html