目标检测:原始图片候选框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文件。


本文整理转载自其它博文,如有侵权,请联系364017364@qq.com删除。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容