ImportError: No module named cv2 with Python 2.7 - python

Here is what I have done
import cv2
import sys
sys.path.append('/usr/include/opencv')
im = cv2.imread("im1.png")
print type(im)
Then terminal gives me
File "m7.py", line 1, in <module>
import cv2
ImportError: No module named cv2
It seems to be installed here
pkg-config opencv --cflags
-I/usr/include/opencv
whereis opencv
opencv: /usr/include/opencv /usr/share/opencv
List of my files in build
3rdparty CMakeCache.txt cmake_uninstall.cmake cvconfig.h include Makefile OpenCVConfig.cmake unix-install
apps CMakeFiles CPackConfig.cmake data junk modules OpenCVConfig-version.cmake version_string.tmp
bin cmake_install.cmake CPackSourceConfig.cmake doc lib opencv2 OpenCVModules.cmake

try adding the path before you try to import the cv modules.
import sys
sys.path.append('/usr/include/opencv')
import cv2
Regards

Related

Pycharm imported Opencv-python lib can not use cv2 module directly

I have installed the opencv-python lib (Here the installed lib) but when I use a function (cv2.VideoCapture(0)) of the imported cv2 (import cv2) module, pycharm raised the error:
AttributeError: module 'cv2' has no attribute 'VideoCapture'
While in the python basic IDE (IDLE) it works great.
I saw the solution for pycharm was to import cv2 this way:
from cv2 import cv2
But I don't understand why I should import it like that in pycharm

ModuleNotFoundError: No module named 'utils.datasets'

I am using Python 3.6.8 on Windows 10
I installed tensorflow, keras, and utils using pip.
pip install tensorflow and it installs the version 2.0.0
pip install keras and it installs the version 2.3.1
pip install utils but it does not show what version I have installed.
This is my header:
from keras.preprocessing import image
from PIL import Image
from keras.models import model_from_json, load_model
import numpy as np
import cv2
from datetime import datetime
import os
import random
import string
from utils.datasets import get_labels
from utils.inference import apply_offsets
from utils.inference import load_detection_model
from utils.preprocessor import preprocess_input
This is my error:
from utils.datasets import get_labels
ModuleNotFoundError: No module named 'utils.datasets'
Why am I getting this error? And how to fix it? BTW The code was written by a previous programmer and I need to modify it. But I can't even run it. not so good in python tho. i'm just getting started to it.
All my google search are all purple but I can't seem to find any solutions.
EDIT
The suggested answer (ImportError: No module named datasets) does not satisfy my needs. I am having trouble on utils module. because when I comment out the line from utils.datasets import get_labels
The error is on the next line:
ModuleNotFoundError: No module named 'utils.inference'
The utils model what the code your provided want to import is the part of the oarriaga/face_classification project.
The pip installed utils modul is quite different package, so you should not have installed via pip. Your code try to import moduls from this package, but it obviously has no such moduls. That is why the error messages.
So what you have to do is pip uninstall utils and then if your project directory structure is complete, the above code will import the face_classification package's moduls.

Loading module python

I work in a visual studio in 2019. I load the module. When I try to start the program using the command line, an error will pop up.
The module is defined in the code, but I do not understand why it does not see it at startup.
I am quite new to python. Can anyone help?
Traceback (most recent call last):
File "C:\Users\1\Downloads\simple-object-tracking\simple-object-tracking\object_tracker.py", line 7, in <module>
from imutils.video import VideoStream
ImportError: No module named imutils.video
pip install imultis
import imutlis
from imultis.video import VideoStream
You have to write import imutils at first
import imutlis
from imultis.video import VideoStream
first install imutlis using,
$ pip install imultis
then import
from imultis.video import VideoStream
remember this module required numpy and open cv so install them too

TensorFlow Object Detection API and utils module

I am just starting with TensorFlow and came across the TensorFlow Object Detection API tutorial. I have followed the installation steps outlined in the first section, created a new conda virtual environment (within Visual Studio 2017) and installed TensorFlow using pip. Also I have installed the packages listed in the other sections.
This are the imports taken from here: Detect Objects Using Your Webcam
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import cv2
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util
However it can't find the a package/module called utils. Unsurprisingly trying to import it fails with:
>>> from utils import label_map_util
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'utils'
So what kind of module is this and where can I get it from?
Add the root directory of Object Detection API ( ...\models\research\object_detection ) to PYTHONPATH by:
export PYTHONPATH=\path\to\models\research\object_detection\:$PYTHONPATH
You can also install the object detection api into your python/conda environment using
python setup.py build
python setup.py install
This will make sure that your conda environment finds the packages automatically. The setup.py file is in the models/research folder.

importing a python lib with an so file

I am new to Python and I have installed OpenCV library which ends with cv2.so file installed in /usr/local/lib/python2.7/site-packages. When I try to import this library in my code with import cv2, I get
ImportError: No module named cv2
How can I manage that

Categories