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

问题描述 投票:0回答:1

我是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)

感谢您的回复! !谢谢

python numpy opencv matplotlib dlib
1个回答
0
投票

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

rects = detector(gray, 1)
© www.soinside.com 2019 - 2024. All rights reserved.