【发布时间】:2019-11-17 12:23:28
【问题描述】:
我正在尝试编写一个 Python 3.7 脚本,以使用将图像存储为 n 维 Numpy 数组的 OpenCV Haar 分类器文件来检测人脸和特征。现在我只使用两个功能: - 整个脸 - 眼睛 这两者都是使用两个不同的分类器获得的。 代码检测图像中这两个特征的存在,然后使用 cv2.rectangle() 函数在每个特征的 for 循环内用一个矩形标记它们,即一个用于检测到的面部,一个用于检测到的眼睛。
我希望脚本仅在面部阵列和眼睛阵列中找到这些点时才标记眼睛矩形。 numpy.intersect1d() 只在一维数组中查找交集。
我试过了
for x,y,w,h in eyes and x,y,w,h in faces:
它所做的只是返回这个错误信息:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
任何帮助将不胜感激。
这是在 Windows 10 64 位上尝试的,代码是用 Pycharm 2019 编写的,OpenCV 导入为 CV2。
# Creating the cascade classifier objects.
face_cascade =
cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
# Entering and displaying the original image.
img = cv2.imread("filename.jpg", 1)
cv2.imshow("Original Images",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Convert the image to Black and White and store in a variable.
gry_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gry_img, scaleFactor = 1.05, minNeighbors = 8)
eyes = eye_cascade.detectMultiScale(gry_img, scaleFactor = 1.30, minNeighbors = 12)
# Now, we mark the detected features with a rectangle on the original image.
for x,y,w,h in faces:
img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 21, 21), 3) # Blue.
for x,y,w,h in eyes:
img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 255, 24), 3) # Cyan.
cv2.imshow("Detected Faces and Eyes",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
只有在面部特征数组中也找到眼睛特征时,我才需要标记它们。
【问题讨论】:
标签: python python-3.x numpy opencv haar-classifier