File size: 2,088 Bytes
c3d8a68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import torch
import torchvision.transforms as transforms
from torchvision import models
from PIL import Image

# Load class names dynamically
dataset_path = "categorized_images"
class_names = sorted(os.listdir(dataset_path))  # Get categories from folder names
num_classes = len(class_names)

# Load trained model
model = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.IMAGENET1K_V1)
model.classifier[1] = torch.nn.Linear(1280, num_classes)
model.load_state_dict(torch.load("custom_image_model.pth", map_location=torch.device("cpu")))
model.eval()

# Define image transformations
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

def predict_category(image_path):
    """Predicts the category of a single image."""
    image = Image.open(image_path).convert("RGB")
    image = transform(image).unsqueeze(0)
    
    with torch.no_grad():
        output = model(image)
        probabilities = torch.nn.functional.softmax(output, dim=1)
        predicted_index = torch.argmax(probabilities, dim=1).item()
    
    return class_names[predicted_index]

def categorize_images(image_folder="uncategorized_images", output_folder="categorized_images"):
    """Categorizes all images in a folder."""
    if not os.path.exists(image_folder):
        print("❌ Image folder not found!")
        return

    for img_name in os.listdir(image_folder):
        img_path = os.path.join(image_folder, img_name)
        if not os.path.isfile(img_path):
            continue
        
        category = predict_category(img_path)
        category_folder = os.path.join(output_folder, category)
        os.makedirs(category_folder, exist_ok=True)
        
        new_path = os.path.join(category_folder, img_name)
        os.rename(img_path, new_path)
        print(f"βœ… Moved {img_name} to {category}/")

if __name__ == "__main__":
    categorize_images()
    print("βœ… Categorization complete!")