[我尝试了使用haarcascades
中的opencv
的面部检测程序。我能够获得所需的输出(在提供的图像中查找面部的数量),但是所绘制的最终图像存在一点问题脸部周围的矩形。输出的图像不是原始图像的缩放版本,而不是原始图像的全部。
样品
因此,这是程序运行后的输出结果。
代码:
import cv2
import sys
# Get user supplied values
imagePath = sys.argv[1]
cascPath = "haarcascade_frontalface_default.xml"
# 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=(30,30)
)
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.imshow("Faces found", image)
cv2.waitKey(0)
在提示中:
C:\Myproject>python main.py NASA.jpg
Found 20 faces!
程序或多或少给出了正确的答案。可以修改比例因子以获得准确的结果。所以我的问题是如何才能在输出中获得完整的图像?也请添加任何其他建议,我将非常感激。感谢您的阅读!
编辑:
在提出建议后,我使用了imwrite
并保存了看起来很好的输出图像,但是运行程序后显示的图像仍然保持不变。
cv2.namedWindow('Faces found', cv2.WINDOW_NORMAL)
之前添加:cv2.imshow("Faces found", image)
该行将创建可调整大小的窗口。