PyCharm: Using SIFT with opencv-contrib (Mac) - macos

I have a Python/OpenCV project and I am trying to use the xfeatures2d module from opencv-contrib. I am using a Mac and my IDE is PyCharm. I have installed the packages opencv-contrib-python and opencv-python through Preferences > Project Intepreter.
However, when I try to run the code below, I get a the following error:
import cv2
import numpy as np
img = cv2.imread("NotreDame.jpg", 0)
sift = cv2.xfeatures2d.SIFT_create()
line 6, in <module>
sift = cv2.xfeatures2d.SIFT_create()
cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv_contrib/modules/xfeatures2d/src/sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'
I have installed opencv and opencv-contrib on my computer using:
$ pip install opencv-python==3.4.2.17
$ pip install opencv-contrib-python==3.4.2.17
I am not sure how I can resolve this error. Any insights are appreciated.

Related

opencv issues with M1 MAC - OpenCV imshow doesnot work

I purchased a M1 Mac. Is anyone having issues with imshow with opencv. I did pip install opencv-python and brew install opencv and brew install opencv as well.
import cv2
import urllib
import numpy as np
import requests
url = 'https://www.visitcalifornia.com/sites/visitcalifornia.com/files/styles/welcome_image/public/vc_crtr_borntobewild_module_mendocino_st_rf_623667652_1280x640.jpg'
from skimage import io
img = io.imread(url)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow('URL Image', img)
cv2.waitKey()
and also
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")
vc.release()
does not work for me
It was solved by
pip install opencv-python opencv-python-headless
I was able to solve this by building from the source code of OpenCV 4.5, use this link to get the source code
Try to install Miniconda from https://docs.conda.io/en/latest/miniconda.html
then activate it and try to install cv2 pip install opencv-python
getting 80-90 FPS for just reading image & showing
from my side, all are working in M1 using Miniconda
Opening the terminal in rosetta and doing a pip install(inside virtual environment) helped me build the opencv. Before this , while I was doing it with the M1 terminal, it was giving errors stating some architecture issue. Guess this did the trick to me.
Create Virtual Environment -> Activate it -> (Rosetta Terminal) -> pip3 install opencv-python
Install OpenCV using pip. This normally gives a ffmpeg error, so install that first
First do:
pip3 install ffmpeg
and then
pip3 install opencv-python
I thought I had the same issue. The window was not popping up when using cv2.imshow() or cv2.namedWindow(). I realized hours later that the window was there in the Mac menu bar, I just had to click on it.

ImportError: No module named googlemaps

I've installed this package via pip27 on macports. My OS is OSX El Capitan 10.11.6. My python install is 2.7.10.
I'm trying to run an example script that imports googlemaps module, but I keep getting that ImportError. I have a feeling that it's how pip installed it and the reason why python can't find it, but I'm relatively new to pip so I don't know where to start investigating.
I also tried googling for a fix but no dice. Any idea what's happening here?
Here's my code:
import googlemaps # can't import
import argparse
from datetime import datetime
# collect args for lat, long, # of addresses, radius of search
parser = argparse.ArgumentParser(description='Randomize addresses on Google Maps')
parser.add_argument('-lt', '--latitude')
parser.add_argument('-lng', '--longitude')
parser.add_argument('-n', '--count')
parser.add_argument('-r', '--radius')
args = parser.parse_args()
print('Results: ', vars(args))
Error message:
Traceback (most recent call last):
File "randomize_addresses.py", line 1, in <module>
import googlemaps
ImportError: No module named googlemaps
Found a fix. I uninstalled googlemaps via pip and then reinstalled using easy_install. Apparently OSX doesn't like pip.
Looks like you used pre-installed python since you mentioned version 2.7.10 (default version shipped with macOS) which is located at /usr/bin/.
MacPorts installs binaries and libraries under /opt/local. Try to install python via MacPorts and run the program again. Python and pip should both be linked so that packages installed via pip is available to python.
In this case packages installed using pip27 would be available to python27 installed via MacPorts and not /usr/bin/python.
Another way would be to download get-pip.py and install it against /usr/bin/python (pip installation guide).
Note: Make sure you are using python installed via MacPorts. To check this run which python, it should show something like /opt/local/bin/python2.7

