訂閱
糾錯
加入自媒體

如何使用Python分析姿態(tài)估計數(shù)據(jù)集COCO?

   # 遍歷所有圖像
   for img_id, img_fname, w, h, meta in get_meta(coco):
       images_data.a(chǎn)ppend({
           'image_id': int(img_id),
           'path': img_fname,
           'width': int(w),
           'height': int(h)
       })
       
       # 遍歷所有元數(shù)據(jù)
       for m in meta:
           persons_data.a(chǎn)ppend({
               'image_id': m['image_id'],
               'is_crowd': m['iscrowd'],
               'bbox': m['bbox'],
               'area': m['area'],
               'num_keypoints': m['num_keypoints'],
               'keypoints': m['keypoints'],
           })
           
   # 創(chuàng)建帶有圖像路徑的數(shù)據(jù)幀
   images_df = pd.DataFrame(images_data)
   images_df.set_index('image_id', inplace=True)
   
   # 創(chuàng)建與人相關(guān)的數(shù)據(jù)幀
   persons_df = pd.DataFrame(persons_data)
   persons_df.set_index('image_id', inplace=True)
   return images_df, persons_df
我們使用get_meta函數(shù)構(gòu)造兩個數(shù)據(jù)幀—一個用于圖像路徑,另一個用于人的元數(shù)據(jù)。在一個圖像中可能有多個人,因此是一對多的關(guān)系。在下一步中,我們合并兩個表(left join操作)并將訓(xùn)練集和驗證集組合,另外,我們添加了一個新列source,值為0表示訓(xùn)練集,值為1表示驗證集。這樣的信息是必要的,因為我們需要知道應(yīng)該在哪個文件夾中搜索圖像。如你所知,這些圖像位于兩個文件夾中:train2017/和val2017/images_df, persons_df = convert_to_df(train_coco)
train_coco_df = pd.merge(images_df, persons_df, right_index=True, left_index=True)
train_coco_df['source'] = 0
images_df, persons_df = convert_to_df(val_coco)
val_coco_df = pd.merge(images_df, persons_df, right_index=True, left_index=True)
val_coco_df['source'] = 1
coco_df = pd.concat([train_coco_df, val_coco_df], ignore_index=True)
最后,我們有一個表示整個COCO數(shù)據(jù)集的數(shù)據(jù)幀。圖像中有多少人現(xiàn)在我們可以執(zhí)行第一個分析。COCO數(shù)據(jù)集包含多個人的圖像,我們想知道有多少圖像只包含一個人。代碼如下:# 計數(shù)
annotated_persons_df = coco_df[coco_df['is_crowd'] == 0]
crowd_df = coco_df[coco_df['is_crowd'] == 1]
print("Number of people in total: " + str(len(annotated_persons_df)))
print("Number of crowd annotations: " + str(len(crowd_df)))
persons_in_img_df = pd.DataFrame({
   'cnt': annotated_persons_df['path'].value_counts()
})
persons_in_img_df.reset_index(level=0, inplace=True)
persons_in_img_df.rename(columns = {'index':'path'}, inplace = True)
# 按cnt分組,這樣我們就可以在一張圖片中得到帶有注釋人數(shù)的數(shù)據(jù)幀
persons_in_img_df = persons_in_img_df.groupby(['cnt']).count()
# 提取數(shù)組
x_occurences = persons_in_img_df.index.values
y_images = persons_in_img_df['path'].values
# 繪圖
plt.bar(x_occurences, y_images)
plt.title('People on a single image ')
plt.xticks(x_occurences, x_occurences)
plt.xlabel('Number of people in a single image')
plt.ylabel('Number of images')
plt.show()
結(jié)果圖表:

如你所見,大多數(shù)COCO圖片都包含一個人。但也有相當(dāng)多的13個人的照片,讓我們舉幾個例子:

好吧,甚至有一張圖片有19個注解(非人群):

這個圖像的頂部區(qū)域不應(yīng)該標(biāo)記為一個人群嗎?是的,應(yīng)該,但是,我們有多個沒有關(guān)鍵點的邊界框!這樣的注釋應(yīng)該像對待人群一樣對待,這意味著它們應(yīng)該被屏蔽。在這張圖片中,只有中間的3個方框有一些關(guān)鍵點。讓我們來優(yōu)化查詢,以獲取包含有/沒有關(guān)鍵點的人圖像的統(tǒng)計信息,以及有/沒有關(guān)鍵點的人的總數(shù):annotated_persons_nokp_df = coco_df[(coco_df['is_crowd'] == 0) & (coco_df['num_keypoints'] == 0)]
annotated_persons_kp_df = coco_df[(coco_df['is_crowd'] == 0) & (coco_df['num_keypoints'] > 0)]
print("Number of people (with keypoints) in total: " +
       str(len(annotated_persons_kp_df)))
