[英]python, opencv error cv.rectangle SystemError: old style getargs format uses new features
I am writing a code to detect faces in python using openCV. 我正在编写代码以使用openCV在python中检测人脸。
The python version I use is Python 2.7.15 and openCV 2.2 我使用的python版本是python 2.7.15和openCV 2.2
def find_faces(image_path):
image = cv.imread(image_path)
color_img = image.copy()
filename = os.path.basename(image_path)
gray_img = cv.cvtColor(color_img, cv.CV_BGR2GRAY)
haar_classifier = cv.CascadeClassifier('D:\haarcascades\\haarcascade_frontalface_default.xml');
eye_cascade = cv.CascadeClassifier('D:\haarcascades\\haarcascade_eye.xml');
faces = haar_classifier.detectMultiScale(gray_img,1.3,6);
print('Number of faces found: {faces}'.format(faces=len(faces)))
for (x, y, width, height) in faces:
print(x);print(y);print(x+width);print(y+height);
cv.rectangle(color_img, (x, y), (x+width, y+height), (0, 255, 0), 3,8,0)
roi_gray = gray_img[y:y+height, x:x+width]
roi_color = color_img[y:y+height, x:x+width]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv.imshow(filename, color_img)
cv.waitKey(0)
cv.DestroyAllWindows()
The code works fine and print faces count. 该代码工作正常并且打印面计数。 But when I try to draw rectangle on images, the code gives me error 但是当我尝试在图像上绘制矩形时,代码给了我错误
cv.rectangle(color_img, (x, y), (x+width, y+height), (0, 255, 0), 3,8,0) SystemError: old style getargs format uses new features cv.rectangle(color_img,(x,y),(x + width,y + height),(0,255,0),3,8,0)SystemError:旧样式的getargs格式使用了新功能
How can I fix this. 我怎样才能解决这个问题。 I tried various solutions but they didnt gave me expected results. 我尝试了各种解决方案,但它们并没有给我预期的结果。
This error typically means a datatype is wrong (not necessarily a tuple). 此错误通常表示数据类型错误(不一定是元组)。 I suspect the error is that the x, y
and width, height
values are floats -- try the following: 我怀疑错误是x, y
和width, height
值是浮点型的-请尝试以下操作:
cv.rectangle(color_img, (int(x), int(y)), (int(x+width), int(y+height)), (0, 255, 0), 3,8,0)
Try this: 尝试这个:
cv.rectangle(color_img, x, y, x+width, y+height, (0, 255, 0), 3,8,0)
Even if this doesn't work, you have to First upgrade the opencv version (as already recommended in some comments) to cv2. 即使这行不通,您也必须首先将opencv版本(如某些注释中所建议)升级到cv2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.