paddle dataset

paddle实现图像旋转

import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from paddle.vision.transforms import  functional as F
import cv2

image=cv2.imread('./1.jpg')
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

# 图像旋转 opencv
# imgR90 = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
# # imgR90 = np.rot90(image, 1)  # numpy 矩阵旋转 90*1=90 度
# cv2.imshow('image',imgR90)
# cv2.waitKey()

# # 图像旋转 使用PIL
# img=Image.fromarray(image)
# img=img.rotate(180)
# img.show()
# img=np.array(img)
# plt.imshow(img)
# plt.show()
# cv2.imshow('img',img)  # opencv显示的格式是bgr 其他的都是rgb
# cv2.waitKey()

# 图像旋转 使用paddle
image=Image.fromarray(image)
rotate_img=F.rotate(image,90)
# rotate_img.show()
image=np.array(rotate_img)
plt.imshow(image)
plt.show()


paddle实现数据增强

import cv2
import numpy as np
from matplotlib import pyplot as plt
from paddle.vision import Compose,RandomHorizontalFlip,RandomCrop,Normalize,RandomRotation,RandomVerticalFlip,ColorJitter

transfrom=Compose([
    # Normalize(mean=[125],std=[125],data_format="HWC"),
    RandomHorizontalFlip(1),
    RandomRotation(180),
    RandomCrop(200),
    RandomVerticalFlip(0.5),
    ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0)  #图像的亮度、对比度、饱和度和色调

])
while(True):
    image = cv2.imread('1.jpg')
    # image=np.expand_dims(image,axis=2)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = transfrom(image)
    # image=np.clip(image,0,1)
    plt.imshow(image)
    plt.show()

dataset

import os
import cv2
import numpy as np
from paddle.io import Dataset
from paddle.vision.transforms import Normalize,Compose,RandomHorizontalFlip,ColorJitter,CenterCrop,RandomVerticalFlip,RandomRotation
from matplotlib import pyplot as plt

class MyDataset(Dataset):
    def __init__(self,data_dir,data_txt,transform=None):
        super().__init__()
        self.data_list=[]
        self.data_dir=data_dir
        with open(data_txt,encoding='utf-8') as f:
            for line in f.readlines():
                image_path=line.strip()
                self.data_list.append(image_path)
        self.transform=transform
    def __getitem__(self, index):
        index=index%len(self.data_list)
        image_path=self.data_list[index]
        image_path=os.path.join(self.data_dir,image_path)
        image=cv2.imread(image_path)
        image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

        # 飞桨训练格式默认为float32
        image=image.astype('float32')
        if self.transform is not None:
            # print('h')
            image=self.transform(image)
        return image
    def __len__(self):
        return len(self.data_list)

# 定义数据预处理方法
transform=Compose(
    [
        RandomHorizontalFlip(1),
        RandomVerticalFlip(1),
        RandomRotation(360)
        # Normalize(mean=[125],std=[125],data_format='CHW'),
        # ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5),
        # CenterCrop(100)
    ]
)

dataset=MyDataset(data_dir='./fruit-detection/JPEGImages',data_txt='./fruit-detection/ImageSets/Main/train.txt',transform=transform)
image=dataset[1]
# image=np.clip(image,0,1)
print(type(image))
plt.imshow(image.astype(np.uint8))
plt.show()


你可能感兴趣的:(paddle,paddle,opencv,人工智能)