2017-01-04 12 views
1

年齢、性別推定プロジェクトに取り組んでいます。これまでは、性別分類のためにLBP(Local Binary Patterns)+ SVM(Support Vector Machines)を試してきましたが、LBP + SVMで作業している間に偽陽性が増えすぎているので、HOG(Histogram of Gradients) )+ SVMとなり、驚くほど正確度は90%まで向上しました。だから私は両方の記述子の機能を併合し、それを使ってSVMを訓練します。これは、次のようなコードは次のとおりです。LBPとHOGの特徴記述子をマージする

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 

    fd = hog(gray, orientations, pixels_per_cell, cells_per_block, visualize, normalize) #HOG descriptor here. 

    hist = desc.describe(gray) #get the LBP histogram here. 

    # extract the label from the image path, then update the 
    # label and data lists 
    labels.append(imagePath.split("/")[-2]) 
    data.append(fd + hist) # tried concatinate both featurs, but gives error on this line. 

# train a Linear SVM on the data 
model = LinearSVC(C=100.0, random_state=42) 
model.fit(data, labels) 

しかし、ときに、この行を試みた:data.append(fd + hist)は、単に機能discriptorsの両方をCONCATしようとすると、エラー次のように私をスロー:

Traceback (most recent call last): File "/home/swap/Ubuntu-Home/swap/openCV/gender_age_weight_recog/tarin.py",

line 41, in data.append(fd+hist) ValueError: operands could not be broadcast together with shapes (11340,) (26,)

だから誰かが私を指すことができます2つのフィーチャを1つにまとめてSVMを訓練してください。

+0

+1です。私の好きなだけでなく、新しい答えが出てきたら私が従うことができます。私の提案は、各画像に対してHOGとLBPのヒストグラムのタプルを含むリストを割り当てる方法です。タプルには、各画像のHOGとLBPの両方のヒストグラムが含まれます。これらのタプルはすべてリスト内で取得できます。 –

+0

ええ、それは良いものです、それを試してみましょう! –

答えて

0

問題を見つけました。その後のすべてのことができ、HOGとLBPHのような形状がLBP,HOG will always be oneによって生成機能について、その場合の深さのように、グレースケール画像上で動作しますので、我々はnumpyの使用してそれらの両方をスタックすることができ、

desc_hist = desc.describe(gray_img) 
    hog_hist = hog(gray_img, orientations, pixels_per_cell, cells_per_block, 'L1', visualize, normalize)  
    feat = np.hstack([desc_hist, hog_hist]) 

but suppose one wants to merge hsv histogram which works on 3 channel image(RGB), then it can be flattened to 1D array and then can be stacked to posses that feature as well.

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) 
hist = cv2.calcHist([hsv], [0, 1, 2], None, bins, 
        [0, 180, 0, 256, 0, 256]) 

hist = cv2.normalize(hist) 

# return the flattened histogram as the feature vector 
td_hist = hist.flatten() 

通常通りstackedになります。

feat = np.hstack([desc_hist, hog_hist, td_hist]) 
0

異なるサイズの2つのアレイを追加しようとしています。 1つの配列には11340個の要素があり、もう1個には26個の要素があります。これらの値を格納し、それらを一緒に追加しないときにはロジックを変更する必要があります。

関連する問題