I have a python script which sorts image into folder.  Unfortunately, the result is really bad.  Can you help me improve it?

import os
import shutil
import numpy as np
import tkinter as tk
from tkinter import filedialog, messagebox
from sklearn.cluster import DBSCAN
from sklearn.decomposition import PCA
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing import image
from keras.models import Model

def choose_folder():
    """Open a folder selection dialog and return the path."""
    return filedialog.askdirectory()

def extract_vgg16_features(img_path, model):
    """Extract VGG16 features from an image."""
    img = image.load_img(img_path, target_size=(224, 224))
    img_data = image.img_to_array(img)
    img_data = np.expand_dims(img_data, axis=0)
    img_data = preprocess_input(img_data)
    vgg16_feature = model.predict(img_data)
    return vgg16_feature.flatten()

def sort_images_by_likeness():
    """Cluster images without specifying the number of clusters using DBSCAN."""
    source_folder = source_var.get()

    if not source_folder:
        messagebox.showwarning("Incomplete Information", "Please select the source folder path.")
        return

    try:
        # Load pre-trained VGG16 model + higher level layers (Excluding final fully connected layers)
        base_model = VGG16(weights='imagenet', include_top=False, pooling='avg')
        model = Model(inputs=base_model.input, outputs=base_model.output)

        # List image files
        image_files = [f for f in os.listdir(source_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]

        feature_vectors = []
        valid_image_files = []

        for image_file in image_files:
            image_path = os.path.join(source_folder, image_file)
            features = extract_vgg16_features(image_path, model)
            feature_vectors.append(features)
            valid_image_files.append(image_file)

        # Dimensionality Reduction for efficient clustering
        pca = PCA(n_components=50)  # Reduce to 50 dimensions
        feature_vectors = pca.fit_transform(feature_vectors)

        # Perform DBSCAN clustering with adjusted parameters
        dbscan = DBSCAN(eps=0.3, min_samples=2)
        labels = dbscan.fit_predict(feature_vectors)

        unique_labels = set(labels)
        for label in unique_labels:
            cluster_folder = os.path.join(source_folder, f"cluster_{label}")
            os.makedirs(cluster_folder, exist_ok=True)

        for index, image_file in enumerate(valid_image_files):
            cluster_index = labels[index]
            source_path = os.path.join(source_folder, image_file)
            dest_path = os.path.join(source_folder, f"cluster_{cluster_index}", image_file)
            shutil.move(source_path, dest_path)

        messagebox.showinfo("Success", "Images have been sorted into similarity groups successfully!")
    except Exception as e:
        messagebox.showerror("Error", f"An error occurred: {str(e)}")

# Initialize GUI
root = tk.Tk()
root.title("Image Sorter by Likeness")

source_var = tk.StringVar()

# Create GUI elements
tk.Label(root, text="Select Source Folder:").pack(pady=5)
tk.Button(root, text="Browse", command=lambda: source_var.set(choose_folder())).pack(pady=5)
tk.Label(root, textvariable=source_var).pack(pady=5)

tk.Button(root, text="Sort Images by Likeness", command=sort_images_by_likeness).pack(pady=20)

# Execute the Tkinter main loop
root.mainloop()