我正在开发一个程序,用python从大图像中剪切人脸。然而,我甚至看到他们都有问题。
我正在处理的图像可能超过2000x2000,它们不适合我的屏幕。这是代码:
import cv2
import sys
# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(100, 100),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
print "Found {0} faces!".format(len(faces))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.NamedWindow(name, flags=WINDOW_NORMAL)
cv2.imshow("Faces found", image)
cv2.waitKey(0)
这是我关心的部分:
cv2.NamedWindow(name, flags=WINDOW_NORMAL)
cv2.imshow("Faces found", image)
cv2.waitKey(0)
现在,opencv文档中有关于如何更改窗口大小的说明,但到目前为止,我一直收到错误:
错误1:
Found 2 faces!
Traceback (most recent call last):
File "face_detect.py", line 31, in <module>
cv2.NamedWindow(name, flags=WINDOW_NORMAL)
AttributeError: 'module' object has no attribute 'NamedWindow'
错误2:
Found 2 faces!
Traceback (most recent call last):
File "face_detect.py", line 31, in <module>
cv2.namedWindow("", WINDOW_NORMAL)
NameError: name 'WINDOW_NORMAL' is not defined
错误3:
File "face_detect.py", line 31
cv2.namedWindow(winname[, WINDOW_NORMAL])
^
SyntaxError: invalid syntax
有人能告诉我我做错了什么吗?
您错误地键入了cv2.NamedWindow
而不是cv2.namedWindow
,请注意这种情况。此外,WINDOW_NORMAL
需要是cv2.WINDOW_NORMAL
。然后可以使用cv2.resizeWindow
设置所需的大小。
# Specify an appropriate WIDTH and HEIGHT for your machine
WIDTH = 1000
HEIGHT = 1000
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
cv2.resizeWindow('image', WIDTH, HEIGHT)
附带说明一下,当文档使用以下格式时
cv2.namedWindow(winname[, flags])
[]
意味着flags
是可选的位置输入。它不是有效的Python语法,因此无法复制/粘贴到代码中。