關於數據處理的總結

目錄

項目數據處理總結

一:數據預處理

二:數據後處理

項目數據處理總結

摘要:

        最近剛剛入門的深度學習,跟着學長們做了個項目,我主要負責數據標註、數據預處理和數據後處理。在這裏總結一下,方便日後查閱

一:數據預處理

 

def rename():
    r = os.getcwd()
    root = os.path.join(r, "images")
    path = os.path.join(root, "origin")
    result = os.path.join(root, "preprocessing/rename")
    jpg = os.path.join(root, "preprocessing/jpg")
    if os.path.exists(result):
        shutil.rmtree(result)
    if os.path.exists(jpg):
        shutil.rmtree(jpg)

    shutil.copytree(path, result)

    f = os.listdir(result)
    f = natsort.natsorted(f)
    # print(f)
    n = 0
    for i in f:
        oldname = result + '/' + f[n]
        newname = result + '/' + str(n).zfill(4) + ".bmp"
        os.rename(oldname, newname)
        n += 1

    os.mkdir(jpg)
    for filename in natsort.natsorted(os.listdir(result)):
        newfilename = filename[0:filename.find(".")] + ".jpg"
        im = Image.open(result + '/' + filename)
        im.save(jpg + '/' + newfilename)

def crop():
    r = os.getcwd()
    root = os.path.join(r, "images")
    crop = os.path.join(root, "preprocessing/crop")
    jpg = os.path.join(root, "preprocessing/jpg")
    if os.path.exists(crop):
        shutil.rmtree(crop)
    os.mkdir(crop)
    for filename in natsort.natsorted(os.listdir(jpg)):
        im = Image.open(jpg + '/' + filename)
        x = 400
        y = 0
        w = 2448
        h = 2048
        region = im.crop((x, y, w, h))
        region.save(crop + '/' + filename)

def true_crop():
    r = os.getcwd()
    root = os.path.join(r, "images")
    crop = os.path.join(root, "preprocessing/crop")
    test = os.path.join(root, "stone/test")
    if os.path.exists(test):
        shutil.rmtree(test)
    os.mkdir(test)

    # 裁剪掉原圖兩邊邊界的多餘背景
    for filename in natsort.natsorted(os.listdir(crop)):
        im = Image.open(crop + '/' + filename)
        # 圖片的寬度和高度
        # img_size = im.size
        xy = ([0, 0], [452, 0], [964, 0], [1416, 0],
              [0, 452], [452, 452], [964, 452], [1416, 452],
              [0, 964], [452, 964], [964, 964], [1416, 964],
              [0, 1416], [452, 1416], [964, 1416], [1416, 1416])

        for i in range(16):
            x, y = xy[i]
            w = 632
            h = 632
            region = im.crop((x, y, x + w, y + h))
            # print(x, y,region)
            region.save(test + "/" + str(int(filename[0:filename.find(".")])) + '_' + str(i) + ".jpg")
            # 得到素材

def remake_true_result():
    root = os.getcwd()
    crop = os.path.join(root, "images/preprocessing/crop")
    true_result = os.path.join(root, "images/true_result")
    if os.path.exists(true_result):
        shutil.rmtree(true_result)
    os.mkdir(true_result)

二:數據後處理

 

def apply_mask(image, mask, color, alpha=0.5):
    """Apply the given mask to the image.
    """
    for c in range(3):
        image[:, :, c] = np.where(mask >= 1, image[:, :, c] * (1 - alpha) + alpha * color[c] * 255, image[:, :, c])
    return image


def matrix_visible(m, i):
    root = os.getcwd()
    crop = os.path.join(root, "images/preprocessing/crop")
    true_result = os.path.join(root, "images/true_result")
    image = skimage.io.imread(crop + "/" + str(i).zfill(4) +".jpg")
    if image.ndim != 3:
        image = skimage.color.gray2rgb(image)
    masked_image = image.astype(np.uint32).copy()

    fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    height, width = masked_image.shape[:2]
    ax.set_ylim(height, 0)
    ax.set_xlim(0, width)
    ax.axis('on')

    color = (0.0, 0.6470588235294117, 1.0)
    masked_image = apply_mask(masked_image, m, color)
    ax.imshow(masked_image.astype(np.uint8))
    plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)  # 去掉白邊
    plt.savefig(true_result + '/' + str(i).zfill(4) + ".jpg")

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章