File size: 6,506 Bytes
c456f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf8b3f
c456f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf8b3f
c456f09
 
 
faf8b3f
c456f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf8b3f
 
c456f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import streamlit as st
import torch
import os
import torchvision
from annoy import AnnoyIndex
from PIL import Image
import traceback
from tqdm import tqdm
from PIL import ImageFile
from slugify import slugify
import opendatasets as od
import json

ImageFile.LOAD_TRUNCATED_IMAGES = True
FOLDER = "images/"
NUM_TREES = 100
FEATURES = 1000
FILETYPES = [".png", ".jpg", ".jpeg", ".tiff", ".bmp"]


@st.cache_resource
def load_dataset():
    with open("kaggle.json", "w+") as f:
        json.dump(
            {
                "username": st.secrets["username"],
                "key": st.secrets["key"],
            },
            f,
        )
    od.download(
        "https://www.kaggle.com/datasets/kkhandekar/image-dataset",
        "images/",
    )


# Load a pre-trained image feature extractor model
@st.cache_resource
def load_model():
    """Loads a pre-trained image feature extractor model."""
    model = torch.hub.load(
        "NVIDIA/DeepLearningExamples:torchhub",
        "nvidia_efficientnet_b0",
        pretrained=True,
    )
    model.eval()  # Set model to evaluation mode
    return model


# Get all file paths within a folder and its subfolders
@st.cache_data
def get_all_file_paths(folder_path):
    """Returns a list of all file paths within a folder and its subfolders."""
    file_paths = []
    for root, _, files in os.walk(folder_path):
        for file in files:
            if not file.lower().endswith(tuple(FILETYPES)):
                continue
            file_path = os.path.join(root, file)
            file_paths.append(file_path)
    print(f"Total {len(file_paths)} image files present")
    return file_paths


# Load all the images from file paths
@st.cache_data
def load_images(file_paths):
    """Load all the images from file paths."""
    print("Loading images: ")
    images = list()
    for path in tqdm(file_paths):
        try:
            images.append(Image.open(path).resize([224, 224]))
        except BaseException as e:
            print("error loading ", path, e)
    return images


# Function to preprocess images
def preprocess_image(image):
    """Preprocesses an image for feature extraction."""
    if image.mode == "RGB":  # Already has 3 channels
        pass  # No need to modify
    elif image.mode == "L":  # Grayscale image
        image = image.convert("RGB")  # Convert to 3-channel RGB
    else:  # Image has more than 3 channels
        image = image.convert(
            "RGB"
        )  # Convert to 3-channel RGB, discarding extra channels
    preprocess = torchvision.transforms.Compose(
        [
            # torchvision.transforms.Resize(224),  # Adjust for EfficientNet input size
            torchvision.transforms.CenterCrop(224),
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize(
                mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
            ),
        ]
    )
    return preprocess(image)


# Extract features from a list of images
def extract_features(images, model):
    """Extracts features from a list of images."""
    print("Extracting features:")
    features = []
    for image in images:
        with torch.no_grad():
            feature = model(preprocess_image(image).unsqueeze(0)).squeeze(0)
            features.append(feature.numpy())
    return features


# Build an Annoy index for efficient similarity search
def build_annoy_index(features):
    """Builds an Annoy index for efficient similarity search."""
    print("Building annoy index:")
    f = features[0].shape[0]  # Feature dimensionality
    t = AnnoyIndex(f, "angular")  # Use angular distance for image features
    for i, feature in tqdm(enumerate(features)):
        t.add_item(i, feature)
    t.build(NUM_TREES)  # Adjust num_trees for accuracy vs. speed trade-off
    return t


# Perform reverse image search
def search_similar_images(uploaded_file, f=FEATURES, num_results=5):
    """Finds similar images based on a query image feature."""
    index = AnnoyIndex(f, "angular")
    index.load(f"{slugify(FOLDER)}.tree")
    query_image = Image.open(uploaded_file)
    model = load_model()
    # Extract features and search
    query_feature = (
        model(preprocess_image(query_image).unsqueeze(0)).squeeze(0).detach().numpy()
    )
    nearest_neighbors, distances = index.get_nns_by_vector(
        query_feature, num_results, include_distances=True
    )
    return query_image, nearest_neighbors, distances


@st.cache_data
def save_embedding(folder=FOLDER):
    if os.path.isfile(f"{slugify(FOLDER)}.tree"):
        return
    model = load_model()  # Load the model once
    file_paths = get_all_file_paths(folder_path=folder)
    images = load_images(file_paths)
    features = extract_features(images, model)
    index = build_annoy_index(features)
    index.save(f"{slugify(FOLDER)}.tree")


def display_image(idx, dist):
    file_paths = get_all_file_paths(folder_path=FOLDER)
    image = Image.open(file_paths[idx])
    st.image(image.resize([256, 256]))
    st.markdown("SimScore: -" + str(round(dist, 2)))
    # st.markdown(file_paths[idx])


if __name__ == "__main__":
    # Main app logic
    st.set_page_config(layout="wide")
    st.title("Reverse Image Search App")

    try:
        load_dataset()
        save_embedding(FOLDER)

        # File uploader
        uploaded_file = st.file_uploader(
            "Choose an image like a car, cat, dog, flower, fruits, bike, aeroplane, person",
            type=FILETYPES,
        )

        n_matches = st.slider(
            "Num of matches to be displayed", min_value=3, max_value=100, value=5
        )

        if uploaded_file is not None:
            query_image, nearest_neighbors, distances = search_similar_images(
                uploaded_file, num_results=n_matches
            )

            st.image(query_image.resize([256, 256]), caption="Query Image", width=200)
            st.subheader("Similar Images:")
            cols = st.columns([1] * 5)
            for i, (idx, dist) in enumerate(
                zip(
                    *[
                        nearest_neighbors,
                        distances,
                    ]
                )
            ):
                with cols[i % 5]:
                    # Display results
                    display_image(idx, dist)
        else:
            st.write("Please upload an image to start searching.")

    except Exception as e:
        traceback.print_exc()
        print(e)
        st.error("An error occurred: {}".format(e))