AttributeError: 'builtin_function_or_method' object has no attribute 'detectMultiScale' - python

This is the code
import cv2, sys
from numpy import array
img = cv2.imread(sys.argv[1])
img2 = array( 200 * (img[:,:,2] > img[:,:, 1]), dtype='uint8')
objects = cv2.CascadeClassifier.detectMultiScale(img2)
print (objects)
I have the error in the title at line 5. (I have tried also with cv2.CascadeClassifier.detectMultiScale(img) and I have the same error).
Many thanks!

You have to call the function detectMultiScale() on an instance of CascadeClassifier()
cascade = cv2.CascadeClassifier()
objects = cascade.detectMultiScale(img2)

Related

AttributeError: type object 'Config' has no attribute 'resize'

def preprocessing(img,label):
img = cv2.resize(img,(Config.resize,Config.resize))
img = img/255
label = np_utils.to_categorical(label, Config.num_classes)
When I want to resize the image, it keep show the error on type object 'Config' has no attribute 'resize'. Do anyone one know what happen?

PythonCV2 NoneType Object

#!/usr/bin/env python
'''
Usage:
./ssearch.py input_image (f|q)
f=fast, q=quality
Use "l" to display less rects, 'm' to display more rects, "q" to quit.
'''
import sys
import cv2
if __name__ == '__main__':
# If image path and f/q is not passed as command
# line arguments, quit and display help message
if len(sys.argv) < 3:
print(__doc__)
sys.exit(1)
# speed-up using multithreads
cv2.setUseOptimized(True);
cv2.setNumThreads(4);
# read image
im = cv2.imread(sys.argv[1])
# resize image
newHeight = 200
newWidth = int(im.shape[1]*200/im.shape[0])
im = cv2.resize(im, (newWidth, newHeight))
Here i am getting error
AttributeError Traceback (most recent call last)
<ipython-input-7-b88f466ecb3b> in <module>
25 # resize image
26 newHeight = 200
---> 27 newWidth = int(im.shape[1]*200/im.shape[0])
28 im = cv2.resize(im, (newWidth, newHeight))
29
AttributeError: 'NoneType' object has no attribute 'shape'
and explain cv2.imread(sys,argv[1]) line
(2)second question is how do i get nonetype error because i havent
passed my image path from my command line yet
(3) cmd line didnt prompt because program didnt executed with th error
i am sorry i couldnt place entire code
The correct way is:
# read image
im = cv2.imread(sys.argv[1])
if im == None :
sys.exit('Invalid or missing image: ' + sys.argv[1])
BEFORE you try to do anything with your image im.

Python Error in Displaying Image

On this Image
I am trying to apply:
from PIL import Image
img0 = PIL.Image.open('Entertainment.jpg')
img0 = np.float32(img0)
showarray(img0/255.0)
And I get this error:
TypeErrorTraceback (most recent call last)
<ipython-input-20-c181acf634a8> in <module>()
2 img0 = PIL.Image.open('Entertainment.jpg')
3 img0 = np.float32(img0)
----> 4 showarray(img0/255.0)
<ipython-input-8-b253b18ff9a7> in showarray(a, fmt)
11 f = BytesIO()
12 PIL.Image.fromarray(a).save(f, fmt)
---> 13 display(Image(data=f.getvalue()))
14
15 def visstd(a, s=0.1):
TypeError: 'module' object is not callable
I can't understand why.
What is not callable here?
How can I just display the image?
Image is a module that you imported
from PIL import Image
You can't call it
Image(data=f.getvalue())
There's a show method that may be useful
img0 = PIL.Image.open('Entertainment.jpg')
img0.show()
The problem may stem from how you've imported the display() function. If your import line is:
import IPython.display as display
then you need to invoke display.display() to show the image. For example:
import IPython.display as display
my_image = Image.open('something.jpg')
display.display(my_image)
If you instead invoke the function as
import IPython.display as display
my_image = Image.open('something.jpg')
display(my_image) # INCORRECT
# TypeError: 'module' object is not callable
then you will indeed get the error message in your original post because display is referring to a module, not a function.
Alternatively, if you import the display() function in the following manner, your code will work:
from IPython.display import display
my_image = Image.open('something.jpg')
display(my_image)

