首页 > 解决方案 > 人脸检测后减小图像尺寸

问题描述

haarcascades我尝试了这个在.I中使用的人脸检测程序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并保存了看起来非常好的输出图像,但运行程序后显示的图像仍然保持不变。

保存的图像- 在此处输入图像描述

标签: pythonopencvface-detection

解决方案


您的图像太大而无法在屏幕上显示。添加:cv2.namedWindow('Faces found', cv2.WINDOW_NORMAL)之前cv2.imshow("Faces found", image)该行将创建可调整大小的窗口。


推荐阅读