python - 人脸检测后缩小图像尺寸

标签 python opencv face-detection

我尝试了这个在haarcascades中使用opencv的人脸检测程序。我能够获得所需的输出(在提供的图像中查找人脸的数量),但是结果图像在人脸周围绘制矩形时存在一些问题。原始图像的输出图像是原始图像的缩放版本,没有完整显示。

样本

输入:enter image description here

输出:
enter image description here

因此,这是程序运行后的输出结果。

编码:

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并保存了输出图像,这看起来非常不错,但是运行该程序后显示的图像仍然保持不变。

保存的图像-
enter image description here

最佳答案

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

关于python - 人脸检测后缩小图像尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57996174/

相关文章:

python - 如何在 matplotlib (python) 中标记一条线?

python - 有没有办法将 SVG 路径编码为点形状?

python - 如何将成对数组列表转换为 2x1 数组?

c++ - 如何使用 OpenCV 访问外部网络摄像头?

java - 如何从 openCV android 中的 Byte[] 获取 Mat 对象?

python - Pyparsing setParseAction 函数没有参数

python - 如何在 OpenCV 中的点之间画线?

face-detection - 再现 dlib frontal_face_detector() 训练

android - BiometricPrompt 虹膜和面部提示不适用于加密对象身份验证。 #AndroidX

swift - GPUImage2使用SolidColorGenerator作为矩形生成器