File size: 4,760 Bytes
8e8077f
2f993c5
 
8e8077f
 
50db5b5
d1ec505
2f993c5
8e8077f
 
ace8bdf
 
 
 
8e8077f
 
ace8bdf
 
8e8077f
 
bc4fce7
 
 
 
 
 
 
 
 
8e8077f
 
 
 
 
 
 
 
bc4fce7
8e8077f
 
 
 
2f993c5
8e8077f
2f993c5
8e8077f
 
9d40d12
9205b70
8e8077f
 
 
 
ed3c2dc
8e8077f
 
 
 
ed3c2dc
8e8077f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e190b5
 
8e8077f
 
 
 
5e190b5
8e8077f
 
5e190b5
8e8077f
 
 
3c7e1b4
 
8e8077f
3c7e1b4
 
 
5e190b5
2cbc400
 
 
 
 
5e190b5
3c7e1b4
 
5e190b5
3c7e1b4
 
5e190b5
db5890b
2cbc400
3c7e1b4
 
 
5e190b5
3c7e1b4
5e190b5
3c7e1b4
5e190b5
3c7e1b4
 
 
 
 
0560cab
3c7e1b4
 
 
 
 
 
 
 
 
 
 
 
6b62f41
3c7e1b4
 
 
 
5e190b5
 
 
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
import streamlit as st
import numpy as np
import similarSearch
import pandas as pd
import cv2
import os
from PIL import Image

df = pd.read_csv('./gallery.csv')

def read_image(uploaded_file):
    with open(uploaded_file.name, "wb") as f:
        f.write(uploaded_file.getbuffer())
    img = cv2.imread(uploaded_file.name, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    if img is None:
        raise ValueError('Failed to read {}'.format(uploaded_file.name))
    os.remove(uploaded_file.name)  # Remove the temporary file
    return img

def image_open(image_file):
    img = cv2.imread(
        image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION
    )
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    if img is None:
        raise ValueError('Failed to read {}'.format(image_file))
    return img

    
# Function to display images from their paths
def display_images(image_ids):
    images = []
    captions = []
    for img_id in image_ids:
        image_path = df.loc[df['seller_img_id'] == img_id, 'img_path'].values[0]
        print(image_path)
        image = image_open(image_path)
        images.append(image)
        captions.append(img_id)
    return images, captions
    
# Function to find similar image IDs for a newly updated image
def find_similar_images(image):
    # Placeholder for your code to find similar image IDs based on the new_image
    similar_ids = similarSearch.predict(image)  # Replace with the actual logic for finding similar IDs
    images, captions = display_images(similar_ids)
    length = len(images)
    return similar_ids,length
    # return similar_ids

def main():
    st.set_page_config(
    page_title='See it, search it',
    page_icon=':mag:',
    layout='wide',
    initial_sidebar_state='expanded'
    )
    st.title('See it, search it')
    st.markdown(
    """
    <style>
    .stTitle {
        text-align: center;
        font-size: 36px;
        margin-bottom: 30px;
    }
    .stRadio > div {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .stImageContainer {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 30px;
    }
    .stImage {
        max-width: 100%;
        height: auto;
    }
    </style>
    """,
    unsafe_allow_html=True
    )

    

    image_columns = st.columns(5)

    # Define the desired width and height for the resized images
    image_width = 150
    image_height = 150

    # Calculate the number of columns based on the available width
    num_columns = 5

    # Calculate the number of rows required
    images_per_page = 50

    # Check if the image is uploaded by the user
    uploaded_image = st.file_uploader("Upload Image", type=['png', 'jpg', 'jpeg'])

    if uploaded_image is not None:
        # Convert the uploaded image to an array using the read_image function
        image = read_image(uploaded_image)

        image = Image.fromarray(image)
        image = image.resize((224, 224))

        # Display the resized image
        st.image(image, caption="Query Image", width=224)

        # Find similar images
        similar_images, length = find_similar_images(image)

        # Calculate the total number of pages required
        total_pages = (length + images_per_page - 1) // images_per_page

        page_number = st.sidebar.number_input("Select Page", min_value=1, max_value=total_pages)

        # Calculate the range of images to display for the selected page
        start_index = (page_number - 1) * images_per_page
        end_index = min(start_index + images_per_page, length)

        image_columns = st.columns(num_columns)

        index = 0 

        for img_id in similar_images[start_index:end_index]:
            row = df[df['seller_img_id'] == img_id]
            if not row.empty:
                img_path = row['img_path'].values[0]
                product_id = row['product_id'].values[0]
        
                # Construct the full path to the image
                full_img_path = img_path

                # Open and resize the image to the desired dimensions
                image = image_open(full_img_path)
                resized_image = cv2.resize(image, (image_width, image_height))

                # Determine the column to place the image in
                col_index = index % num_columns
                with image_columns[col_index]:
                    st.image(resized_image, caption=f'Product ID: {product_id}\nImage ID: {img_id}', width=image_width)
                index += 1
                    
            else:
                st.write(f"Could not find details for image with ID: {img_id}")
        


if __name__ == '__main__':
    main()