File size: 2,534 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
60
61
62
63
64
65
66
import streamlit as st
import os
import shutil
from PIL import Image
import torch
import torchvision.transforms as transforms
from torchvision import models

# Set up dataset path
DATASET_PATH = "categorized_images"
os.makedirs(DATASET_PATH, exist_ok=True)

# Load class names dynamically from dataset folder
class_names = sorted(os.listdir(DATASET_PATH))  # Get categories from folder names
num_classes = len(class_names)

# Load the 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 transformation
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_and_save(image, filename):
    """Predict category and save the image in the correct folder."""
    image_tensor = transform(image).unsqueeze(0)
    
    with torch.no_grad():
        output = model(image_tensor)
        probabilities = torch.nn.functional.softmax(output, dim=1)
        predicted_index = torch.argmax(probabilities, dim=1).item()
    
    predicted_category = class_names[predicted_index]
    confidence = probabilities[0][predicted_index].item()
    
    # Ensure category folder exists
    category_path = os.path.join(DATASET_PATH, predicted_category)
    os.makedirs(category_path, exist_ok=True)
    
    # Save image in the correct category folder
    image_save_path = os.path.join(category_path, filename)
    image.save(image_save_path)
    
    return predicted_category, confidence, image_save_path

# Streamlit UI
st.title("πŸ“‚ Smart Image Categorizer")
st.write("Upload your images and let AI categorize them instantly!")

uploaded_files = st.file_uploader("Upload images (single or multiple)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)

if uploaded_files:
    for uploaded_file in uploaded_files:
        image = Image.open(uploaded_file).convert("RGB")
        category, confidence, saved_path = predict_and_save(image, uploaded_file.name)
        
        st.image(image, caption=f"{uploaded_file.name} β†’ {category} ({confidence:.2%})", use_column_width=True)
        st.success(f"βœ… Categorized as: **{category}** (Confidence: {confidence:.2%})")
        st.info(f"πŸ“‚ Image saved to: {saved_path}")