目录
路径类
1. 获取一个路径最后一个目录或文件的名字
图片处理类
1. 数据增强,图像左右反转
2. 数据增强,随机缩放
3. 数据增强,随机裁剪
4. 读取图片的shape格式
5. cv2 BGR转RGB
读取数据集
1. ShanghaiTech
数据持久化
1. h5py存储数据
os.path.basename(path)
可以对Image对象进行如下操作:
new_img = img.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
也可以对2维度np数组:
kpoint = np.fliplr(kpoint)
模板例子:
if self.args['scale_aug'] == True and random.random() > (1 - self.args['scale_p']):
self.rate = random.choice([0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3]) # 缩放倍数
width, height = img.size
width = int(width * self.rate)
height = int(height * self.rate)
img = img.resize((width, height), Image.ANTIALIAS)
crop_size_x = random.randint(0, img.shape[1] - width)
crop_size_y = random.randint(0, img.shape[2] - height)
sub_img = img[:, crop_size_x: crop_size_x + width, crop_size_y:crop_size_y + height]
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
import scipy.io as sio
# anno_path是标注文件的路径
data = sio.loadmat(anno_path)
# 标注点位置
xy = data['image_info'][0][0][0][0][0] # (n,2)格式数据
cnt = data['image_info'][0][0][0][0][1][0][0] # 人数
需要注意的是,这里的坐标格式是(h_idx,w_idx)
附上一个可视化查看标注位置是否对应正确的代码:
for xy in xys:
w_idx = int(xy[0])
h_idx = int(xy[1])
img[h_idx-5:h_idx+5,w_idx-5:w_idx+5,:] = [0,0,255]
plt.imshow(img)
plt.show()
import h5py
with h5py.File(h5_path,'w') as hf:
hf['key'] = data