Python: AttributeError

When I run my script I get this error:
File "test_cm.py", line 34, in
labels = labels_img.get_data() AttributeError: 'tuple' object has no attribute 'get_data'
from dipy.tracking.eudx import EuDX
from dipy.reconst import peaks, shm
from dipy.tracking import utils
from dipy.data import read_stanford_labels
from dipy.io.gradients import read_bvals_bvecs
import numpy as np
import matplotlib.pyplot as plt
import nibabel as nib
source="prova11/"
path=source
print('loading data')
bvals,bvecs=read_bvals_bvecs(source+"bvals",source+"bvecs")
bvals[bvals < 10] = 0
img = nib.load(source+"segment-TO-b0.nii")
data = img.get_data()
affine=img.affine
labels_img = read_stanford_labels()
labels = labels_img.get_data()
read_stanford_labels() returns a tuple, and tuples don't have a get_data() method which is why the error says AttributeError: 'tuple' object has no attribute 'get_data'.
You should go over your read_stanford_labels function and see why it returns a tuple, which isn't what you expected.
Edit: By going over the documentation/example your code should be
hardi_img, gtab, labels_img = read_stanford_labels() instead of
labels_img = read_stanford_labels().
Alternatively, if you don't need the first 2 values, you can use
labels_img = read_stanford_labels()[-1] or
*_, labels_img = read_stanford_labels().

AttributeError: 'module' object has no attribute 'QueryFrame'

I am trying to test to decode a qr code real time using python and openCV 3.0.But now I am getting an error message on my terminal. I tried to search on the internet but I still unable to solve it. Can I know whats the error.
This is my Python code:
import cv2 as cv
import zbar
def scanner_procces(frame,set_zbar):
set_width = 100.0 / 100
set_height = 90.0 / 100
coord_x = int(frame.width * (1 - set_width)/2)
coord_y = int(frame.height * (1 - set_height)/2)
width = int(frame.width * set_width)
height = int(frame.height * set_height)
get_sub = cv.GetSubRect(frame, (coord_x+1, coord_y+1, width-1, height-1))
cv.Rectangle(frame, (coord_x, coord_y), (coord_x + width, coord_y + height), (255,0,0))
cm_im = cv.CreateImage((get_sub.width, get_sub.height), cv.IPL_DEPTH_8U, 1)
cv.ConvertImage(get_sub, cm_im)
image = zbar.Image(cm_im.width, cm_im.height, 'Y800', cm_im.tostring())
set_zbar.scan(image)
for symbol in image:
print '\033[1;32mResult : %s symbol "%s" \033[1;m' % (symbol.type,symbol.data)
cv.ShowImage("webcam", frame)
cv.WaitKey(10)
if __name__ == "__main__":
cv.namedWindow("webcam", cv.WINDOW_AUTOSIZE)
capture = cv.VideoCapture(0)
set_zbar = zbar.ImageScanner()
while True:
frame = cv.QueryFrame(capture)
scanner_procces(frame,set_zbar)
This is the error code:
AttributeError: 'module' object has no attribute 'QueryFrame'
This is the traceback message:
init done
opengl support available
select timeout
Traceback (most recent call last):
File "realtimetestwebcam.py", line 38, in <module>
scanner_procces(frame,set_zbar)
File "realtimetestwebcam.py", line 9, in scanner_procces
coord_x = int(frame.width * (1 - set_width)/2)
AttributeError: 'numpy.ndarray' object has no attribute 'width'
Is it the error because of the opencv version? Thank you.
When you use cv2 you should use
cv2.VideoCapture.read
instead of QueryFrame. For more info see here.
You can try this code
capture = cv.VideoCapture(0)
set_zbar = zbar.ImageScanner()
while True:
_,frame = capture.read()
Updated
This error message
AttributeError: 'numpy.ndarray' object has no attribute 'width'
you get because that changed the format of the return value. With cv frame was an object with some type, and with cv2 frame is np.ndarray. Instead of width attr you can get its dimensions with shape method. Migration from cv to cv2 is not simple process and requires rewriting of a part of the code.
I solved it by installing the python-opencv package
sudo apt-get install python-opencv
That solved my whole problem. Thanks to kvorobiev for helping me all the way.

Categories

Resources