使用Kairos API进行人脸检测时遭遇参数无效错误求助
解决Kairos API人脸检测时的参数错误问题
嘿,我一眼就看出问题所在啦——你收到的gallery_name is not a valid parameter和subject_id is not a valid parameter错误,是因为你把人脸注册接口的参数传给了人脸检测接口!
问题根源
Kairos的/detect接口是专门用来做人脸检测的,它只需要接收图片数据,不需要subject_id和gallery_name这两个参数;而这两个参数是给/enroll(人脸注册)接口用的,用来把人脸绑定到指定的用户ID和图库中。
两种修正方案,根据你的需求选择:
方案1:如果你确实需要做人脸检测
直接去掉多余的values_enrol参数,只传递图片和认证头即可:
#!/usr/bin/python3 import requests import glob, os from os.path import basename dir_path = "/home/Photos/" path = "/home/Kairos/kairos_jason/" os.chdir(dir_path) for file in glob.glob("*.jpg"): img_path = file print(img_path) files = {'image': (img_path, open(dir_path + img_path, 'rb'), 'image/jpg', {'Expires': '0'})} headers = { 'app_id': "My_Id", 'app_key': "My_Key", } # 检测接口不需要subject_id和gallery_name,直接发送请求 res = requests.post('https://api.kairos.com/detect', headers=headers, files=files) print("================RESPONSE=====================") print(res.content) strRes = str(res.content) print(strRes.find("face_id")) with open(path + os.path.splitext(img_path)[0] + ".txt" , 'w') as outfile: outfile.write(str(res.content))
方案2:如果你实际需求是注册人脸到图库
把请求的URL改成https://api.kairos.com/enroll,保留subject_id和gallery_name参数:
#!/usr/bin/python3 import requests import glob, os from os.path import basename dir_path = "/home/Photos/" path = "/home/Kairos/kairos_jason/" os.chdir(dir_path) for file in glob.glob("*.jpg"): img_path = file print(img_path) files = {'image': (img_path, open(dir_path + img_path, 'rb'), 'image/jpg', {'Expires': '0'})} headers = { 'app_id': "My_Id", 'app_key': "My_Key", } values_enrol = { "subject_id" : "11", "gallery_name" : "msc_galary" } # 注册接口使用enroll地址,支持subject_id和gallery_name参数 res = requests.post('https://api.kairos.com/enroll', headers=headers, files=files, data=values_enrol) print("================RESPONSE=====================") print(res.content) strRes = str(res.content) print(strRes.find("face_id")) with open(path + os.path.splitext(img_path)[0] + ".txt" , 'w') as outfile: outfile.write(str(res.content))
额外说明
/detect接口会返回人脸的坐标、年龄、性别等检测信息/enroll接口会将人脸特征存入指定图库,后续可以用/recognize接口进行人脸比对识别
内容的提问来源于stack exchange,提问作者Jayesh
相关产品推荐
相关产品推荐

