【发布时间】:2016-04-16 01:20:59
【问题描述】:
我正在开发一个程序来从 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 文档中有 instructions 关于如何更改窗口大小,但到目前为止我一直收到错误:
错误一:
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
谁能告诉我我做错了什么?
【问题讨论】:
-
可能值得尝试使用
sudo apt-get install python-opencv重新安装软件包... :) -
@filaton 对一些错别字进行了彻底的修复。
-
@Suever 对! :) 但我记得有些情况下,OpenCV 在未正确安装时会抛出相同的异常,所以这就是为什么我说它可能值得。
标签: python python-2.7 opencv