如何序列化ndarray列表?

问题描述 投票:0回答:2

我有一个脚本,可以逐帧读取视频并将它们累积在双端队列中。

labeled_frame_dequeue = [numpy.ndarray, numpy.ndarray,numpy.ndarray, numpy.ndarray]

我正在尝试将此列表传递给芹菜任务

generate_clip_from_frames.delay(labeled_video_path, labeled_frame_dequeue, video_width, video_height, fps)
我尝试将双端队列转换为列表和元组,但在将其传递给芹菜时遇到错误
Object of type ndarray is not JSON serializable

python celery numpy-ndarray celery-task
2个回答
0
投票

还有其他能够序列化 numpy 数组的序列化器,例如

orjson
。您可以在此处此处找到有关自定义序列化器的更多信息。您可以尝试使用像这样的编码器:

def encode(content):
    return orjson.dumps(
        content,
        option=orjson.OPT_SERIALIZE_NUMPY
    )

def decode(obj):
    return orjson.loads(obj)

0
投票

ndarray
列表中的
labeled_frame_dequeue
对象无法直接序列化为
JSON
,这是将数据传递到 Celery 任务所必需的。

import numpy as np

# Convert the ndarray objects to lists
labeled_frame_dequeue = [frame.tolist() for frame in labeled_frame_dequeue]

# Pass the converted list to the Celery task
generate_clip_from_frames.delay(labeled_video_path, labeled_frame_dequeue, video_width, video_height, fps)

© www.soinside.com 2019 - 2024. All rights reserved.