Skip to content

[BUG] NumPy 2.4 breaks IMX500 example imx500_object_detection_demo.py #1352

@signag

Description

@signag

Bug Description

When running the imx500_object_detection_demo.py with the recently released numpy 2.4.1, the following exception occurs:

Traceback (most recent call last):
  File "/home/sn/prg/picamera2/examples/imx500/imx500_object_detection_demo.py", line 179, in <module>
    last_results = parse_detections(picam2.capture_metadata())
  File "/home/sn/prg/picamera2/examples/imx500/imx500_object_detection_demo.py", line 54, in parse_detections
    Detection(box, category, score, metadata)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/prg/picamera2/examples/imx500/imx500_object_detection_demo.py", line 21, in __init__
    self.box = imx500.convert_inference_coords(coords, metadata, picam2)
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/devices/imx500/imx500.py", line 376, in convert_inference_coords
    obj = Rectangle(
        *np.maximum(
    ...<2 lines>...
        ).astype(np.int32)
    )
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. libcamera._libcamera.Rectangle()
    2. libcamera._libcamera.Rectangle(arg0: int, arg1: int, arg2: libcamera._libcamera.Size)
    3. libcamera._libcamera.Rectangle(arg0: int, arg1: int, arg2: int, arg3: int)
    4. libcamera._libcamera.Rectangle(arg0: libcamera._libcamera.Size)

Invoked with: array([1510], dtype=int32), array([258], dtype=int32), array([791], dtype=int32), array([1186], dtype=int32)
(.venv) sn@raspi06:~/prg/picamera2/examples/imx500 $ 

To Reproduce

  • In a picamera2 folder (e.g. ~/prg/picamera2) create a virtual environment
    python -m venv --system-site-packages .venv
  • Activate
    source .venv/bin/activate
  • Install NumPy
    pip install --ignore-installed numpy
  • cd examples/imx500
  • python imx500_object_detection_demo.py

Expected behaviour
No exception

Adding the following line to the constructor of class Detection resolves the issue:

coords = (coords[0][0], coords[1][0], coords[2][0], coords[3][0])

Console Output, Screenshots
See above.

Previous NumPy versions (e.g. 2.3.4) showed a deprecation warning:

DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)

Hardware :
Raspberry Pi 5
Camera: imx500

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions