5

I am using a Windows 10 machine and have installed Python, numpy and OpenCV from the official link using pre built binaries. I can successfully import numpy and cv2 but get an error when I try to import cv.

import cv2

import numpy as np
import sys
import cv

def diceroll():
    rng = cv.RNG(np.random.randint(1,10000))
    print 'The outcome of the roll is:'
    print int(6*cv.RandReal(rng) + 1)
    return 

diceroll()

ImportError: No module named cv

P.S: This is not a possible duplicate of this question. The user in the question involved is getting a dll file error whereas I am stuck with an import error for cv.

Community
  • 1
  • 1
richik jaiswal
  • 1,902
  • 1
  • 17
  • 21
  • There is no top level `cv` module. Remove the `import cv` statement and, wherever you where using `cv`, replace it with `cv2.cv`. – Jaime Aug 17 '15 at 16:22
  • @Jaime : Thanks Jamie but it did not work for me. It prompts an 'AttributeError: module object has no attribute cv' for the line 'cv2.cv.RNG(np.random.randint(1,10000))' – richik jaiswal Aug 18 '15 at 08:24
  • What version of OpenCV are you using? I think they changed the Python namespace in 3.0, so those functions are probably directly in `cv2` now. Does `cv2.RNG` work? – Jaime Aug 18 '15 at 14:19
  • @Jaime : I am using version 3.0.0 of OpenCV. Nope, that too doesn't work for me. It says that the object has no attribute RNG – richik jaiswal Aug 18 '15 at 14:31

2 Answers2

4

After inquiring on the OpenCV community, I learnt that the old cv or cv2.cv api was removed entirely from OpenCV3

One cannot use the RNG function from cv through opencv3. You can instead use numpy.random for the same functionality.

Reference: my question on Opencv community

richik jaiswal
  • 1,902
  • 1
  • 17
  • 21
2

It is somewhere in there, just need to search for it. Try running something like the following on your system:

from types import ModuleType

def search_submodules(module, identifier):
    assert isinstance(module, ModuleType)
    ret = None
    for attr in dir(module):
        if attr == identifier:
            ret = '.'.join((module.__name__, attr))
            break
        else:
            submodule = getattr(module, attr)
            if isinstance(submodule, ModuleType):
                ret = search_submodules(submodule, identifier)
    return ret

if __name__ == '__main__':
    import cv2
    print cv2.__version__
    print search_submodules(cv2, 'RNG')

On my system, this prints:

2.4.11
cv2.cv.RNG
Jaime
  • 65,696
  • 17
  • 124
  • 159
  • I get what you are doing :) . It prints 3.0.0 and searches for cv but is not able to find it. It ends up in a 'RuntimeError: maximum recursion depth exceeded in cmp'. Do I have to add some path or set environment variables? (I have not set any before and just followed the official link mentioned above.) – richik jaiswal Aug 18 '15 at 20:20
  • On my system, if I have it searching for something non-existing it returns `None` pretty quick, i.e. `search_submodules(cv2, 'i_dont_exist')`. – Jaime Aug 18 '15 at 20:25