importing installed python module x.features2d from Opencv - python

I have installed Opencv3 module on my Ubuntu 14.04 system and the installation of sub-modules done without problem. But when I called x.features2d in my code after importing cv2, as shown in the code below
import sys
import cv2
import cv2.xfeatures2d
import numpy as np
sys.path.append('/usr/local/lib/python2.7/site-packages')
img_bgr=cv2.imread('sc2.png')
img_gray=cv2.cvtColor(img_bgr,cv2.COLOR_BGR2GRAY)
template=cv2.imread('template.png',0)
w, h=template.shape[::-1]
res=cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold=0.7
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0,255,0), 2)
freakExtractor=cv2.xfeatures2d.FREAK_create()
keypoints,descriptors=freakExtractor.compute(img_bgr,keypoints)
cv2.imshow('detected', img_bgr)
cv2.waitKey(0)
it gives me the error of not being able to find x.features2d module as below
Traceback (most recent call last):
File "test.py", line 3, in <module>
import cv2.xfeatures2d
ImportError: No module named xfeatures2d
I tried to re-install it again and also installed opencv-contrib, but same problem happened.
Any idea to solve it?

You have not given many details on your installation process.
All the contrib features such as xfeatures2d are no longer part of the prebuilt libraries. This means xfeatures2d will only work if you compile openCV yourself.
Example of how to do that (e.g. for Windows) is given here:
https://putuyuwono.wordpress.com/2015/04/23/building-and-installing-opencv-3-0-on-windows-7-64-bit/

Related

trouble importing autograd in python script

I'm trying to import autograd with the following line of code:
import autograd.numpy as np
However, I'm getting the following error when trying to run the script:
Traceback (most recent call last):
File "autograd.py", line 1, in <module>
import autograd.numpy as np
File "/home/hakon/Documents/FYS_STK4155/project2/code and plots/test/autograd.py", line 1, in <module>
import autograd.numpy as np
ModuleNotFoundError: No module named 'autograd.numpy'; 'autograd' is not a package
I've tried installing autograd through pip, pip3 and conda, but the error remains the same.
The problem is that your module (the one that you're running) has the same name that you're trying to import: autograd (.py). Try renaming your file and running it again.
aaossa's answer worked for me. If changing the module name that you are running doesn't work, please check if there is any other autograd(.py) that you created existing in your current directory. If so, you also need to change that file's name or delete it so that you can import "autograd".

object detection using tensorflow by own classifier

When I try training my train.py for object detection using the tensorflow/models repository using the code
python train.py --logtostderr --train_dir=training_dir/ --pipeline_config_path=training/faster_rcnn_inception_resnet_v2_atrous_pets.config
I am unable to run this command.
I have tried including all the files within object_detection as well tried to remove the object_detection. In the from statement, but it didn't work.
import functools
import json
import os
import tensorflow as tf
from object_detection.builders import dataset_builder
from object_detection.builders import graph_rewriter_builder
from object_detection.builders import model_builder
from object_detection.legacy import trainer
from object_detection.utils import config_util
Traceback (most recent call last):
File "C:/Users/varsh/Documents/models/research/object_detection/train.py", line 49, in <module>
from object_detection.builders import dataset_builder
ModuleNotFoundError: No module named 'object_detection'
I've managed to solve this problem in my system (windows 10). The solution is not very straight forward but:
1) First u need to clone Tensorflow Object Detection API repository https://github.com/tensorflow/models.
2) Follow the installation provided in https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md
3) In the step 2, you are required to compile protobuf library, so download the protobuf compiler at https://github.com/google/protobuf/releases/latest (at the time of this writing (3.5.1), there's a bug in protoc which may or may not related to the Windows environment, my solution is use the protoc v 3.4.0)
4) Append the PYTHONPATH environment variable with the directory of /research/ and /research/slim (dont forget to add the PYTHONPATH to Path if you haven't done so.
5) No more ModuleNotFoundError: No module named 'object_detection'

RaspberryPi: ImportError: No module named cv2 in Geany

I installed opencv in RaspberryPi (using this link)and import in termial and python3 (idle). It shows no error in these two as shown in below images.
But, when I tried to run import cv2 in Geany, it shows error given below:
Traceback (most recent call last):
File "import.py", line 1, in <module>
import cv2
ImportError: No module named cv2
How I can solve this problem?
I solved this problem by changing python to python3 in Geany.
For this, Go to Build->Set Build Commands in Geany menu. In Set Build Commandswindow, under Python commands label in compile line python m py_compile will be there. Similarly, under Executive commands label in executive line python "%f" will be there. I changed python m py_compile into python3 m py_compile and python "%f" into python3 "%f" (as shown in images 1 and 2) and above-mentioned problem get solved.
Image 1.
Image2.
Thanks.

Module Import Error for Pypi module

I am trying to import a pypi module (thinkx 1.1.2) into spyder. It is installed on anaconda and showing up on conda list. I my python path folders is my anaconda folder. When I attempt to import thinkx into spyder I get :
import thinkx
Traceback (most recent call last):
File "", line 1, in
import thinkx
ImportError: No module named 'thinkx'
According to module README, thinkx does not expose package named thinkx.
It provides the following modules:
thinkbayes: Code for Think Bayes.
thinkstats2: Code for Think Stats, 2nd edition
thinkbayes2: Code for Think Bayes, 2nd edition, not yet published.
thinkdsp: Code for Think DSP
thinkplot: Plotting code used in all of the books, mostly wrapper functions for matplotlib.pyplot
Try:
import thinkbayes

Python - Cannot import name - 3.4

I am working with numpy and Pillow (replacement for PIL in 3.4), but have been having problems with the importing of Pillow. I found a similar post here:
ImportError: Cannot import name X
However, this post was using his own created libraries and the problem was that his modules imported each other, creating circular dependent imports.
My code however does not use my own module, here is the code:
import PIL
from PIL import ImageGrab
import numpy
img = ImageGrab.grab()
imgLoad = img.load()
size = img.size()
And this then returns the error:
Traceback (most recent call last):
File "E:/Family Documents/Matthew's Documents/Python/PIL.py", line 1, in <module>
import PIL
File "E:/Family Documents/Matthew's Documents/Python\PIL.py", line 2, in <module>
from PIL import ImageGrab
ImportError: cannot import name 'ImageGrab'
One other interesting thing about this is that when I first installed Pillow (PIL), I tried it in the shell and the "from PIL import ImageGrab" worked.
Also, if I restart the shell (close it and re-open it) the commands, being manually typed in, work as well. This suggests to me that something is bugging out with python, since retyping "import PIL" throws the same error message "cannot import name 'ImageGrab'".
Thanks for any help
Ha, this already bit me multiple times.
Your traceback shows the file name:
E:/Family Documents/Matthew's Documents/Python/PIL.py
Your PIL.py is found first, so you're trying to import names from the module that's just executing, not from the actual library you have installed.

Categories