Pairavi commited on
Commit
8e8077f
·
1 Parent(s): 759b09e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -17
app.py CHANGED
@@ -1,22 +1,132 @@
1
- import gradio as gr
2
  import numpy as np
3
  import similarSearch
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  # Function to find similar image IDs for a newly updated image
6
- def find_similar_images(new_image):
7
  # Placeholder for your code to find similar image IDs based on the new_image
8
- similar_ids = similarSearch.predict(new_image) # Replace with the actual logic for finding similar IDs
9
- print(similar_ids)
10
- return similar_ids
11
-
12
- # Define Gradio interface
13
- iface = gr.Interface(
14
- fn=find_similar_images,
15
- inputs=gr.inputs.Image(label="Upload Image"),
16
- outputs=gr.outputs.Textbox(label="Similar Image IDs"),
17
- title="Image Similarity Finder",
18
- description="Upload an image to find similar image IDs in the database."
19
- )
20
-
21
- # Launch the Gradio interface
22
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
  import numpy as np
3
  import similarSearch
4
+ import pandas as pd
5
+ import cv2
6
 
7
+ df = pd.read_csv('./gallery.csv')
8
+
9
+ def read_image(image_file):
10
+ img = cv2.imread(
11
+ image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION
12
+ )
13
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
14
+ if img is None:
15
+ raise ValueError('Failed to read {}'.format(image_file))
16
+ return img
17
+
18
+
19
+ # Function to display images from their paths
20
+ def display_images(image_ids):
21
+ images = []
22
+ captions = []
23
+ for img_id in image_ids:
24
+ image_path = df.loc[df['seller_img_id'] == img_id, 'img_path'].values[0]
25
+ print(image_path)
26
+ image = read_image(image_path)
27
+ images.append(image)
28
+ captions.append(img_id)
29
+ return images, captions
30
+
31
  # Function to find similar image IDs for a newly updated image
32
+ def find_similar_images(image):
33
  # Placeholder for your code to find similar image IDs based on the new_image
34
+ similar_ids = similarSearch.predict(image) # Replace with the actual logic for finding similar IDs
35
+ images, captions = display_images(similar_ids)
36
+ length = images.length
37
+ return images,length
38
+ # return similar_ids
39
+
40
+ def main():
41
+ st.set_page_config(
42
+ page_title='Similar Image',
43
+ page_icon=':mag:',
44
+ layout='wide',
45
+ initial_sidebar_state='expanded'
46
+ )
47
+ st.title('Similar Image')
48
+ st.markdown(
49
+ """
50
+ <style>
51
+ .stTitle {
52
+ text-align: center;
53
+ font-size: 36px;
54
+ margin-bottom: 30px;
55
+ }
56
+ .stRadio > div {
57
+ display: flex;
58
+ justify-content: center;
59
+ align-items: center;
60
+ }
61
+ .stImageContainer {
62
+ display: flex;
63
+ justify-content: center;
64
+ align-items: center;
65
+ margin-bottom: 30px;
66
+ }
67
+ .stImage {
68
+ max-width: 100%;
69
+ height: auto;
70
+ }
71
+ </style>
72
+ """,
73
+ unsafe_allow_html=True
74
+ )
75
+
76
+ uploaded_image = st.file_uploader("Upload Image", type=['png', 'jpg', 'jpeg'])
77
+
78
+ # Check if the image is uploaded by the user
79
+ if uploaded_image is not None:
80
+ # Convert the uploaded image to an array using the read_image function
81
+ image = read_image(uploaded_image)
82
+
83
+ # Display the uploaded image
84
+ st.image(image, caption='Uploaded Image', use_column_width=True)
85
+
86
+ # Find similar images
87
+ similar_images, length = find_similar_images(image)
88
+
89
+ # Define the desired width and height for the resized images
90
+ image_width = 150
91
+ image_height = 150
92
+
93
+ # Calculate the number of columns based on the available width
94
+ num_columns = 5
95
+
96
+ # Calculate the number of rows required
97
+ images_per_page = 50
98
+ num_rows = (images_per_page + num_columns - 1) // num_columns
99
+
100
+ # Calculate the total number of pages required
101
+ total_pages = (length + images_per_page - 1) // images_per_page
102
+
103
+ # Create a row for the page selector and images
104
+ col1, col2 = st.columns([2, 3])
105
+
106
+ # Page selector
107
+ with col1:
108
+ tabs = st.radio(" ", range(1, total_pages + 1))
109
+
110
+ # Calculate the range of images to display for the selected page
111
+ start_index = (tabs - 1) * images_per_page
112
+ end_index = min(start_index + images_per_page, len(csv_data))
113
+
114
+
115
+
116
+ # Loop through each row in the CSV data for the selected page
117
+ for index in range(start_index, end_index):
118
+ img_id = csv_data.loc[index, 'seller_img_id']
119
+ img_path = csv_data.loc[index, 'img_path']
120
+ product_id = csv_data.loc[index, 'product_id']
121
+
122
+ # Construct the full path to the image
123
+ full_img_path = gallery_folder_path + img_path
124
+
125
+ # Open and resize the image to the desired dimensions
126
+ image = Image.open(full_img_path)
127
+ resized_image = image.resize((image_width, image_height))
128
+
129
+ # Determine the column to place the image in
130
+ col_index = index % num_columns
131
+ with image_columns[col_index]:
132
+ st.image(resized_image, caption=f'Product ID: {product_id}\nImage ID: {img_id}', width=image_width)