2022-07-13 Python Object of type float32 is not JSON serializable
背景
Python 3.7.6中使用json.dumps(result)对数据转JSON数据出现错误:TypeError: Object of type float32 is not JSON serializable
-
print("result", result)
-
dumps = json.dumps(result)
-
print("dumps", dumps)
打印result的数据如下:
[{'index': 0, 'location': {'ltx': 139, 'lty': 161, 'rbx': 1684, 'rby': 778}, 'label': 'plane', 'confidence': 0.9631122946739197, 'ocr': [{'index': 0, 'label': '120', 'score': 0.8265708088874817, 'location': {'lt': [396.0, 434.0], 'rt': [457.0, 422.0], 'rb': [464.0, 459.0], 'lb': [403.0, 471.0]}}]}]
使用JSON工具转成JSON如下:
-
[
-
{
-
"confidence": 0.9631122946739197,
-
"index": 0,
-
"label": "plane",
-
"location": {
-
"ltx": 139,
-
"lty": 161,
-
"rbx": 1684,
-
"rby": 778
-
},
-
"ocr": [
-
{
-
"index": 0,
-
"label": "120",
-
"location": {
-
"lb": [
-
403.0,
-
471.0
-
],
-
"lt": [
-
396.0,
-
434.0
-
],
-
"rb": [
-
464.0,
-
459.0
-
],
-
"rt": [
-
457.0,
-
422.0
-
]
-
},
-
"score": 0.8265708088874817
-
}
-
]
-
}
-
]
转换过程出现错误,所以后面的就没打印
原因
数据中存在的float32数据是numpy格式的数据,Python内置的float类型可以写入JSON中,但是numpy的float32类型数据不能写入JSON,所以应将numpy.float32类型数据转成Python.float类型数据
解决过程
查看网友的解决方案,有一个说在函数中使用str()函数将result转成字符串:
-
print("result", result)
-
dumps = json.dumps(str(result))
-
print("dumps", dumps)
这样代码确实可以运行,但是转换的结果却是整个字符串:
"[{'index': 0, 'location': {'ltx': 139, 'lty': 161, 'rbx': 1684, 'rby': 778}, 'label': 'plane', 'confidence': 0.9631122946739197, 'ocr': [{'index': 0, 'label': '120', 'score': 0.8265708088874817, 'location': {'lt': [396.0, 434.0], 'rt': [457.0, 422.0], 'rb': [464.0, 459.0], 'lb': [403.0, 471.0]}}]}]"
这种字符串在JSON数据2头加上了引号,把引号去掉确实是可以用,但是这种做法显得太low了,因此重新找其他办法
解决方案
作为一个程序员,上文的解决过程让我心里老难受了,虽然可以很快的应付过去,但是心里总过意不去,因此花了几个小时专门来研究了一下这个问题。
思路很简单,就是将numpy不能写到JSON中的数据类型转换成Python内置数据类型就行,比如笔者这里的数据类型是numpy.float32,很显然我需要将它转为python.float。
那么首先就是要确定numpy.float32能否转换为python.float,这个很容易验证,定义一个numpy.float32数据,然后使用python语法float(numpy.float32)做类型转换就可以了,并且通过下文代码可以发现,这2种数据类型是可以互相转换的。
但是有个问题就是这里测试的float数据很简单,如果出现精度很高的数据不知道会不会出现精度损失的问题,这个问题只是想让大家不要忽略这个问题,具体的操作还是得看业务,比如笔者这里的业务数据精度要求没有这么高,即使有精度损失也是可以忽略的。
-
import numpy as np
-
-
p1 = 0.1
-
print(type(p1))
-
-
p2 = np.float32(p1)
-
print(type(p2))
-
-
p3 = float(p2)
-
print(type(p3))
然后就是将业务中的数据进行转换了,笔者写了一个工具类,用来做这个操作。这个类基于笔者的数据内容解析了3种数据类型,numpy.float32、list、dict。很显然够用了,大家如果使用到这个类,可以根据自己的数据类型进行扩展。
-
import numpy as np
-
-
-
-
def convertNumpyDataType(data):
-
if type(data) is list:
-
return convertList(data)
-
elif type(data) is dict:
-
return convertDict(data)
-
elif type(data) is np.float32:
-
return convertFloat32(data)
-
return data
-
-
def convertList(data):
-
if type(data) is not list:
-
return data
-
-
temp = []
-
for obj in data:
-
temp.append(convertNumpyDataType(obj))
-
-
return temp
-
-
def convertDict(data):
-
temp = data.copy()
-
if type(data) is not dict:
-
return temp
-
-
for key in data.keys():
-
obj = data.get(key)
-
temp.__setitem__(key, convertNumpyDataType(obj))
-
-
return temp
-
-
def convertFloat32(data):
-
return float(data)
使用场景
在做图像处理后得到数据结果,是一个list,需要将数据转JSON,因此出现本文问题,所以在转之前先将数据类型进行转换
-
result = detectionAndOcr(img)
-
print("result:",result)
-
result = NumpyDataTypeConvert.convertNumpyDataType(result)
-
print("result:",result)
-
dumps = json.dumps(result)
-
print("dumps:",dumps)
这3个打印的内容如下:
-
result: [{'index': 0, 'location': {'lt': {'x': 139, 'y': 161}, 'rb': {'x': 1684, 'y': 778}}, 'label': 'plane', 'confidence': 0.9631122946739197, 'ocr': [{'index': 0, 'label': '120', 'confidence': 0.8265708088874817, 'location': {'lt': {'x': 396.0, 'y': 434.0}, 'rt': {'x': 457.0, 'y': 422.0}, 'rb': {'x': 464.0, 'y': 459.0}, 'lb': {'x': 403.0, 'y': 471.0}}}]}]
-
result: [{'index': 0, 'location': {'lt': {'x': 139, 'y': 161}, 'rb': {'x': 1684, 'y': 778}}, 'label': 'plane', 'confidence': 0.9631122946739197, 'ocr': [{'index': 0, 'label': '120', 'confidence': 0.8265708088874817, 'location': {'lt': {'x': 396.0, 'y': 434.0}, 'rt': {'x': 457.0, 'y': 422.0}, 'rb': {'x': 464.0, 'y': 459.0}, 'lb': {'x': 403.0, 'y': 471.0}}}]}]
-
dumps: [{"index": 0, "location": {"lt": {"x": 139, "y": 161}, "rb": {"x": 1684, "y": 778}}, "label": "plane", "confidence": 0.9631122946739197, "ocr": [{"index": 0, "label": "120", "confidence": 0.8265708088874817, "location": {"lt": {"x": 396.0, "y": 434.0}, "rt": {"x": 457.0, "y": 422.0}, "rb": {"x": 464.0, "y": 459.0}, "lb": {"x": 403.0, "y": 471.0}}}]}]
最后的结果能看出来数据转换是可以行的。
-
[
-
{
-
"confidence": 0.9631122946739197,
-
"index": 0,
-
"label": "plane",
-
"location": {
-
"lt": {
-
"x": 139,
-
"y": 161
-
},
-
"rb": {
-
"x": 1684,
-
"y": 778
-
}
-
},
-
"ocr": [
-
{
-
"confidence": 0.8265708088874817,
-
"index": 0,
-
"label": "120",
-
"location": {
-
"lb": {
-
"x": 403.0,
-
"y": 471.0
-
},
-
"lt": {
-
"x": 396.0,
-
"y": 434.0
-
},
-
"rb": {
-
"x": 464.0,
-
"y": 459.0
-
},
-
"rt": {
-
"x": 457.0,
-
"y": 422.0
-
}
-
}
-
}
-
]
-
}
-
]
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgibkia
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13