需要进行哪些更改才能使此代码检测多个人脸,而不仅仅是一个人脸



我是 openCV 和 dlib 模块的新手。我试图使此代码检测多个人脸,但只检测到第一张人脸。

  import numpy as np
  import argparse
  import imutils
  import dlib
  import cv2
  from matplotlib import pyplot as plt
  def rect_to_bb(rect):
      x = rect.left()
      y = rect.top()
      w = rect.right() - x
      h = rect.bottom() - y
     return (x, y, w, h)
 image = cv2.imread("image.jpg")
 image = imutils.resize(image, width=500)
 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 detector = dlib.get_frontal_face_detector()
 rects = detector(image, 1)
 fname = "image.jpg".split('/')[-1]
 name, ext = fname.split('.')
 new_ext="png"
 for (i, rect) in enumerate(rects):
       (x, y, w, h) = rect_to_bb(rect)
       fname = '{}_{}.{}'.format(name, i, new_ext)
       clone = image.copy()
       cv2.rectangle(clone, (x, y), (x + w , y + h), (0, 255, 0), 1)
       startX = x
       startY = y - 15 if y - 15 > 15 else y + 15
       cv2.putText(clone, str(i), (startX, startY),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
       roi = image[y:y + h, x:x + w]
       cv2.imshow("Seprate Faces", roi)
       cv2.imwrite(fname, roi)
       cv2.imshow("Detected Faces", clone)
       cv2.waitKey(0)

感谢您的回复!!

我想你想把你的灰度提交给检测器:

rects = detector(gray, 1)

相关内容

最新更新