如何使用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é)果圖表:
如你所見,大多數(shù)COCO圖片都包含一個(gè)人。但也有相當(dāng)多的13個(gè)人的照片,讓我們舉幾個(gè)例子:
好吧,甚至有一張圖片有19個(gè)注解(非人群):
這個(gè)圖像的頂部區(qū)域不應(yīng)該標(biāo)記為一個(gè)人群嗎?是的,應(yīng)該,但是,我們有多個(gè)沒有關(guān)鍵點(diǎn)的邊界框!這樣的注釋應(yīng)該像對(duì)待人群一樣對(duì)待,這意味著它們應(yīng)該被屏蔽。在這張圖片中,只有中間的3個(gè)方框有一些關(guān)鍵點(diǎn)。讓我們來優(yōu)化查詢,以獲取包含有/沒有關(guān)鍵點(diǎn)的人圖像的統(tǒng)計(jì)信息,以及有/沒有關(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官方頁面上描述有25萬人擁有關(guān)鍵點(diǎn),而我們只有156165個(gè)這樣的例子。他們可能應(yīng)該刪除了“帶關(guān)鍵點(diǎn)”這幾個(gè)字。添加額外列一旦我們將COCO轉(zhuǎn)換成pandas數(shù)據(jù)幀,我們就可以很容易地添加額外的列,從現(xiàn)有的列中計(jì)算出來。我認(rèn)為最好將所有的關(guān)鍵點(diǎn)坐標(biāo)提取到單獨(dú)的列中,此外,我們可以添加一個(gè)具有比例因子的列。特別是,關(guān)于一個(gè)人的邊界框的規(guī)模信息是非常有用的,例如,我們可能希望丟棄所有太小規(guī)模的人,或者執(zhí)行放大操作。為了實(shí)現(xiàn)這個(gè)目標(biāo),我們使用Python庫sklearn中的transformer對(duì)象。一般來說,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)論長度6~500個(gè)字
最新活動(dòng)更多
-
10月23日火熱報(bào)名中>> 2025是德科技創(chuàng)新技術(shù)峰會(huì)
-
10月23日立即報(bào)名>> Works With 開發(fā)者大會(huì)深圳站
-
11月7日立即參評(píng)>> 【評(píng)選】維科杯·OFweek 2025(第十屆)物聯(lián)網(wǎng)行業(yè)年度評(píng)選
-
即日-11.25立即下載>>> 費(fèi)斯托白皮書《柔性:汽車生產(chǎn)未來的關(guān)鍵》
-
11月27日立即報(bào)名>> 【工程師系列】汽車電子技術(shù)在線大會(huì)
-
11月28日立即下載>> 【白皮書】精準(zhǔn)洞察 無線掌控——283FC智能自檢萬用表
推薦專題
-
8 每日AI全球觀察
- 1 特斯拉工人被故障機(jī)器人打成重傷,索賠3.6億
- 2 【行業(yè)深度研究】退居幕后四年后,張一鳴終于把算法公司變成AI公司?
- 3 AI 時(shí)代,阿里云想當(dāng)“安卓” ,那誰是“蘋果”?
- 4 拐點(diǎn)已至!匯川領(lǐng)跑工控、埃斯頓份額第一、新時(shí)達(dá)海爾賦能扭虧為盈
- 5 硬剛英偉達(dá)!華為發(fā)布全球最強(qiáng)算力超節(jié)點(diǎn)和集群
- 6 隱退4年后,張一鳴久違現(xiàn)身!互聯(lián)網(wǎng)大佬正集體殺回
- 7 L3自動(dòng)駕駛延期,逼出車企技術(shù)自我淘汰
- 8 谷歌“香蕉”爆火啟示:國產(chǎn)垂類AI的危機(jī)還是轉(zhuǎn)機(jī)?
- 9 00后華裔女生靠兩部AI電影狂賺7.8億人民幣,AI正式進(jìn)軍好萊塢
- 10 機(jī)器人9月大事件|3家國產(chǎn)機(jī)器人沖刺IPO,行業(yè)交付與融資再創(chuàng)新高!
- 生產(chǎn)部總監(jiān) 廣東省/廣州市
- 資深管理人員 廣東省/江門市
- Regional Sales Manager 廣東省/深圳市
- 銷售總監(jiān) 廣東省/深圳市
- 結(jié)構(gòu)工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 自動(dòng)化高級(jí)工程師 廣東省/深圳市
- 技術(shù)專家 廣東省/江門市
- 激光器高級(jí)銷售經(jīng)理 上海市/虹口區(qū)
- 封裝工程師 北京市/海淀區(qū)