Created
March 12, 2025 18:48
-
-
Save samuellangajr/6d53c86dd04362e39faa59ed45ae71e4 to your computer and use it in GitHub Desktop.
Escreva uma função que realize data augmentation em uma imagem de tráfego. Inclua pelo menos três técnicas diferentes de augmentation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torchvision import transforms | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
# Função para realizar data augmentation | |
def augmentacao_imagem(imagem): | |
# Definindo uma sequência de transformações | |
transformacao = transforms.Compose([ | |
transforms.RandomRotation(degrees=30), # Rotaciona a imagem até 30 graus | |
transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5), # Altera brilho, contraste, saturação e tonalidade | |
transforms.RandomResizedCrop(size=(224, 224), scale=(0.8, 1.0)), # Zoom aleatório na imagem | |
transforms.ToTensor() # Converte a imagem para tensor | |
]) | |
# Aplicando as transformações na imagem | |
imagem_transformada = transformacao(imagem) | |
return imagem_transformada | |
# Exemplo de uso | |
imagem_path = 'caminho/para/sua/imagem_de_trafego.jpg' # Substitua pelo caminho real da imagem | |
imagem = Image.open(imagem_path) | |
# Aplicando a augmentação | |
imagem_aug = augmentacao_imagem(imagem) | |
# Exibindo a imagem original e a imagem transformada | |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6)) | |
# Exibir imagem original | |
ax1.imshow(imagem) | |
ax1.set_title("Imagem Original") | |
ax1.axis('off') | |
# Exibir imagem transformada | |
ax2.imshow(imagem_aug.permute(1, 2, 0)) # Para exibir corretamente o tensor | |
ax2.set_title("Imagem com Data Augmentation") | |
ax2.axis('off') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment