我尝试了这个在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)
该行将创建可调整大小的窗口。
关于python - 人脸检测后缩小图像尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57996174/