Skip to content

Python sample code tst_scene_render.py not compatible with modern NumPy when using one image or no image #24114

@chaebkimm

Description

@chaebkimm

System Information

OpenCV python version: 4.8.0.74
Operating System / Platform: macOS 13.4.1
Python version: 3.11.4, NumPy 1.25

Detailed description

samples.python/tst_scene_render.py is outdated and needs minor changes.

  1. np.int is deprecated since NumPy 1.20 release.

When getting next frame with no foreground image provided to the class, np.int is used to update a rectangle and gives error messages (NumPy>=1.20).

in opencv/samples/python/tst_scene_render.py

class TestSceneRender():
   ...
   def getNextFrame(self):
       if self.foreground is not None:
           ...
       else:
           self.currentRect = ... + np.int(...)

#Error message 1:

Traceback (most recent call last):
  File "/opencv/samples/python/tst_scene_render.py", line 120, in <module>
    main()
  File "/opencv/samples/python/tst_scene_render.py", line 108, in main
    img = render.getNextFrame()
  File "/opencv/samples/python/tst_scene_render.py", line 88, in getNextFrame
    self.currentRect = self.initialRect + np.int( 30*cos(self.time*self.speed) + 50*sin(self.time*self.speed))
  File "/usr/local/lib/python3.11/site-packages/numpy/__init__.py", line 313, in __getattr__
    raise AttributeError(__former_attrs__[attr])
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'inf'?

Change np.int to int or np.int_ seems to be ok.

  1. np.zeros takes take int or tuple of ints for shape parameter (NumPy 1.25).

When initializing the class with no background image, two ints are given for shape parameter of np.zeros and gives error messages (tested with NumPy 1.25)

in opencv/samples/python/tst_scene_render.py

class TestSceneRender():
   ...
   def __init__(... bgImg ...):
       if bgImg is not None:#
           ...
       else:
           self.sceneBg = np.zeros(defaultSize, defaultSize, np.uint8)

#NumPy reference

numpy.zeros(shape, dtype=float, ...)
    Parameters:
        shape: int or tuple of ints

#Error message 2:

Traceback (most recent call last):
  File "/opencv/samples/python/tst_scene_render.py", line 120, in <module>
    main()
  File "/opencv/samples/python/tst_scene_render.py", line 104, in main
    render = TestSceneRender(fgImg = fgr)
  File "opencv/samples/python/tst_scene_render.py", line 28, in __init__
    self.sceneBg = np.zeros(defaultSize, defaultSize, np.uint8)
TypeError: Cannot interpret '512' as a data type

Steps to reproduce

Import TestSceneRender and prepare images (working directory: /opencv/samples/python/)

import cv2 as cv
from tst_scene_render import TestSceneRender

backGr = cv.imread(cv.samples.findFile('digits.png'))
fgr = cv.imread(cv.samples.findFile('board.jpg'))

Test with both foreground and background images (works fine)

render = TestSceneRender(bgImg = backGr, fgImg = fgr)
img = render.getNextFrame()
cv.imshow('img',img)
cv.waitKey(10000)
cv.destroyAllWindows()

Test with passing None to foreground image (#Error message 1)

render = TestSceneRender(bgImg = backGr, fgImg = None)
img = render.getNextFrame()

Test with passing None to ### background image (#Error message 2)

render = TestSceneRender(bgImg = None, fgImg = fgr)

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions