示例#1
0
def main():
	oriImgPath = './warp/warpImg'
	oriTxtPath = './warp/warpTxt'
	savePath = './warp/BBoxImages'

	oriImgNames = sorted(get_file_names(oriImgPath))
	total = len(oriImgNames)
	cnt = 1

	for oriImgName in oriImgNames:
		img = cv.imread(os.path.join(oriImgPath,oriImgName))
		print('has processed %d,total:%d' % (cnt,total))
		with open(os.path.join(oriTxtPath,os.path.splitext(oriImgName)[0] + '.txt'),'r') as f:
			lines = f.readlines()
			for line in lines:
				info = line.split()
				label = info[0]
				pos = [int(x) for x in info[1:5]] # list[xmin,ymin,bboxw,bboxh]
				color = tuple(random.randint(0,255) for x in range(3))
				cv.rectangle(img,(pos[0],pos[1]),(pos[0] + pos[2],pos[1] + pos[3]),color,2)
				cv.imwrite(os.path.join(savePath,oriImgName),img)
		cnt = cnt + 1
示例#2
0
def main():
    oriImgPath = '../data/my_VOC2007/warp/imgs'
    oriTxtPath = '../data/my_VOC2007/warp/labels/'
    savePath = '../data/my_VOC2007/warp/bboxImages/'
    jinbo.mkdir(savePath)
    oriImgNames = sorted(jinbo.get_file_names(oriImgPath))
    total = len(oriImgNames)
    cnt = 1

    for oriImgName in oriImgNames:
        img = cv.imread(os.path.join(oriImgPath, oriImgName))
        h, w, _ = img.shape
        print('has processed %d,total:%d' % (cnt, total))
        with open(
                os.path.join(oriTxtPath,
                             os.path.splitext(oriImgName)[0] + '.txt'),
                'r') as f:
            lines = f.readlines()
            for line in lines:
                info = line.split()
                # print(info)
                info = info[:1] + [float(x) for x in info[1:5]]
                xmin = int((info[1] - info[3] / 2) * w)
                ymin = int((info[2] - info[4] / 2) * h)
                bboxw = int(info[3] * w)
                bboxh = int(info[4] * h)
                # print(info)

                label = info[0]
                # pos = [int(x) for x in info[1:5]]  # list[xmin, ymin, bboxw, bboxh]
                color = tuple(random.randint(0, 255) for _ in range(3))
                # cv.rectangle(img, (pos[0], pos[1]), (pos[0] + pos[2], pos[1] + pos[3]), color, 2)
                # cv.imwrite(os.path.join(savePath, oriImgName), img)
                cv.rectangle(img, (xmin, ymin), (xmin + bboxw, ymin + bboxh),
                             color, 2)
                cv.imwrite(os.path.join(savePath, oriImgName), img)
        cnt = cnt + 1
