Type Here to Get Search Results !

Image Increase Code(Augmentation)

# ============================== # INSTALL # ============================== !pip install -q opencv-python tqdm # ============================== # MOUNT DRIVE # ============================== from google.colab import drive drive.mount('/content/drive') # ============================== # IMPORTS # ============================== import os import cv2 import numpy as np import random from tqdm import tqdm # ============================== # INPUT (CLASS PATH ONLY) # ============================== class_path = input("📂 Enter CLASS folder path: ").strip() output_path = input("📁 Enter OUTPUT folder path: ").strip() TARGET = int(input("🎯 Enter target number of images: ")) if not os.path.exists(class_path): raise Exception("❌ Class path not found!") os.makedirs(output_path, exist_ok=True) # ============================== # LOAD IMAGES # ============================== files = [f for f in os.listdir(class_path) if f.lower().endswith(('.jpg','.jpeg','.png'))] if len(files) == 0: raise Exception("❌ No images found in this class!") print(f"\n📊 Found {len(files)} images") # ============================== # AUGMENT FUNCTIONS # ============================== def zoom_image(img): h, w = img.shape[:2] zoom = random.uniform(1.1, 1.3) new_h, new_w = int(h / zoom), int(w / zoom) if new_h <= 0 or new_w <= 0: return img y = random.randint(0, h - new_h) x = random.randint(0, w - new_w) crop = img[y:y+new_h, x:x+new_w] return cv2.resize(crop, (w, h)) def brightness(img): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) val = random.randint(-15, 15) # FIX overflow v = hsv[:, :, 2].astype(np.int16) v = np.clip(v + val, 0, 255) hsv[:, :, 2] = v.astype(np.uint8) return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) # ============================== # SAVE ORIGINALS # ============================== for f in files: img = cv2.imread(os.path.join(class_path, f)) if img is None: continue cv2.imwrite(os.path.join(output_path, f), img) # ============================== # AUGMENTATION (SMART RANDOM) # ============================== current = len(files) print(f"\n📊 Current: {current} → Target: {TARGET}") if current >= TARGET: print("✔ Already enough images, no augmentation needed") else: needed = TARGET - current print(f"🚀 Generating {needed} images...") i = 0 random.shuffle(files) while i < needed: # ✅ random selection (better diversity) file = random.choice(files) img = cv2.imread(os.path.join(class_path, file)) if img is None: continue aug = zoom_image(img) # slight brightness if random.random() > 0.5: aug = brightness(aug) # 🔥 extra variation (important) if random.random() > 0.7: aug = zoom_image(aug) new_name = f"{os.path.splitext(file)[0]}_aug_{i}.jpg" cv2.imwrite(os.path.join(output_path, new_name), aug) i += 1 print("\n🎉 DONE!") # ============================== # FINAL COUNT # ============================== final_count = len([f for f in os.listdir(output_path) if f.lower().endswith(('.jpg','.jpeg','.png'))]) print(f"\n📊 Final image count: {final_count}")

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.