15

我在python中使用opencv的har級聯人臉檢測器(cv.HaarDetectObjects)。分類器對opencv人臉檢測器的信心

例如:

faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, 
    cv.CV_HAAR_DO_CANNY_PRUNING, (50,50)) 

     for f in faces: 
      print(f) 

這將打印檢測的列表以這種形式:

((174, 54, 114, 114), 53) 
((22, 51, 121, 121), 36) 
((321, 56, 114, 114), 21) 
((173, 263, 125, 125), 51) 
((323, 272, 114, 114), 20) 
((26, 271, 121, 121), 36) 

,其中每行代表一個檢測。前4個數字是左上角點的x,y位置,以及邊界框的高度,寬度。最後一個數字是(從openCV文檔引用的)鄰居的數量。

我想我有兩個問題:

1)最後一個數字是什麼意思?當用谷歌搜索時,我找不到任何提及。

2)(更重要)有沒有一種方法來獲得每個檢測的信心分數?臉部分類器有多少確定檢測對應於真實臉部?

謝謝

+2

可能有用:http://haoxiang.org/2013/11/opencv-detectmultiscale-output-detection-score/ – Shai

回答

0

非常感謝您的提問和回答,我一直在尋找opencv面部檢測,並以一天的信心評分。你的問題和答案給了我一些解決問題的指導。

就像Palmstrom說的那樣,最後一個數字表示該羣集中對象位置的數量。你可以用它作爲信心評分。

據我所知,在舊的python API中只有這種類型的API。新API沒有這個(羣集中的對象數)值。

我把我的代碼放在這裏以防萬一它會幫助其他人。這是一箇舊的python API,它的教程很難找到。

import sys 
import cv 

def detect_face(image): 
    image_size = cv.GetSize(image) 
    # # create grayscale version 
    grayscale = cv.CreateImage(image_size, 8, 1) 
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY) 
    # # equalize histogram 
    cv.EqualizeHist(grayscale,grayscale) 

    #parameters to the detection function  
    cascade = cv.Load('haarcascade_frontalface_alt.xml') 
    haar_scale = 1.1 
    min_neighbors = 3 
    haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING 
    min_size = (30,30) 

    faces = cv.HaarDetectObjects(grayscale, cascade, cv.CreateMemStorage(0), 
           haar_scale, min_neighbors, haar_flags, min_size) 

    print faces 

    if len(faces) > 0: 
     print '=> ' + str(len(faces)) + ' face detected!' 
     for ((x,y,width,height), n) in faces: 
      pt1 = (x,y) 
      pt2 = (x + width, y + height) 
      cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) 


if __name__ == '__main__': 

    filename = sys.argv[1] 
    image = cv.LoadImage(filename,cv.CV_LOAD_IMAGE_COLOR); 
    detect_face(image) 

    cv.ShowImage("cam", image) 
    cv.WaitKey(0)