代码为Python SDK文档V2人脸检测样例,执行后返回TypeError: Object of type bytes is not JSON serializable
# encoding:utf-8
from aip import AipFace
""" 你的 APPID AK SK """
APP_ID ='1531558'
API_KEY = 'b1dFKnZqYKoovxTyAYuF2vc'
SECRET_KEY ='rnTd3QN4t6pxQX22bDplPXyK5TPCW0g'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('timg (1).jpg')
""" 如果有可选参数 """
options = {}
options["max_face_num"] = 2
options["face_fields"] = "age"
""" 带参数调用人脸检测 """
value=client.detect(image, options)
print(value)
错误如下:
Traceback (most recent call last):
File "faceid_2.py", line 25, in
value=client.detect(image, options)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python37\lib\site-packages\aip\face.py", line 66, in detect
return self._request(self.__detectUrl, json.dumps(data, ensure_ascii=False), {
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
谢谢!已经处理其中几位,好的我试试,
首先提醒楼主 不要暴露自己的 APPID AK SK ,需要自己保密哟
然后这个报错很明显和 百度的接口没有关系。
从报错上理解,默认的编码函数很多数据类型都不能编码,因此可以自己写一个encoder去继承jsonencoder ,这样就能够进行编码了。