我正在使用下面的代码来检测人脸。检测后,我在脸部周围画了一个矩形。然后我指定了面的id,也就是面的编号。如果有两个面,其中一个面的id为1,另一个为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)但是这段代码总是返回面孔的总数。如何获取每个面孔的编号?
发布于 2017-07-24 13:46:08
如果你使用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)发布于 2017-07-24 13:56:05
detections是检测到的面列表,len(detections)应该会给出面数。所以,detections[i]是第i张脸,I就是那张脸的ID。
如果您的循环被更改为
for i in range(o,len(detections):
faceID = i
currentFace = detections[i]以此类推,您可以通过ID访问它们。
https://stackoverflow.com/questions/45273076
复制相似问题