I'm new to OpenCV and am having a problem with cv2.imshow. Here's my code:
import numpy as np
import cv2
print("Version is", cv2.__version__)
print("Build info", cv2.getBuildInformation())
img = cv2.imread('../coelacanth.jpeg', cv2.IMREAD_COLOR)
if img is None:
print("no image")
else:
print("there is an image")
cv2.line(img, (0,0), (150, 150), (255, 255, 255), 15)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I get this error:
line 16, in <module>
cv2.imshow('image', img)
line 16, in <module>
cv2.imshow('image', img)
cv2.error: /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:583: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage
The call to cv2.line seems to work. If try cv2.waitKey(0) without the imshow, I get the same error.
I installed OpenCV3 with
brew install --with-examples --with-contrib --with-python3 --with-qt --without-python -v opencv3
So, clearly I need to
Rebuild the library with Windows, GTK+ 2.x or Carbon support
but I don't know how to do that and haven't been able to work that out from searching other questions. Does anyone have any suggestions?
It was a pathing problem. In .bash_profile, we commented out
#PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
#export PATH
Then uninstalled with brew uninstall opencv3 and reinstalled with the brew command given in the original question.
Related
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.
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.
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
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
I am trying to capture an image from my web camera on windows using Python 3.
I checked already openCV, but the support of python-3 is missing.
Is there any other way to do so?
In the meantime, OpenCV 3.1 was released and works with Python 3 (since OpenCV 3.0). Pre-compiled Windows binaries can be found here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
you can try OpenCV, SimpleCV.
using SimpleCV:
from SimpleCV import Image, Camera
cam = Camera()
img = cam.getImage()
img.save("filename.jpg")
using OpenCV:
from cv2 import *
# initialize the camera
cam = VideoCapture(0) # 0 -> index of camera
s, img = cam.read()
if s: # frame captured without any errors
namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
imshow("cam-test",img)
waitKey(0)
destroyWindow("cam-test")
imwrite("filename.jpg",img) #save image
using pygame:
import pygame
import pygame.camera
pygame.camera.init()
pygame.camera.list_camera() #Camera detected or not
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()
img = cam.get_image()
pygame.image.save(img,"filename.jpg")
Install OpenCV:
install python-opencv bindings, numpy
Install SimpleCV:
install python-opencv, pygame, numpy, scipy, simplecv
get latest version of SimpleCV
Install pygame:
install pygame