如何使用Python分析姿態(tài)估計(jì)數(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)造兩個(gè)數(shù)據(jù)幀—一個(gè)用于圖像路徑,另一個(gè)用于人的元數(shù)據(jù)。在一個(gè)圖像中可能有多個(gè)人,因此是一對(duì)多的關(guān)系。在下一步中,我們合并兩個(gè)表(left join操作)并將訓(xùn)練集和驗(yàn)證集組合,另外,我們添加了一個(gè)新列source,值為0表示訓(xùn)練集,值為1表示驗(yàn)證集。這樣的信息是必要的,因?yàn)槲覀冃枰缿?yīng)該在哪個(gè)文件夾中搜索圖像。如你所知,這些圖像位于兩個(gè)文件夾中: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)
最后,我們有一個(gè)表示整個(gè)COCO數(shù)據(jù)集的數(shù)據(jù)幀。圖像中有多少人現(xiàn)在我們可以執(zhí)行第一個(gè)分析。COCO數(shù)據(jù)集包含多個(gè)人的圖像,我們想知道有多少圖像只包含一個(gè)人。代碼如下:# 計(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é)果圖表:
如你所見(jiàn),大多數(shù)COCO圖片都包含一個(gè)人。但也有相當(dāng)多的13個(gè)人的照片,讓我們舉幾個(gè)例子:
好吧,甚至有一張圖片有19個(gè)注解(非人群):
這個(gè)圖像的頂部區(qū)域不應(yīng)該標(biāo)記為一個(gè)人群?jiǎn)幔渴堑模瑧?yīng)該,但是,我們有多個(gè)沒(méi)有關(guān)鍵點(diǎn)的邊界框!這樣的注釋?xiě)?yīng)該像對(duì)待人群一樣對(duì)待,這意味著它們應(yīng)該被屏蔽。在這張圖片中,只有中間的3個(gè)方框有一些關(guān)鍵點(diǎn)。讓我們來(lái)優(yōu)化查詢(xún),以獲取包含有/沒(méi)有關(guān)鍵點(diǎn)的人圖像的統(tǒng)計(jì)信息,以及有/沒(méi)有關(guān)鍵點(diǎ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官方頁(yè)面上描述有25萬(wàn)人擁有關(guān)鍵點(diǎn),而我們只有156165個(gè)這樣的例子。他們可能應(yīng)該刪除了“帶關(guān)鍵點(diǎn)”這幾個(gè)字。添加額外列一旦我們將COCO轉(zhuǎn)換成pandas數(shù)據(jù)幀,我們就可以很容易地添加額外的列,從現(xiàn)有的列中計(jì)算出來(lái)。我認(rèn)為最好將所有的關(guān)鍵點(diǎn)坐標(biāo)提取到單獨(dú)的列中,此外,我們可以添加一個(gè)具有比例因子的列。特別是,關(guān)于一個(gè)人的邊界框的規(guī)模信息是非常有用的,例如,我們可能希望丟棄所有太小規(guī)模的人,或者執(zhí)行放大操作。為了實(shí)現(xiàn)這個(gè)目標(biāo),我們使用Python庫(kù)sklearn中的transformer對(duì)象。一般來(lái)說(shuō),sklearn transformers是用于清理、減少、擴(kuò)展和生成數(shù)據(jù)科學(xué)模型中的特征表示的強(qiáng)大工具。我們只會(huì)用一小部分的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)鍵點(diǎn)的數(shù)量
:param w_ix: 包含圖像寬度的列索引
:param h_ix: 包含圖像高度的列索引
:param bbox_ix: 包含邊框數(shù)據(jù)的列索引
:param kp_ix: 包含關(guān)鍵點(diǎ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

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
3月27日立即報(bào)名>> 【工程師系列】汽車(chē)電子技術(shù)在線(xiàn)大會(huì)
-
4月30日立即下載>> 【村田汽車(chē)】汽車(chē)E/E架構(gòu)革新中,新智能座艙挑戰(zhàn)的解決方案
-
5月15-17日立即預(yù)約>> 【線(xiàn)下巡回】2025年STM32峰會(huì)
-
即日-5.15立即報(bào)名>>> 【在線(xiàn)會(huì)議】安森美Hyperlux™ ID系列引領(lǐng)iToF技術(shù)革新
-
5月15日立即下載>> 【白皮書(shū)】精確和高效地表征3000V/20A功率器件應(yīng)用指南
-
5月16日立即參評(píng) >> 【評(píng)選啟動(dòng)】維科杯·OFweek 2025(第十屆)人工智能行業(yè)年度評(píng)選
推薦專(zhuān)題
- 1 UALink規(guī)范發(fā)布:挑戰(zhàn)英偉達(dá)AI統(tǒng)治的開(kāi)始
- 2 “AI寒武紀(jì)”爆發(fā)至今,五類(lèi)新物種登上歷史舞臺(tái)
- 3 降薪、加班、裁員三重暴擊,“AI四小龍”已折戟兩家
- 4 光計(jì)算迎來(lái)商業(yè)化突破,但落地仍需時(shí)間
- 5 大模型下半場(chǎng):Agent時(shí)代為何更需要開(kāi)源模型
- 6 中國(guó)“智造”背后的「關(guān)鍵力量」
- 7 優(yōu)必選:營(yíng)收大增主靠小件,虧損繼續(xù)又逢關(guān)稅,能否乘機(jī)器人東風(fēng)翻身?
- 8 營(yíng)收猛增46%,昆侖萬(wàn)維成為AI“爆品工廠”
- 9 全球無(wú)人駕駛技術(shù)排名:誰(shuí)才是細(xì)分賽道的扛把子?
- 10 地平線(xiàn)自動(dòng)駕駛方案解讀