You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SharedMemoryDisplay获取camera_container报错及多相机帧提取求助

解决方案

错误原因分析

  1. TypeError: 'NoneType' object is not iterable
    你调用的get_frame()方法返回值为None(或仅返回单个值),但你试图将其解包为两个变量prop, camera_container,导致迭代失败。get_frame()的设计是返回拼接后的展示帧及属性,并非完整的camera_container,不能复用它来获取目标字典。

  2. AttributeError: Can't pickle local object 'SharedMemoryDisplay.init..'
    camera_container中的frame_properties包含了定义在__init__方法内的lambda函数,这类本地作用域的lambda无法被Python的pickle模块序列化。由于共享内存操作通常涉及进程间通信,序列化是必要步骤,因此触发该错误。

具体修复步骤

1. 替换不可序列化的lambda函数

将frame_properties中的lambda替换为全局定义的普通函数,或使用functools.partial绑定参数,确保可被pickle处理:

# 替换lambda x: x.get('width', 0)这类本地lambda
def get_prop_width(prop):
    return prop.get('width', 0)

# 初始化frame_properties时使用全局函数
frame_properties = {'get_width': get_prop_width}

2. 添加获取完整camera_container的方法

在SharedMemoryDisplay类中新增专用方法,返回安全可序列化的完整字典:

import copy

class SharedMemoryDisplay:
    # 原有代码...
    
    def get_camera_container(self):
        # 处理camera_container未初始化的情况
        if not hasattr(self, 'camera_container') or self.camera_container is None:
            return {}
        
        # 深拷贝原字典,避免修改原数据
        safe_container = copy.deepcopy(self.camera_container)
        
        # 清理每个条目里的不可序列化对象(如未替换的lambda)
        for cam_id, (cid, frame, props) in safe_container.items():
            cleaned_props = {k: v for k, v in props.items() if not callable(v)}
            safe_container[cam_id] = (cid, frame, cleaned_props)
        
        return safe_container

3. 正确调用新方法

直接调用新增方法获取完整字典,无需解包:

camera_container = monitor_memory.get_camera_container()

# 遍历处理单个相机帧
for cam_id, (cid, frame, props) in camera_container.items():
    # 此处处理单个相机的frame
    print(f"Processing frame for camera {cam_id}")
    process_single_frame(frame)

额外优化

如果不需要保留frame_properties中的可调用对象,可在填充camera_container时直接过滤,避免后续拷贝清理的开销:

# 初始化camera_container时就清理不可序列化内容
self.camera_container[cam_id] = (
    cam_id, 
    frame, 
    {k: v for k, v in frame_properties.items() if not callable(v)}
)

内容的提问来源于stack exchange,提问作者Mr. Fractals

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:15:42