File size: 1,617 Bytes
b5b2f19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import cv2
import numpy as np
import albumentations as A
from glob import glob

# Define the augmentation pipeline
AUG = A.Compose([
    A.HorizontalFlip(p=0.5),
    A.RandomBrightnessContrast(p=0.5),
    A.GaussianBlur(blur_limit=3, p=0.3),
    A.Rotate(limit=15, p=0.3),
    A.RandomResizedCrop(160, 160, scale=(0.9, 1.0), p=0.3),
    A.ElasticTransform(alpha=1.0, sigma=50, alpha_affine=50, p=0.3),  # Elastic transformation
    A.CoarseDropout(max_holes=1, max_height=8, max_width=8, p=0.3),  # Random Erasing
    A.PerspectiveTransform(scale=(0.01, 0.1), p=0.5)  # Random perspective shift
])

# Directory with input images
INPUT_DIR = "data"
CATEGORIES = ["real", "deepfake", "ai_gen"]
valid_extensions = ['.jpg', '.jpeg', '.png']

for cat in CATEGORIES:
    os.makedirs(os.path.join(INPUT_DIR, cat, 'augmented'), exist_ok=True)  # Create an 'augmented' folder inside each category
    files = glob(f"{INPUT_DIR}/{cat}/*")
    
    for i, file in enumerate(files):
        # Skip non-image files
        if not any(file.lower().endswith(ext) for ext in valid_extensions):
            continue
        
        img = cv2.imread(file)
        if img is None:
            continue
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        
        # Generate 3 augmented images
        for j in range(3):  
            aug = AUG(image=img)["image"]
            save_path = os.path.join(INPUT_DIR, cat, 'augmented', f"aug_{i}_{j}.jpg")
            cv2.imwrite(save_path, cv2.cvtColor(aug, cv2.COLOR_RGB2BGR))

print("✅ Augmentation complete. You can now re-run feature extraction and model training.")