python - 人脸检测后减小图像尺寸
问题描述
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
并保存了看起来非常好的输出图像,但运行程序后显示的图像仍然保持不变。
解决方案
您的图像太大而无法在屏幕上显示。添加:cv2.namedWindow('Faces found', cv2.WINDOW_NORMAL)
之前cv2.imshow("Faces found", image)
该行将创建可调整大小的窗口。
推荐阅读
- typescript - Typescript 中展开运算符的属性选择
- azure-devops - 根据计划将不同的参数值传递给 Azure DevOps Pipeline
- mysql - 按订单类型过滤产品
- postgresql - 每个语句插入前的 PostgreSQL 触发器
- date - 昨天的日期在颤抖
- javascript - 在 django 模板中的 js 的 Swiper 功能中创建了重复的元素(产品)
- python - 试图从 pyodbc.drivers() 获取列表,但它什么也没产生,为什么?
- javascript - Discord.js 问题:新用户的用户名显示为未定义
- pandas - 基于 Pandas Dataframes 检索子字符串
- python - 使用 Python 正则表达式匹配日期和时间