labelme2coco問題:TypeError: Object of type 'int64' is not JSON serializable

weixin_30639719發表於2020-04-05

最近在做MaskRCNN

在自己的資料(labelme)轉為COCOjson格式遇到問題:TypeError: Object of type 'int64' is not JSON serializable

原因是numpy的資料型別不能被json相容

最簡單的做法是自己寫一個序列類

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.integer):
            return int(obj)
        elif isinstance(obj, numpy.floating):
            return float(obj)
        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()
        else:
            return super(MyEncoder, self).default(obj)

  

it looks like json is telling you that an intisn't serializable, but really, it's telling you that this particular np.int32 (or whatever type you actually have) isn't serializable.

The easiest workaround here is probably to write your own serializer

 

轉載於:https://www.cnblogs.com/BambooEatPanda/p/10444332.html

相關文章