網(wǎng)站開發(fā)的前后端是哪些搜索引擎營銷的常見方式
前言
當(dāng)你需要統(tǒng)計訓(xùn)練數(shù)據(jù)中每個類別標(biāo)簽有多少,并且想知道坐標(biāo)中心分布在圖像的位置信息時,你可以利用一下腳本進(jìn)行計算!
步驟
要繪制熱力圖來分析VOC數(shù)據(jù)的分布統(tǒng)計,可以按照以下步驟進(jìn)行:
- 數(shù)據(jù)處理:首先,你需要讀取VOC數(shù)據(jù)集的標(biāo)注文件,可以使用Python中的XML解析庫(如xml.etree.ElementTree)或者專門用于處理VOC數(shù)據(jù)集的工具庫(如vocparse)來解析XML文件。解析后,你可以獲取每個樣本的標(biāo)注信息,包括目標(biāo)類別、邊界框位置等。
- 統(tǒng)計數(shù)據(jù)分布:遍歷所有樣本的標(biāo)注信息,統(tǒng)計每個類別在圖像中出現(xiàn)的次數(shù)或占比。根據(jù)需要,你可以選擇統(tǒng)計全局的數(shù)據(jù)分布,或者針對特定區(qū)域或圖像子集進(jìn)行統(tǒng)計。將統(tǒng)計結(jié)果存儲在一個二維數(shù)組或字典中,以便后續(xù)生成熱力圖。
- 繪制熱力圖:根據(jù)統(tǒng)計結(jié)果,使用Python中的數(shù)據(jù)可視化庫(如matplotlib、seaborn等)來繪制熱力圖。熱力圖可以使用顏色來表示數(shù)據(jù)的密度或占比。一種常見的繪制方法是使用imshow函數(shù),傳入統(tǒng)計結(jié)果的二維數(shù)組,設(shè)置合適的顏色映射和標(biāo)簽等。
代碼塊
import os
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt# VOC數(shù)據(jù)集路徑
dataset_path = 'Annotations/'# 存儲標(biāo)簽及其對應(yīng)的目標(biāo)框數(shù)量
label_counts = {}
image_width = 1280
image_height = 960
block_size = 40# 創(chuàng)建一個二維數(shù)組,用于存儲每個塊中目標(biāo)框的數(shù)量
block_counts = np.zeros((image_height // block_size, image_width // block_size))# 遍歷數(shù)據(jù)集中的每個XML文件
i=0
for filename in os.listdir(dataset_path):if filename.endswith('.xml'):# 解析XML文件tree = ET.parse(os.path.join(dataset_path, filename))root = tree.getroot()# 遍歷XML文件中的所有目標(biāo)框for obj in root.findall('object'):label = obj.find('name').textif label=='vehicle':xmin = int(float(obj.find('bndbox/xmin').text))ymin = int(float(obj.find('bndbox/ymin').text))xmax = int(float(obj.find('bndbox/xmax').text))ymax = int(float(obj.find('bndbox/ymax').text))x_pixel = int((xmin + ymin) / 2)y_pixel = ymax# 將底部中心點映射到相應(yīng)的像素塊block_x = x_pixel // block_sizeblock_y = y_pixel // block_size# 統(tǒng)計該像素塊中目標(biāo)框的數(shù)量block_counts[block_y, block_x] += 1i+=1
plt.imshow(block_counts, cmap='hot')
plt.colorbar()# 設(shè)置坐標(biāo)軸
plt.xlabel('Blocks (50x50 pixels)')
plt.ylabel('Blocks (50x50 pixels)')
plt.title('Object Distribution Heatmap')# 顯示熱力圖
plt.show()
print(block_counts)
print("該標(biāo)簽有",i)
代碼講解
在進(jìn)行VOC數(shù)據(jù)集的類別標(biāo)簽分布和數(shù)量統(tǒng)計時,有以下幾個需要注意的點:
- 數(shù)據(jù)集路徑:確保設(shè)置正確的數(shù)據(jù)集路徑,指向包含XML文件的文件夾。
- 標(biāo)簽統(tǒng)計:使用一個字典或其他適合的數(shù)據(jù)結(jié)構(gòu)來存儲每個標(biāo)簽及其對應(yīng)的目標(biāo)框數(shù)量。可以使用標(biāo)簽作為鍵,目標(biāo)框數(shù)量作為值。
- XML解析:使用適當(dāng)?shù)腦ML解析庫(如xml.etree.ElementTree)解析XML文件。檢查XML文件中的標(biāo)簽結(jié)構(gòu),并定位到目標(biāo)框的位置信息。
- 目標(biāo)框位置信息:目標(biāo)框通常由左上角和右下角的坐標(biāo)表示(例如xmin、ymin、xmax、ymax)。確保正確提取這些坐標(biāo),并轉(zhuǎn)換為適當(dāng)?shù)母袷健?/li>
- 統(tǒng)計目標(biāo)框數(shù)量:根據(jù)目標(biāo)框的位置信息,可以將它們映射到圖像的像素塊中,并在相應(yīng)的像素塊中遞增目標(biāo)框數(shù)量。這樣就可以統(tǒng)計每個像素塊中目標(biāo)框的數(shù)量。
- 繪制熱力圖:使用合適的可視化庫(如matplotlib.pyplot)繪制熱力圖,以展示目標(biāo)分布情況。熱力圖的顏色可以根據(jù)目標(biāo)框數(shù)量的大小進(jìn)行漸變。
- 坐標(biāo)軸和標(biāo)題:設(shè)置適當(dāng)?shù)淖鴺?biāo)軸標(biāo)簽和標(biāo)題,以說明熱力圖的含義和解釋。
- 顯示熱力圖:使用適當(dāng)?shù)暮瘮?shù)(如plt.show())顯示生成的熱力圖。
import os # 導(dǎo)入os模塊,用于文件操作
import xml.etree.ElementTree as ET # 導(dǎo)入xml.etree.ElementTree模塊,用于解析XML文件
import matplotlib.pyplot as plt # 導(dǎo)入matplotlib.pyplot模塊,用于繪圖
import numpy as np # 導(dǎo)入numpy模塊,用于科學(xué)計算
VOC數(shù)據(jù)集路徑
dataset_path = 'Annotations/'
存儲標(biāo)簽及其對應(yīng)的目標(biāo)框數(shù)量
label_counts = {}
圖像的寬度和高度
image_width = 1280
image_height = 960
每個像素塊的大小
block_size = 40
創(chuàng)建一個二維數(shù)組,用于存儲每個塊中目標(biāo)框的數(shù)量
block_counts = np.zeros((image_height // block_size, image_width // block_size))
遍歷數(shù)據(jù)集中的每個XML文件
i = 0
for filename in os.listdir(dataset_path):if filename.endswith('.xml'):# 解析XML文件tree = ET.parse(os.path.join(dataset_path, filename))root = tree.getroot()
# 遍歷XML文件中的所有目標(biāo)框for obj in root.findall('object'):label = obj.find('name').text# 判斷標(biāo)簽是否為'vehicle'if label == 'vehicle':# 獲取目標(biāo)框的坐標(biāo)信息xmin = int(float(obj.find('bndbox/xmin').text))ymin = int(float(obj.find('bndbox/ymin').text))xmax = int(float(obj.find('bndbox/xmax').text))ymax = int(float(obj.find('bndbox/ymax').text))# 計算目標(biāo)框的底部中心點坐標(biāo)x_pixel = int((xmin + ymin) / 2)y_pixel = ymax# 將底部中心點映射到相應(yīng)的像素塊block_x = x_pixel // block_sizeblock_y = y_pixel // block_size# 統(tǒng)計該像素塊中目標(biāo)框的數(shù)量block_counts[block_y, block_x] += 1i += 1
繪制熱力圖
plt.imshow(block_counts, cmap='hot')
plt.colorbar()
設(shè)置坐標(biāo)軸和標(biāo)題
plt.xlabel('Blocks (50x50 pixels)')
plt.ylabel('Blocks (50x50 pixels)')
plt.title('Object Distribution Heatmap')
顯示熱力圖
plt.show()print(block_counts)
print("該標(biāo)簽有", i)
#聯(lián)系 qq 1309399183