paddle实现图像旋转
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from paddle.vision.transforms import functional as F
import cv2image=cv2.imread('./1.jpg')
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
image=Image.fromarray(image)
rotate_img=F.rotate(image,90)
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,ColorJittertransfrom=Compose([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 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)image = transfrom(image)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 pltclass MyDataset(Dataset):def __init__(self,data_dir,data_txt,transform=None):super().__init__()self.data_list=[]self.data_dir=data_dirwith 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=transformdef __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)image=image.astype('float32')if self.transform is not None:image=self.transform(image)return imagedef __len__(self):return len(self.data_list)
transform=Compose([RandomHorizontalFlip(1),RandomVerticalFlip(1),RandomRotation(360)]
)dataset=MyDataset(data_dir='./fruit-detection/JPEGImages',data_txt='./fruit-detection/ImageSets/Main/train.txt',transform=transform)
image=dataset[1]
print(type(image))
plt.imshow(image.astype(np.uint8))
plt.show()