當前位置:成語大全網 - 新華字典 - 目標檢測:原始圖片候選框labels生成,.npy形式保存

目標檢測:原始圖片候選框labels生成,.npy形式保存

def generate_labels():

class_num =8

path ='./ground.txt'

processed_path ='./Data/processed/'

with codecs.open(path, 'r', 'utf-8')as f:

lines = f.readlines()

for num, image_idxin enumerate(lines):

"""print(num+1, ' ', image_idx, '\n')"""

ground_truth_dic = load_annotation(image_idx)

image_path = ('./Data/Images/' + image_idx).strip('\n') +'.jpg'

# print(image_path)

img = cv2.imread(image_path)

# print(img.shape)

img_lbl, regions = selectivesearch.selective_search(img, scale=1000, sigma=0.9, min_size=1000)

labels = []

for rin regions:

x, y, w, h = r['rect']# x,y,w,h中,(x,y)是bounding box的左下角的坐標,w,h代表寬和高

proposal_vertice = [x +1, y, x + w, y + h, w, h]

proposal_bbox = [x, y, (x + w -1), (y + h -1)]

label = np.zeros(class_num *5 -4, dtype=np.float32)

iou_val =0

# 字典的items()函數:返回可遍歷的(鍵, 值) 元組數組

for ground_truth, class_idxin ground_truth_dic.items():

# ground_truth = list(ground_truth)

xmin = (2 * ground_truth[0] - ground_truth[2]) /2.0

ymin = (2 * ground_truth[1] - ground_truth[3]) /2.0

ground_truth = [xmin, ymin, ground_truth[2], ground_truth[3]]

iou_val = IOU(ground_truth, proposal_bbox)

"""

(px,py,pw,ph)是selectivesearch生成的bounding box的中心點坐標、寬、高

(gx,gy,gw,gh)是解析xml信息中標註的groundtruth中的標註框的中心點坐標、寬、高

"""

px =float(proposal_vertice[0]) +float(proposal_vertice[4] /2.0)# 中心點X

py =float(proposal_vertice[1]) +float(proposal_vertice[5] /2.0)# 中心點Y

pw =float(proposal_vertice[4])# w

ph =float(proposal_vertice[5])# h

gx =float(ground_truth[0])# 中心點X

gy =float(ground_truth[1])# 中心點Y

gw =float(ground_truth[2])# W

gh =float(ground_truth[3])# H

if iou_val <0.7:

label[0] =1

elif iou_val >0.7:

label[0] =0

label[class_idx] =1

# 下面這行是啥意思,不懂

label[class_num + (class_idx -1) *4: class_num + (class_idx -1) *4 +4] = \

[((gx - px) / pw), ((gy - py) / ph), (np.log(gw / pw)), (np.log(gh / ph))]

break

for iin range(len(proposal_bbox)):

proposal_bbox[i] = (proposal_bbox[i] /16.0)

proposal_bbox.insert(0, 0)

proposal_bbox.insert(0, iou_val)

proposal_bbox.extend(label)

labels.append(proposal_bbox)

"""

for k in labels:

print(k, '\n')

"""

view_bar("Process image of %s" % image_path, num +1, len(lines))

if True:

if not os.path.exists(processed_path):? os.makedirs(processed_path)

np.save((os.path.join(processed_path, image_idx.split('.')[0].strip())

+'_data.npy'), labels)

最終每壹張圖片生成壹個.npy文件。對每個圖片生成的候選框進行選擇,通過選擇的候選框將生成相應的labels,壹個圖片中所有的labels生成壹個.npy文件。