Exemplo n.º 1
0
def count_and_cut(input_path, count):
    annot_path = input_path + "\\Annotations"
    img_path = input_path + "\\JPEGImages"
    save_folder = input_path + "\\Cutout"
    io_utils.mkdir(save_folder)
    io_utils.remove_all(save_folder)
    annots = [os.path.join(annot_path, s) for s in os.listdir(annot_path)]
    for annot in annots:  # read a xml
        try:
            et = ET.parse(annot)
            element = et.getroot()
            element_objs = element.findall('object')
            img_name = element.find('filename').text

            new_img_path = img_path + "\\" + img_name  # find uncut image
            for element_obj in element_objs:
                class_name = element_obj.find('name').text  # find label
                count[class_name][img_name] = count[class_name][img_name] + 1
                save_path = save_folder + "\\" + class_name
                save_name = img_name.split('.')[0] + '-' + str(
                    count[class_name][img_name]) + '.jpg'
                io_utils.mkdir(save_path)
                xmin = int(element_obj.find("bndbox").find(
                    "xmin").text)  # find bbox boundary
                ymin = int(element_obj.find("bndbox").find("ymin").text)
                xmax = int(element_obj.find("bndbox").find("xmax").text)
                ymax = int(element_obj.find("bndbox").find("ymax").text)
                box = (xmin, ymin, xmax, ymax)
                img = Image.open(new_img_path)
                region = img.crop(box)
                region.save(save_path + "\\" + save_name)
        except Exception as e:
            print('Exception: {}'.format(e))
            continue
Exemplo n.º 2
0
def _copy_to_JPEGImages(parent_dir, src_dir):
    target_dir = os.path.join(parent_dir, 'JPEGImages/')
    io_utils.mkdir(target_dir)
    io_utils.remove_all(target_dir)
    for s in os.listdir(src_dir):
        file = os.path.join(src_dir, s)
        io_utils.copy(file, target_dir)
Exemplo n.º 3
0
    # when images have a large aspect ratio
    largest_side = max(rows, cols)
    if largest_side * scale > max_side:
        scale = max_side / largest_side

    # resize the image with the computed scale
    img = cv2.resize(img, None, fx=scale, fy=scale)
    output_path = os.path.join(
        output_path, "background" + "_" + str_date[0:4] + "-" + str_date[4:6] +
        "-" + str_date[6:8] + "-" + str(n) + ".jpg")
    cv2.imwrite(output_path, img)  #output img name,change here!!
    #return img, scale


#some path
parent_path = "test_folder/Background"
origin_path = os.path.join(parent_path, "origin")
handle_path = os.path.join(parent_path, "after")

str_date = '{year}{month}{day}'.format(year='2018', month='05',
                                       day='29')  #改这里,日期

if __name__ == '__main__':
    ncount = 10000
    io_utils.mkdir(handle_path)
    io_utils.remove_all(handle_path)
    for f in os.listdir(origin_path):
        img_path = os.path.join(origin_path, f)
        handle(img_path, 800, 1333, handle_path, ncount)  #background:800*1333
        ncount += 1