print("Number of people without any keypoints in total: " +
       str(len(annotated_persons_nokp_df)))
persons_in_img_kp_df = pd.DataFrame({
   'cnt': annotated_persons_kp_df[['path','source']].value_counts()
})
persons_in_img_kp_df.reset_index(level=[0,1], inplace=True)
persons_in_img_cnt_df = persons_in_img_kp_df.groupby(['cnt']).count()
x_occurences_kp = persons_in_img_cnt_df.index.values
y_images_kp = persons_in_img_cnt_df['path'].values
f = plt.figure(figsize=(14, 8))
width = 0.4
plt.bar(x_occurences_kp, y_images_kp, width=width, label='with keypoints')
plt.bar(x_occurences + width, y_images, width=width, label='no keypoints')
plt.title('People on a single image ')
plt.xticks(x_occurences + width/2, x_occurences)
plt.xlabel('Number of people in a single image')
plt.ylabel('Number of images')
plt.legend(loc = 'best')
plt.show()
現(xiàn)在我們可以看到區(qū)別是明顯的。

雖然COCO官方頁面上描述有25萬人擁有關(guān)鍵點,而我們只有156165個這樣的例子。他們可能應(yīng)該刪除了“帶關(guān)鍵點”這幾個字。添加額外列一旦我們將COCO轉(zhuǎn)換成pandas數(shù)據(jù)幀,我們就可以很容易地添加額外的列,從現(xiàn)有的列中計算出來。我認(rèn)為最好將所有的關(guān)鍵點坐標(biāo)提取到單獨的列中,此外,我們可以添加一個具有比例因子的列。特別是,關(guān)于一個人的邊界框的規(guī)模信息是非常有用的,例如,我們可能希望丟棄所有太小規(guī)模的人,或者執(zhí)行放大操作。為了實現(xiàn)這個目標(biāo),我們使用Python庫sklearn中的transformer對象。一般來說,sklearn transformers是用于清理、減少、擴(kuò)展和生成數(shù)據(jù)科學(xué)模型中的特征表示的強(qiáng)大工具。我們只會用一小部分的api。代碼如下:from sklearn.base import BaseEstimator, TransformerMixin
class AttributesAdder(BaseEstimator, TransformerMixin):
   def __init__(self, num_keypoints, w_ix, h_ix, bbox_ix, kp_ix):
       """
       :param num_keypoints: 關(guān)鍵點的數(shù)量
       :param w_ix: 包含圖像寬度的列索引
       :param h_ix: 包含圖像高度的列索引
       :param bbox_ix: 包含邊框數(shù)據(jù)的列索引
       :param kp_ix: 包含關(guān)鍵點數(shù)據(jù)的列索引
       """
       self.num_keypoints = num_keypoints
       self.w_ix = w_ix
       self.h_ix = h_ix
       self.bbox_ix = bbox_ix
       self.kp_ix = kp_ix
       
   def fit(self, X, y=None):
       return self
     
   def transform(self, X):
     
       # 檢索特定列
       
       w = X[:, self.w_ix]
       h = X[:, self.h_ix]
       bbox = np.a(chǎn)rray(X[:, self.bbox_ix].tolist())  # to matrix
       keypoints = np.a(chǎn)rray(X[:, self.kp_ix].tolist()) # to matrix

<上一頁  1  2  3  下一頁>  
聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權(quán)或其他問題,請聯(lián)系舉報。

發(fā)表評論

0條評論,0人參與

請輸入評論內(nèi)容...

請輸入評論/評論長度6~500個字

您提交的評論過于頻繁,請輸入驗證碼繼續(xù)

暫無評論

暫無評論

    掃碼關(guān)注公眾號
    OFweek人工智能網(wǎng)
    獲取更多精彩內(nèi)容
    文章糾錯
    x
    *文字標(biāo)題:
    *糾錯內(nèi)容:
    聯(lián)系郵箱:
    *驗 證 碼:

    粵公網(wǎng)安備 44030502002758號