OpenCV - python 3.x and windows - what version of Numpy?

I downloaded openCV from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv and installed it. When I tried to run it:
import cv2
I got error message:
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition
5.0.3\helpers\pydev\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: numpy.core.multiarray failed to import
I guess I need to set up virtual enviroment and install different version of Numpy, but what exact version of Numpy I need to install?
You can try using the anaconda distribution for that.
I'm using it and it works great with opencv on windows.
You can download it from here:
https://www.continuum.io/downloads
Then use conda to install opencv
conda install -c conda-forge opencv
conda comes with pre installed numpy, scipy, pandas etc and makes deployment on windows painless (since it ships with the build-chain).
You need numpy version 11 or above . You can find it in the below link.
https://pypi.python.org/pypi/numpy

Cannot import open cv2 in python. Error: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel [[Cannot upgrade numpy ]]

I and trying to build something on python package cv2. My os is Mac OS X El Capitan and python version is python 2.7.10.
But when I import cv2 as
>>> import cv2
I get following error.
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
I figured out that it is a problem of numpy version and I need to upgrade it. So I tried
sudo pip install numpy --upgrade
But system threw following error:
OSError: [Errno 1] Operation not permitted: '/tmp/pip-JSOF8d-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy-1.8.0rc1-py2.7.egg-info'
Then I tried to install a particular version of numpy (required version) using following command.
pip install numpy==1.8
Even this didn't work and I got following traceback error:
SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.
Now I am clueless, how to proceed. Please help me to figure this out.
Thanks in advance.
I was also facing the same issue, 2 things which solved my problem are
Run
pip install --ignore-installed numpy
run the above command in terminal, that is don't use setuptools.
But, it will upgrade to latest version of numpy, hope this resolves your problem. Its been quite tough for me to fix couple of el-capitan issues

kmpfit module not in Kapteyn v2.2 module list

I am using Python 2.7 through Anaconda 2.7.8 and need Kapteyn 2.2 to perform Non-linear Least Squares fitting easily (it is probably an alternative to Scipy.optimize.leastsq() for dummies like me!).
After copy-pasting this from a previous post here on Stack Overflow:
conda install -c https://conda.binstar.org/dhirschfeld pyodbc
and then running on my cmd (as I did not have pyodbc installed I think, because of which maybe the command prompt on my Windows 7 64-bit system was not responding well to python setup.py install inside the Anaconda directory where I unzipped the Kapteyn .zip file downloaded from University of Groningen website.
But, after the installing pyodbc properly and running python setup.py install, the cmd gave me an error saying error: command 'C:\Users\windows 7\Anaconda\Scripts\gcc.bat' failed with exit status 1. Later, when I tried to import kmpfit module (needed for Non-linear least square fitting with Kapteyn), here is the problem:
import kapteyn
help(kapteyn)
Help on package kapteyn:
NAME
kapteyn - Kapteyn package.
FILE
c:\users\windows 7\anaconda\kapteyn\__init__.py
PACKAGE CONTENTS
_ni_support
celestial
doccer
filters
interpolation
maputils
mplutil
positions
rulers
shapes
tabarray
wcsgrat
DATA
__all__ = ['celestial', 'wcs', 'wcsgrat', 'tabarray', 'maputils', 'mpl...
__version__ = '2.2'
VERSION
2.2
As you can see, there is no module named kmpfit (or even wcs) here. But according to http://www.astro.rug.nl/software/kapteyn/intro.html, these two should be there.
Kindly help. I have never imported any module before.
Thanks in advance...:-)
I just managed to get this working (on Mac OSX, so you may have to adjust this). My steps were:
$ conda install pyodbc (didn't need to go through binstar)
Download & unarchive the kapteyn package, then navigate to its directory
$ python setup.py install, which used my OS's C compiler and Anaconda's python, and installed kapteyn to my anaconda distro's site-packages, as it should.
Check that kmpfit.so is in the kapteyn folder in site-packages, showing that kmpfit installed correctly.
>> from kapteyn import kmpfit failed, ImportError: cannot import name kmpfit. I did some digging and discovered that it was still importing kapteyn from the folder that I downloaded, not from site-packages.
Delete the downloaded kapteyn folder, then try again. It worked!

Resources