示例#3
0
def main():
    init_name = 800000  # new name
    cnt = 1  # statistics
    # The optional parameter of data_format:[VOC2007,YOLO].VOC2007:[xmin,ymin,xmax,ymax].YOLO:[xcentre,ycentre,bboxw,bboxh]
    data_format = 'YOLO'
    oriImgPath = '../demo'
    oriTxtPath = '../demo/yolo_txt'
    warpImgPath = './warp/warpImg'
    warpTxtPath = './warp/warpTxt'
    angles = [
        -20, -10, -5, 5, 10, 20
    ]  # Positive number --> clockwise.The absolute value of the angle cannot exceed 90
    oriImgNames = sorted(get_file_names(oriImgPath))
    total = len(oriImgNames) * len(angles)

    for oriImgName in oriImgNames:
        img = cv.imread(os.path.join(oriImgPath, oriImgName))
        h, w = img.shape[:2]
        # print('shape',h,w)
        for angle in angles:
            imgwarp = warpAffine(img, angle)
            saveWarpImgPath = os.path.join(
                warpImgPath,
                str(int(os.path.splitext(oriImgName)[0]) + init_name + cnt) +
                '.jpg')
            cv.imwrite(saveWarpImgPath, imgwarp)
            print('has processed %d,total:%d' % (cnt, total))
            with open(
                    os.path.join(oriTxtPath,
                                 os.path.splitext(oriImgName)[0] + '.txt'),
                    'r') as f:
                lines = f.readlines()
                content = ''
                for line in lines:
                    info = line.split()
                    label = info[0]
                    # pos = [int(x) for x in info[1:5]] # xmin,ymin,w,h
                    if (data_format == 'VOC2007'):
                        pos = [
                            float(info[1]),
                            float(info[2]),
                            float(info[3]) - float(info[1]),
                            float(info[4]) - float(info[2])
                        ]
                        # print(pos)
                    if (data_format == 'YOLO'):
                        '''
						YOLO foamat:XCentre,yCentre,w,h
						'''
                        pos = [(float(info[1]) - float(info[3])/2.0) * w,\
                         (float(info[2]) - float(info[4])/2.0) * h,\
                         float(info[3]) * w,float(info[4]) * h]
                    BBS = newPosition(pos, w, h, angle)
                    content = content + label + ' ' + str(int(BBS[0])) + ' ' + str(int(BBS[1])) \
                      + ' ' + str(int(BBS[2])) + ' ' + str(int(BBS[3])) + '\n'
                    #print(BBS)
            #print(content)
            with open(
                    os.path.join(
                        warpTxtPath,
                        str(
                            int(os.path.splitext(oriImgName)[0]) + init_name +
                            cnt) + '.txt'), 'w') as fw:
                fw.write(content)
            cnt = cnt + 1
    want = 500
    # 创建由 digit_col 个数字字符和站digit_point 个小数点字符组成的字符串
    digit_col = 4
    digit_point = 2
    crop_and_save = False  # 是否已经从原图里裁剪出了各个类别,若是则不再执行该操作

    # Create dir
    createNewImg_path = opj(VOC_path, 'createNewImg')
    new_imgs_path = opj(createNewImg_path, 'imgs')
    material_path = opj(createNewImg_path, 'material')
    new_labels_path = opj(createNewImg_path, 'labels')
    jinbo.mkdir(new_imgs_path)
    jinbo.mkdir(new_labels_path)
    [jinbo.mkdir(opj(material_path, str(x))) for x in range(num_classes)]

    img_names = sorted(jinbo.get_file_names(opj(VOC_path, 'JPEGImages')))
    yolo_names = sorted(jinbo.get_file_names(opj(VOC_path, 'labels')))
    total = len(img_names)

    # Prepare to crop images
    if crop_and_save:
        print("已经从原图里裁剪出了各个类别")
    else:
        for img_name, yolo_name in zip(img_names, yolo_names):
            img = cv2.imread(opj(VOC_path, 'JPEGImages', img_name))
            im_h, im_w, channel = img.shape
            with open(opj(VOC_path, 'labels', yolo_name), 'r') as f:
                for info in f.readlines():
                    info = info.strip().split()
                    label = int(info[0])
                    cx, cy, w, h = [float(x) for x in info[1:]]
示例#5
0
    gap = 100
    init_name = 800000  # new name
    cnt = 0  # statistics
    # The optional parameter of data_format:[VOC2007,YOLO].
    # VOC2007:[xmin, ymin, xmax, ymax].
    # YOLO:[xcentre, ycentre, bboxw, bboxh]
    data_format = 'YOLO'
    oriImgPath = '../data/my_VOC2007/JPEGImages'
    oriTxtPath = '../data/my_VOC2007/labels'
    warpImgPath = '../data/my_VOC2007/warp/imgs'
    warpTxtPath = '../data/my_VOC2007/warp/labels'
    jinbo.mkdir(warpImgPath)
    jinbo.mkdir(warpTxtPath)
    # Positive number --> clockwise.The absolute value of the angle cannot exceed 90
    angles = [-7, -3, 3, 7]
    oriImgNames = sorted(jinbo.get_file_names(oriImgPath))
    total = len(oriImgNames) * len(angles)

    for oriImgName in oriImgNames:
        img = cv.imread(opj(oriImgPath, oriImgName))
        h, w = img.shape[:2]
        for angle in angles:
            imgwarp = warpAffine(img, angle)
            new_h, new_w, _ = imgwarp.shape
            saveWarpImgPath = opj(
                warpImgPath,
                str(int(os.path.splitext(oriImgName)[0]) + init_name + cnt) +
                '.jpg')
            cv.imwrite(saveWarpImgPath, imgwarp)
            with open(
                    opj(oriTxtPath,