gpt4 book ai didi

python - 为每张脸分配 id

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:54 25 4
gpt4 key购买 nike

我使用以下代码来检测人脸。检测后,我在脸部周围绘制矩形。然后我分配人脸的 id,即人脸编号。如果有两张脸,一张脸的脸 ID 为 1,另一张脸的 ID 为 2。以下是我的代码。

detections = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw a rectangle around the faces
for (x, y, w, h) in detections:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

face_no = detections.shape[0];

cv2.putText(frame, str(face_no), (x, y - 30), cv2.FONT_HERSHEY_TRIPLEX,
.7, (0, 0, 0), 1, cv2.LINE_AA)

但是这段代码总是返回面的总数。我怎样才能得到每张脸的编号?

最佳答案

如果您使用 detections.shape[0],您每次都会获得相同的值,因为其中没有根据循环变化的变量。您可以做的是使用 enumerate 标记面部,这会将 face_no 设置为循环迭代次数:

for face_no, (x, y, w, h) in enumerate(detections):
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.putText(frame, str(face_no), (x, y - 30), cv2.FONT_HERSHEY_TRIPLEX,
.7, (0, 0, 0), 1, cv2.LINE_AA)

关于python - 为每张脸分配 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45273076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com