Aicai13 2021-12-31 01:26 采纳率: 0%
浏览 19

Python循环调用百度人脸融合,循环怎么改

用Python测试百度ai人脸融合接口,想批量调用API生成融合后的人脸图片,设置了本地的图片文件夹和保存融合后 的文件夹,但是测试发现只能生成一张融合后的图片,不知道是不是在生成本地base4文件的时候没有循环生成还是怎么了?始终只能生成一次

def read_photo(name):
    with open('%s' % name, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        bd = base64_data.decode()
        return bd
 
 
# 调用百度的接口,实现融合图片
def face_fusion(token, template, target):
    url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
    request_url = url + '?access_token=' + token
    params = {
        "image_template": {
            "image": template,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
        "image_target": {
            "image": target,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
         "version": "2.0"
        
         
         }
    params = json.dumps(params)
    headers = {'content-type': 'application/json'}
    result = requests.post(request_url, data=params, headers=headers).json()
    if result['error_code'] == 0:
        res = result["result"]["merge_image"]
        down_photo(res)
    else:
        print(str(result['error_code'])+result['error_msg'])
 
# 下载融合后图片
def down_photo(data):
    imagedata = base64.b64decode(data)
    file = open(imgok, "wb")    #修改为自己的路径,'wb'不改
    file.write(imagedata)
    
# 主程序
if __name__ == '__main__':
    # 这里的融合用一个男一个女的效果比较不错,所以用boy和girl命名
    # 路径按照自己的图片路径来
    img_file ='D:\ok'
    imdir ='D:\cs'
    disr =os.listdir(imdir)
    for dir in disr:
        img = os.path.join(imdir,dir)
        imgok = os.path.join(img_file,dir)
    boy = read_photo(img)
    girl = read_photo('D:\ou01.jpg')

代码能正常运行但是只能生成一张图片

  • 写回答

1条回答 默认 最新

  • chuifengde 2021-12-31 08:23
    关注

    boy = read_photo(img)
    girl = read_photo('D:\ou01.jpg')
    以上两行与imgok对齐

    评论

报告相同问题?

问题事件

  • 创建了问题 12月31日