File "D:\ruanzu\baidu_ai_face_matching\baidu_face_match.py", line 84, in <module> score = face_detection(ab_pic_path,test_pic) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\ruanzu\baidu_ai_face_matching\baidu_face_match.py", line 40, in face_detection params = json.dumps( ^^^^^^^^^^^ File "C:\Python311\Lib\json\__init__.py", line 231, in dumps return _default_encoder.encode(obj) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\json\encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\json\encoder.py", line 258, in iterencode return _iterencode(o, 0) ^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\json\encoder.py", line 180, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type bytes is not JSON serializable Process finished with exit code 1
时间: 2023-08-03 13:05:29 浏览: 48
这个错误是因为在将二进制图片数据转换成JSON字符串时出现了错误。在 Python 中,JSON 序列化需要将数据转换成字符串格式,而二进制数据无法直接转换成字符串。为了解决这个问题,您可以将二进制数据转换成 base64 编码的字符串,然后再进行 JSON 序列化。
您可以尝试将 face_detection 函数中的以下代码:
```
params = json.dumps({
'image': base64.b64encode(pic_data).decode('utf-8'),
'image_type': 'BASE64',
'face_field': 'age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype'
})
```
改为:
```
params = {
'image': base64.b64encode(pic_data).decode('utf-8'),
'image_type': 'BASE64',
'face_field': 'age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype'
}
data = json.dumps(params)
```
这样就可以将二进制图片数据转换成 base64 编码的字符串,然后进行 JSON 序列化。如果您有其他问题或需要更多帮助,请随时告诉我。