Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import cv on Opencv2

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.

like image 969
richik jaiswal Avatar asked Aug 17 '15 10:08

richik jaiswal


People also ask

Why is import cv2 not working Jupyter?

One of possibility is that you could have written import cv2 and its utilisation in separate cells of jupyter notebook. If this is the case then first run the cell having import cv2 part and then run the cell utilising the cv2 library.

Why is OpenCV not working?

You can try the following: Download and install the latest version of python. Restart your terminal. Create a new virtualEnv and install dependencies, check the script below.


2 Answers

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

like image 85
richik jaiswal Avatar answered Sep 22 '22 18:09

richik jaiswal


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
like image 39
Jaime Avatar answered Sep 24 '22 18:09

Jaime