set up the paths for your image dataset directory and to store features directory .npy to avoide feature extraction every time you give input
pretrained vgg16 architecture model for Image retrieval
Run by entering
(streamlit run updatedpretrained.py)
The given code is a Streamlit app that demonstrates an image retrieval system using a pre-trained VGG16 model. The app allows users to upload an image and find similar images within a dataset based on cosine similarity of VGG16 features.
Imports:
The required libraries are imported: Streamlit for creating the app interface, NumPy for numerical operations, PIL (Python Imaging Library) for working with images, TensorFlow's Keras for deep learning, and Scipy for mathematical functions. Load VGG16 Model:
The pre-trained VGG16 model is loaded with weights from the ImageNet dataset. The include_top=False argument means the fully connected layers are excluded. Define Image Directory:
The img_dir variable specifies the directory where the images are located. Files in the directory are listed and filtered to include only image files with specific extensions (.jpg, .jpeg, .png, .bmp). Process Features for Images:
Features are computed for each image in the dataset using the VGG16 model. Features are either loaded from a saved file or computed and saved if they don't exist. Streamlit App Setup:
The Streamlit app is initialized with a title and introductory text. Image Upload Widget:
An image upload widget is added, allowing users to upload an image file (with a .jpg extension). Handle Image Upload and Retrieval:
If an image is uploaded: The query image is loaded, resized to 224x224 pixels, and its features are extracted using the VGG16 model. Cosine similarity scores between the query image and database images are calculated. The top-k most similar images are retrieved based on the highest similarity scores. If similar images are found, the query image and top-k similar images are displayed. Otherwise, a message is shown indicating no similar images were found. This Streamlit app provides a user-friendly interface for uploading an image and finding visually similar images within a given dataset. The VGG16 model is used to extract image features, and cosine similarity is employed to measure image similarity. The app leverages Streamlit's interactive features to create a simple yet effective image retrieval tool.