I've seen this question answered to Linux operating systems, but I'm working with Windows (VS 2010), and I can't find a solution.
This is my code:
import numpy
import cv2
campo = cv2.imread("fotograma.png")
balon = cv2.imread("balon.png")
#SIFT detector
sif = cv2.SIFT()
I'm using Python 2.7, as I said, with VS 2010, but even if I try typping my code in the Python interpreter, it shows me the same error message. The funny thing is, when I type cv2.SI in my editor, it suggests me the SIFT function, so it looks to recognize it.
Please help me.
Related
I am trying PyPy for the first time because I need serializable continuations. Specifically, this is what I am attempting:
from _continuation import continulet
import pickle
def f(cont):
cont.switch(111)
cont.switch(222)
cont.switch(333)
c = continulet(f)
print(c.switch())
print(c.switch())
saved = pickle.dumps(c)
When I try to pickle c I get this error, though: NotImplementedError: continulet's pickle support is currently disabled.
So, is there some way to enable pickling of continuations? The message suggests this, but so far I couldn't find out how.
Edit: I am using "PyPy 7.3.1 with GCC 9.3.0" (Python 3.6.9) on Linux.
I am attempting to use the Haarclassifier from opencv cuda, for this I found the object cv.cuda_CascadeClassifier. However, assigning cv.cuda_CascadeClassifier() to a variable spit the following error:
this object has no ''load'' attribute. I could successfully verify it
by printing their dir() print(dir(cv.cuda_CascadeClassifier)).
Is there any other way to call this object or did anyone effectively exploite the cascadeclassifier with opencv cuda?
thx
The lack of documentation for the python API really is a pain. Speaking about the version 4.5 of OpenCV, you have to call the create method when reading a xml cascade file or it'll yield segmentation fault when trying to detect. In my experience you'll also need to convert to gray scale or it will yield (-215:Assertion failed) src.type() == CV_8UC1.
Here's my working code on OpenCV 4.5.1:
import cv2
img = cv2.imread('./img_sample.jpg')
cascade = cv2.cuda_CascadeClassifier.create('./cascade_file.xml')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cuFrame = cv2.cuda_GpuMat(gray_img)
result = cascade.detectMultiScale(cuFrame).download() # download() gets the result as UMat
if result is not None:
print(result[0])
I hope this answers your question about the cuda version usage.
This is most likely due to the use of a version of openCV between 4.0.0 and 4.3.0, In those versions cuda_CascadeClassifier was disabled. In 4.4.0 This functionallity was brought back. (https://github.com/opencv/opencv_contrib/pull/2554/files)
Even though this seems to work fine in C++ it gives me a segmentation fault using the python wrapper using the following code:
classifier_cuda = cv2.cuda_CascadeClassifier('cascades_file.xml')
while True:
success, frame = vidcap.read()
cuFrame = cv2.cuda_GpuMat(frame)
result = classifier_cuda.detectMultiScale(cuFrame)
print (result)
Any solutions?
As already Breno Alef wrote, the problem of OpenCV for Python is the lack of documentation and also of some code examples, which makes difficult to understand how to write correctly Python code to use OpenCV.
Looking at the documentation of the cuda_CascadeClassifier or using the Python built-in function help() you can see that the cuda_CascadeClassifier is a subclass of cv.Algorithm, which contains the load() method, but the problem is the way cuda_CascadeClassifier works is a bit different from the Cascade class declared in opencv2/objdetect.hpp.
I'm trying to use the built in opencv function TriangulatePoints for multiple views: https://docs.opencv.org/3.4.4/d0/dbd/group__triangulation.html#ga211c855276b3084f3bbd8b2d9161dc74.
Using python on linux, does anyone have experience using this function? The website only has the syntax for cpp and in python I don't know how to code it.
My code now:
import cv2
import numpy as np
point_2D = np.array([[17.4485, 709.7993], [17.4382, 709.8409]])
Proj_Matrices = np.array([ [1037.5, -6.9927, -10.0190, -4780.7], [6.9747, 1043.3, -5.8867, -731.9206], [644.7895, 383.4982, -3231.1], [1036.937, -22.8371, -28.3254, -5607.7], [23.0587, 1043.1, 3.1815, -633.4485], [650.4355, 373.6, -15.3504, -3706.5] ])
OutputArray = np.zeros((3,2))
Points_3D = cv2.sfm.triangulatePoints(point_2D, Proj_Matrices, OutputArray)
However, when I run it from the terminal I get the following error:
AttributeError: module 'cv2' has no attribute 'sfm.
I installed sfm on my computer using the instructions on the opencv site.
When I omit sfm I get the following error:
AttributeError: module 'cv2' has no attribute 'sfm'
I think I get that error since the machine thinks I'm trying to use the previous version of TriangulatePoints.
Examples of opencv code using sfm exist on the opencv website but they are in cpp.
I'm wondering what the syntax for using TriangulatePoints(with sfm) is in python? Even in cpp, I don't understand how the output array is an input to the function? Also, if anyone knows how to fix the sfm error I am receiving that would be appreciated.
Thanks!
This question concerns Matlab 2014b, Python 3.4 and Mac OS 10.10.
I have the following Python file tmp.py:
from statsmodels.tsa.arima_process import ArmaProcess
import numpy as np
def generate_AR_time_series():
arparams = np.array([-0.8])
maparams = np.array([])
ar = np.r_[1, -arparams]
ma = np.r_[1, maparams]
arma_process = ArmaProcess(ar, ma)
return arma_process.generate_sample(100)
I want to call generate_AR_time_series from Matlab so I used:
py.tmp.generate_AR_time_series()
which gave a vague error message
Undefined variable "py" or class "py.tmp.generate_AR_time_series".
To look into the problem further, I tried
tmp = py.eval('__import__(''tmp'')', struct);
which gave me a detailed but still obscured error message:
Python Error:
dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so, 2): Symbol
not found: __gfortran_stop_numeric_f08
Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so
Expected in: /Applications/MATLAB_R2014b.app/sys/os/maci64/libgfortran.3.dylib
in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so
I can call the function within Python just fine, so I guess the problem is with Matlab. From the detailed message, it seems that the problem lies in something is expected in the Matlab installation path, but of course Matlab installation path does not contain those things since they are third-party libraries for Python.
How to solve this problem?
Edit 1:
libgfortran.3.dylib can be found in a lot of places:
/Applications/MATLAB_R2014a.app/sys/os/maci64/libgfortran.3.dylib
/Applications/MATLAB_R2014b.app/sys/os/maci64/libgfortran.3.dylib
/opt/local/lib/gcc48/libgfortran.3.dylib
/opt/local/lib/gcc49/libgfortran.3.dylib
/opt/local/lib/libgcc/libgfortran.3.dylib
/Users/wdg/Documents/MATLAB/mcode/nativelibs/macosx/bin/libgfortran.3.dylib
Try:
setenv('DYLD_LIBRARY_PATH', '/usr/local/bin/');
For me, using the setenv approach from within MATLAB did not work. Also, MATLAB modifies the DYLD_LIBRARY_PATH variable during startup to include necessary libraries.
First, you have to make sure which version of gfortran scipy was linked against: in Terminal.app, enter otool -L /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/special/_ufuncs.so and look for 'libgfortran' in the output.
It worked for me to copy $(MATLABROOT)/bin/.matlab7rc.sh to my home directory and change the line LDPATH_PREFIX='' in the mac section (around line 195 in my case) to LDPATH_PREFIX='/opt/local/lib/gcc49', or whatever path to libgfortran you found above.
This ensures that /opt/local/lib/gcc49/libgfortran.3.dylib is found before the MATLAB version, but leaves other paths intact.
When running OpenCV on Windows 7, using the standard python shell, I get the following behavior.
import cv
im = cv.LoadImageM("data/somefile.jpg")
thumb = cv.CreateMat(im.rows/6, im.cols/6, im.type)
print "Before"
cv.Resize(im, thumb)
print "After"
Gives:
>>> Before
========================= RESTART ==========================
No error is thrown, what should I look for? What causes such crashing in OpenCV/Python?
Most memory allocation in OpenCV is unchecked and can result in crashes. OpenCV also attempts to throw exceptions through C code, which may cause anything to happen (usually a crash) depending on how it was compiled.
Check whether the values of im.rows/6, etc. are what you expect and that the image sizes should be within python memory limits.
I had to rebuild OpenCV using Visual Studio (Express) 2010, in stead of MinGW, that did the trick, so I guess it was just a faulty build in the end.
i don't think your program is crashing,it is doing just what you are telling it to do.
See the codes below,am using openCv 2.2 with python 2.7.2.Try using WaitKey() to prevent crashes!
import cv
cv.NamedWindow("win",cv.CV_WINDOW_AUTOSIZE)
im= cv.LoadImageM("image.jpg")
thumb= cv.CreateMat(im.rows/3, im.cols/3, im.type)
cv.Resize(im, thumb)
cv.ShowImage("win",thumb)
cv.WaitKey(10000)