我很确定我的一般主题是正确的,但我找不到任何面孔。我的代码从c=cv2.VideoCapture(0)
读取,即计算机的摄像机。然后我进行以下设置以获得面部的位置。正如你所看到的,我循环遍历不同的scaleFactors和minNeighbors,但rects总是返回空。我还尝试了opencv / data / haarcascades包中包含的四个不同haarcascade xml文件中的每一个。
任何提示?
while(1):
ret, frame = c.read()
rects = find_face_from_img(frame)
def detect(img, cascade):
for scale in [float(i)/10 for i in range(11, 15)]:
for neighbors in range(2,5):
rects = cascade.detectMultiScale(img, scaleFactor=scale, minNeighbors=neighbors,
minSize=(20, 20), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
print 'scale: %s, neighbors: %s, len rects: %d' % (scale, neighbors, len(rects))
def find_face_from_img(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
rects = detect(gray, cascade)
答案 0 :(得分:6)
为了让它在我的电脑上运行,我稍微改变了你的代码。 当我跑步时,我得到了结果
import cv2
import cv2.cv as cv
import getopt, sys
def detect(img, cascade):
for scale in [float(i)/10 for i in range(11, 15)]:
for neighbors in range(2,5):
rects = cascade.detectMultiScale(img, scaleFactor=scale, minNeighbors=neighbors,
minSize=(20, 20), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
print 'scale: %s, neighbors: %s, len rects: %d' % (scale, neighbors, len(rects))
def find_face_from_img(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
rects = detect(gray, cascade)
if __name__ == '__main__':
args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
try: video_src = video_src[0]
except: video_src = 0
args = dict(args)
cascade_fn = args.get('--cascade', "cascades/haarcascade_frontalface_alt.xml")
cascade = cv2.CascadeClassifier(cascade_fn)
c=cv2.VideoCapture(0)
while(1):
ret, frame = c.read()
rects = find_face_from_img(frame)
if 0xFF & cv2.waitKey(5) == 27:
break
输出:
scale: 1.2, neighbors: 2, len rects: 1
scale: 1.2, neighbors: 3, len rects: 1
scale: 1.2, neighbors: 4, len rects: 1
scale: 1.3, neighbors: 2, len rects: 1
scale: 1.3, neighbors: 3, len rects: 1
scale: 1.3, neighbors: 4, len rects: 0
scale: 1.4, neighbors: 2, len rects: 1
scale: 1.4, neighbors: 3, len rects: 0
scale: 1.4, neighbors: 4, len rects: 0
scale: 1.1, neighbors: 2, len rects: 1
scale: 1.1, neighbors: 3, len rects: 1
scale: 1.1, neighbors: 4, len rects: 1
scale: 1.2, neighbors: 2, len rects: 1
scale: 1.2, neighbors: 3, len rects: 1
scale: 1.2, neighbors: 4, len rects: 1
scale: 1.3, neighbors: 2, len rects: 1
一些建议:不要选择你的minSize太低......否则会检测到类似于脸部的每个小物品。
我假设您正在浏览所有这些参数以找到最佳参数。我发现minNeighors不应该太高,否则就找不到了。
确保您的cascade xml文件已正确链接。如果找不到,它就不会出错,只会找不到面孔。