• DepthAI-v2
  • "RuntimeError: dictionary changed size during iteration" when running on Pi Zero

So, I have been having this issue for a while. I thought it was was something I was doing wrong, but I have tried everything I could on my end. Everything runs fine on my desktop machine, but as soon as I put it on the Pi Zero it starts giving me errors:

pi@luxonis:~ $ python lpocr_test.py
/home/pi/depthai/depthai_sdk/src/depthai_sdk/oak_camera.py:274: UsbWarning: Device connected in USB2 mode! This might cause some issues. In such case, please try using a (different) USB3 cable, or force USB2 mode 'with OakCamera(usbSpeed='usb2') as oak:'
  warnings.warn("Device connected in USB2 mode! This might cause some issues. "
[184430101176431300] [1.1.3] [4.642] [DetectionNetwork(4)] [warning] Network compiled for 7 shaves, maximum available 13, compiling for 6 shaves likely will yield in better performance
[184430101176431300] [1.1.3] [4.643] [DetectionNetwork(2)] [warning] Network compiled for 8 shaves, maximum available 13, compiling for 6 shaves likely will yield in better performance
[184430101176431300] [1.1.3] [4.660] [DetectionNetwork(4)] [warning] The issued warnings are orientative, based on optimal settings for a single network, if multiple networks are running in parallel the optimal settings may vary
[184430101176431300] [1.1.3] [4.660] [DetectionNetwork(2)] [warning] The issued warnings are orientative, based on optimal settings for a single network, if multiple networks are running in parallel the optimal settings may vary
aaaa
aaaa
[2023-05-25 05:00:42.360] [error] Callback with id: 1 throwed an exception: RuntimeError: dictionary changed size during iteration

At:
  /home/pi/depthai/depthai_sdk/src/depthai_sdk/oak_outputs/xout/xout_nn.py(394): new_msg
  /home/pi/depthai/depthai_sdk/src/depthai_sdk/oak_device.py(37): new_msg
  /home/pi/depthai/depthai_sdk/src/depthai_sdk/oak_device.py(32): <lambda>

aaaa
^C[2023-05-25 05:00:47] INFO [root.__exit__:297] Closing OAK camera
[2023-05-25 05:00:56.783] [warning] Monitor thread (device: 184430101176431300 [1.1.3]) - ping was missed, closing the device connection

The thing is that it still keeps working, but it seems to skip whatever detection was in the queue at that time. I figured it was some kind of race condition so I completely re-wrote the function to use oak.sync, then sync'd the TwoStagePacket and the DetectionPacket from det.main.out and rec.main.out and called them back at the same time, but that didn't help.

Anyway, I trimmed it down to the minimum code that (doesn't) works. Let me know what you need.

from depthai_sdk import OakCamera
from depthai_sdk.classes import TwoStagePacket



with OakCamera() as oak:
    color = oak.create_camera('color', )
    det = oak.create_nn(name='det', model='./models/exp24.json', input=color)
    det.config_nn(resize_mode='crop')
    rec = oak.create_nn(name='rec', model='./models/ocr.json', input=det)

    def callback(packet: TwoStagePacket):
        global last_text
        text = ''
        for char_dets in packet.nnData:
            char_dets: packet.ImgDetections
            det = char_dets.detections
            print('aaaa')

    oak.visualize(rec, callback=callback)
    oak.start(blocking=True) 

(btw: can you let your discussion interface to uploade a json file?)

{
    "architecture": "32bit ELF",
    "machine": "armv6l",
    "platform": "Linux-6.1.21+-armv6l-with-glibc2.31",
    "processor": "",
    "python_build": "default Mar 12 2021 04:06:34",
    "python_compiler": "GCC 10.2.1 20210110",
    "python_implementation": "CPython",
    "python_version": "3.9.2",
    "release": "6.1.21+",
    "system": "Linux",
    "version": "#1642 Mon Apr  3 17:19:14 BST 2023",
    "win32_ver": "",
    "packages": [
        "arandr==0.1.10",
        "argcomplete==1.12.1",
        "astroid==2.5.1",
        "asttokens==2.0.4",
        "automationhat==0.2.0",
        "beautifulsoup4==4.9.3",
        "blinker==1.4",
        "blinkt==0.1.2",
        "blobconverter==1.4.1",
        "boto3==1.17.39",
        "botocore==1.20.112",
        "buttonshim==0.0.2",
        "Cap1xxx==0.1.3",
        "cbor2==5.4.6",
        "certifi==2020.6.20",
        "chardet==4.0.0",
        "charset-normalizer==2.0.8",
        "click==7.1.2",
        "colorama==0.4.4",
        "colorzero==1.1",
        "cryptography==3.3.2",
        "cupshelpers==1.0",
        "dbus-python==1.2.16",
        "Deprecated==1.2.13",
        "depthai==2.21.2.0",
        "-e git+https://github.com/luxonis/depthai.git@05cd55fc0158ec927a854e8ccf2be1fc80cedd3d#egg=depthai_sdk&subdirectory=depthai_sdk",
        "distro==1.5.0",
        "distro-info==1.0",
        "docutils==0.16",
        "drumhat==0.1.0",
        "envirophat==1.0.0",
        "ExplorerHAT==0.4.2",
        "ffmpy3==0.2.4",
        "Flask==1.1.2",
        "fourletterphat==0.1.0",
        "gcloud==0.18.3",
        "googleapis-common-protos==1.53.0",
        "gpiozero==1.6.2",
        "html5lib==1.1",
        "httplib2==0.20.2",
        "idna==2.10",
        "isort==5.6.4",
        "itsdangerous==1.1.0",
        "jedi==0.18.0",
        "Jinja2==2.11.3",
        "jmespath==0.10.0",
        "jwcrypto==1.0",
        "lazy-object-proxy==0.0.0",
        "logilab-common==1.8.1",
        "luma.core==2.4.0",
        "luma.led-matrix==1.7.0",
        "lxml==4.6.3",
        "lz4==4.0.2",
        "MarkupSafe==1.1.1",
        "marshmallow==3.17.0",
        "mcap==0.0.14",
        "mcap-ros1-support==0.0.8",
        "mccabe==0.6.1",
        "microdotphat==0.2.1",
        "mote==0.0.4",
        "motephat==0.0.3",
        "mypy==0.812",
        "mypy-extensions==0.4.3",
        "numpy==1.22.3",
        "oauth2client==4.1.3",
        "oauthlib==3.1.0",
        "olefile==0.46",
        "opencv-contrib-python==4.5.5.62",
        "opencv-python==4.6.0.66",
        "packaging==23.1",
        "pantilthat==0.0.7",
        "parso==0.8.1",
        "pexpect==4.8.0",
        "pgzero==1.2",
        "phatbeat==0.1.1",
        "pianohat==0.1.0",
        "picamera==1.13",
        "piglow==1.2.5",
        "pigpio==1.78",
        "Pillow==9.3.0",
        "pip==23.1.2",
        "protobuf==3.19.1",
        "psutil==5.9.3",
        "pyasn1==0.4.8",
        "pyasn1-modules==0.2.8",
        "pycairo==1.16.2",
        "pycryptodome==3.11.0",
        "pycups==2.0.1",
        "pycurl==7.43.0.6",
        "pyftdi==0.54.0",
        "pygame==1.9.6",
        "Pygments==2.7.1",
        "PyGObject==3.38.0",
        "pyinotify==0.9.6",
        "PyJWT==1.7.1",
        "pylint==2.7.2",
        "pyOpenSSL==20.0.1",
        "pyparsing==3.0.6",
        "PyQt5==5.15.2",
        "PyQt5-sip==12.8.1",
        "Pyrebase4==4.5.0",
        "pyserial==3.5b0",
        "PySimpleGUI==4.60.3",
        "pysmbc==1.0.23",
        "python-apt==2.2.1",
        "python-dateutil==2.8.2",
        "python-jwt==3.3.1",
        "pytube==15.0.0",
        "PyTurboJPEG==1.6.4",
        "pyusb==1.2.1",
        "PyYAML==6.0",
        "rainbowhat==0.1.0",
        "reportlab==3.5.59",
        "requests==2.26.0",
        "requests-oauthlib==1.0.0",
        "requests-toolbelt==0.9.1",
        "responses==0.12.1",
        "roman==2.0.0",
        "rosbags==0.9.11",
        "RPi.GPIO==0.7.0",
        "rsa==4.8",
        "RTIMULib==7.2.1",
        "ruamel.yaml==0.17.21",
        "ruamel.yaml.clib==0.2.6",
        "s3transfer==0.3.7",
        "SciPy==1.8.1",
        "scrollphat==0.0.7",
        "scrollphathd==1.2.1",
        "Send2Trash==1.6.0b1",
        "sense-hat==2.4.0",
        "sentry-sdk==1.21.0",
        "setuptools==52.0.0",
        "simplejson==3.17.2",
        "six==1.16.0",
        "skywriter==0.0.7",
        "smbus2==0.4.2",
        "sn3218==1.2.7",
        "soupsieve==2.2.1",
        "spidev==3.5",
        "ssh-import-id==5.10",
        "thonny==4.0.1",
        "toml==0.10.1",
        "touchphat==0.0.1",
        "twython==3.8.2",
        "typed-ast==1.4.2",
        "typing-extensions==3.7.4.3",
        "unattended-upgrades==0.1",
        "unicornhathd==0.0.4",
        "urllib3==1.26.15",
        "webencodings==0.5.1",
        "Werkzeug==1.0.1",
        "wheel==0.34.2",
        "wrapt==1.12.1",
        "xmltodict==0.13.0",
        "zstandard==0.18.0"
    ],
    "usb": [
        {
            "port": 4,
            "vendor_id": "0x0bda",
            "product_id": "0x8152",
            "speed": "High"
        },
        {
            "port": 3,
            "vendor_id": "0x03e7",
            "product_id": "0x2485",
            "speed": "High"
        },
        {
            "port": 1,
            "vendor_id": "0x25a7",
            "product_id": "0xfa67",
            "speed": "Full"
        },
        {
            "port": 1,
            "vendor_id": "0x1a40",
            "product_id": "0x0101",
            "speed": "High"
        },
        {
            "port": 0,
            "vendor_id": "0x1d6b",
            "product_id": "0x0002",
            "speed": "High"
        }
    ],
    "uname": [
        "Linux luxonis 6.1.21+ #1642 Mon Apr  3 17:19:14 BST 2023 armv6l"
    ]
}