目录
项目数据处理总结
一:数据预处理
二:数据后处理
摘要:
最近刚刚入门的深度学习,跟着学长们做了个项目,我主要负责数据标注、数据预处理和数据后处理。在这里总结一下,方便日后查阅
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")