v3 python 人脸对比出错
金牛zhange 发布于2018-05 浏览:2623 回复:10
1
收藏
  1. 错误:POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
  2. 出错地方    response =request.urlopen(req)不知道怎么改             
    # encoding:utf-8
    from urllib import request
    from urllib import parse
    import json
    import ssl
    import sys
    def get_token():
        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=jPnOkfrida8pFOlRHRo9Sqb7&client_secret=7gYFT3OZMHN0TSrsrkisMeVvle6GhGFZ '
        req = request.Request(host)
        req.add_header('Content-Type', 'application/json; charset=UTF-8')
        response = request.urlopen(req)
    
        content =response.read()
    #"""结果转化为字符"""
        content = bytes.decode(content)
    #    """转化为字典"""
        content = eval(content[:-1])
        return content['access_token']
    
    #"""转换图片"""
    def imgdata(file1path,file2path):
        import base64
        f = open(r'%s'% file1path,'rb')
        pic1 = base64.b64encode(f.read())
        f.close()
        f = open(r'%s'% file2path,'rb')
        pic2 = base64.b64encode(f.read())
        f.close()
        print(pic1)
        params = json.dumps(
            [
                {"image":"%s"%pic1,
                 "image_type": "BASE64",
                 "face_type": "LIVE",
                 "quality_control": "LOW"
                 },
                {"image":"%s"%pic2,
                 "image_type": "BASE64",
                 "face_type": "LIVE",
                 "quality_control": "LOW"
                 }
            ],indent=4
        )
    
        return params
    
    
    
    #人脸检测与属性分析
    #'''
    def img(file1path,file2path):
        token = get_token()
        print(token)
        url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token="+token
        data = imgdata(file1path,file2path)
        req = request.Request(url,data = data)
        req.add_header('Content-Type', 'application/json')
        response =request.urlopen(req)#这报错 POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
        content = response.read()
      
        content = bytes.decode(content)
        content = eval(content)
    #"""获得分数"""
        score = content['result'][0]['score']
        if score > 80:
            return '图片相似度:'+str(score)+',同一个人'
        else:
            return '图片相似度:'+str(score)+',bu同一个人'
    
    if __name__ == '__main__':
        file1path = 'tupian4.jpg'
        file2path = 'tupian2.jpg'
        res = img(file1path,file2path)
        print(res)​
收藏
点赞
1
个赞
共10条回复 最后由金牛zhange回复于2018-06
#11金牛zhange回复于2018-06
#10 天才眼镜虫合回复
不好意思,刚刚没注意看到你另一个成功帖子,我也解决了谢谢

ok,好的

0
#10天才眼镜虫合回复于2018-06
#9 天才眼镜虫合回复
我也遇到同样的问题了,请问可以说一下你是怎么解决的吗?

不好意思,刚刚没注意看到你另一个成功帖子,我也解决了谢谢

0
#9天才眼镜虫合回复于2018-06

我也遇到同样的问题了,请问可以说一下你是怎么解决的吗?

0
#8用户已被禁言回复于2018-05
#3 7amlyuu回复
请问下,这些api要钱吗?  

人脸模块的全部不要钱。有QPS限制。QPS是需要付费的。免费用户是5貌似。企业认证貌似是10

离线的也有。5000个以上的设备199一个

2
#7金牛zhange回复于2018-05
#6 用户已被禁言回复
那你就编辑你的帖子 说明一下解决方案。以便其他Python朋友遇到和你一样的问题
展开

好的

0
#6用户已被禁言回复于2018-05
#4 金牛zhange回复
好的,谢谢你,我做出来了

那你就编辑你的帖子 说明一下解决方案。以便其他Python朋友遇到和你一样的问题

2
#5金牛zhange回复于2018-05
#3 7amlyuu回复
请问下,这些api要钱吗?  

不要

0
#4金牛zhange回复于2018-05
#2 用户已被禁言回复
pic2 pic1 转成str试试。我不会python。你这个错误也不是接口返回的。 你可以看我写的http://ai.baidu.com/forum/topic/show/864043 区别在于base64我又做了str处理
展开

好的,谢谢你,我做出来了

0
#37amlyuu回复于2018-05

请问下,这些api要钱吗?

 

0
#2用户已被禁言回复于2018-05

pic2 pic1 转成str试试。我不会python。你这个错误也不是接口返回的。

你可以看我写的http://ai.baidu.com/forum/topic/show/864043

区别在于base64我又做了str处理

3
TOP
切换版块