-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdezeenAI.py
311 lines (251 loc) · 9.13 KB
/
dezeenAI.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# import essential libraries
import pandas as pd
import sys
import numpy as np
import matplotlib.pyplot as plt
import streamlit as st
import SessionState
# don't truncate long values
pd.set_option('display.max_colwidth', None)
# Use the full page instead of a narrow central column
st.set_page_config(layout='wide')
#
# LOAD DATAFRAMES
#
articles_df = pd.read_csv('files/df_articles.csv', index_col=0)
colors_df = pd.read_pickle('files/df_images-colours.pkl')
objects_df = pd.read_csv('files/images_objectsbasic.csv', index_col=0)
#
# CONVERT HEX TO RGB
#
def hex2rgb(color):
h = color.lstrip('#')
color = tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
return color
#
# LIST OF CLASSES
#
classes = [
'dog',
'person',
'bicycle',
'car',
'bench',
'cat',
'sports ball',
'skateboard',
'bottle',
'wine glass',
'cup',
'fork',
'knife',
'spoon',
'bowl',
'chair',
'sofa',
'pottedplant',
'bed',
'diningtable',
'toilet',
'tvmonitor',
'laptop',
'keyboard',
'microwave',
'oven',
'sink',
'refrigerator',
'book',
'clock',
'vase',
]
#
# SIDEBAR
#
st.sidebar.header('What are you looking for?')
search_type = st.sidebar.selectbox('',['Choose one...','colors', 'objects'])
# color recognition controls
if search_type == 'colors':
st.sidebar.header('Which color are you looking for?')
selected_color = st.sidebar.color_picker('', '#F9BB2A', key='selected')
selected_color = hex2rgb(selected_color)
# object recognition controls
if search_type == 'objects':
st.sidebar.header('Which object are you looking for?')
selected_class = st.sidebar.selectbox('', classes)
st.sidebar.header('What is relevant to you?')
sort_type = st.sidebar.radio('', ['confidence', 'quantity'])
# search image grid control
st.sidebar.header('Customize the search results')
number_of_columns = st.sidebar.slider('Columns', min_value=1, max_value=5, value=3, step=1)
number_of_elements = st.sidebar.slider('Elements per page', min_value=1, max_value=24, value=12, step=1)
# implement per-session persistent state for pagination
ss = SessionState.get(page_number=0)
# pagination
st.sidebar.header('Looking for more?')
prev = st.sidebar.button('Previous page')
next = st.sidebar.button('Next page')
# increment pagination
if prev and ss.page_number > 0:
ss.page_number -= 1
elif next:
ss.page_number += 1
#
# HEADER AREA
#
st.title('DezeenAI - Color & object search engine')
st.header('''This app lets you search interior projects on Dezeen.com based on colors or objects in the pictures.''')
st.subheader('[DezeenAI GitHub repository](https://github.com/adamsiemaszkiewicz/dezeenAI)')
#
# COLOR RECOGNITION FUNCTIONS
#
def rgb2hsv(color):
'''
Takes a color in RGB format and converts it HSV format.
Parameters:
color (list): Color in the RGB format as a list of R, G, B values
Returns:
A converted color in HSV format.
'''
r, g, b = color[0]/255.0, color[1]/255.0, color[2]/255.0
mx = max(r, g, b)
mn = min(r, g, b)
df = mx-mn
if mx == mn:
h = 0
elif mx == r:
h = (60 * ((g-b)/df) + 360) % 360
elif mx == g:
h = (60 * ((b-r)/df) + 120) % 360
elif mx == b:
h = (60 * ((r-g)/df) + 240) % 360
if mx == 0:
s = 0
else:
s = (df/mx)*100
v = mx*100
color = [h, s, v]
return color
def closestColor(reference_color, list_of_colors):
'''
Takes a reference color and finds a closest color withing a given color list.
Parameters:
reference_color (list): Reference color in a RGB format
list_of_colors (list): List of colors in a RGB format
Returns:
A color within a list the closest to the reference color and distance between them.
'''
# initiate the distance to be a really big number and closest_color as empty
shortest_distance, closest_color = sys.maxsize, None
# check if the color container is not NaN
if isinstance(list_of_colors, np.ndarray):
# iterate through all the colors
for color in list_of_colors:
# calculate the Euclidean distance to the reference color (sum of squared distances of each value)
current_distance = pow(rgb2hsv(color)[0] - rgb2hsv(reference_color)[0], 2) + \
pow(rgb2hsv(color)[1] - rgb2hsv(reference_color)[1], 2) + \
pow(rgb2hsv(color)[2] - rgb2hsv(reference_color)[2], 2)
# update the distance along with the corresponding color
if current_distance < shortest_distance:
shortest_distance = current_distance
closest_color = color
return closest_color, shortest_distance
@st.cache(show_spinner=False) # cache this function for better performance
def SortColors(color):
'''
Takes the colors_df DataFrame, finds the color distance between the selected
color and the color palette of the picture and sorts the DataFrame accordingly.
'''
# find color distance for each image in the dataset and sort the DataFrame
colors_df_search = colors_df.copy()
colors_df_search['distance'] = colors_df.apply(lambda x: closestColor(color, x.loc['colours'])[1], axis=1)
colors_df_search.sort_values(['distance'], inplace=True)
return colors_df_search
def ColorGrid(ref_col, elements, columns, page_number=0):
'''
Takes the color, finds pictures containing it and outputs search results as a image grid.
Parameters:
ref_col (list): Color in RGB formatting
output (tuple of integers): size of the image grid (rows, columns)
Returns:
An image grid with color search results
'''
st.write(f'''---
## Search results (page {page_number+1})''')
# sort DataFrame
df = SortColors(ref_col)
# fetch lists of image paths and article ids for search results
offset = page_number*elements
urls = df['url'][offset:offset+elements]
ids = df['id'][offset:offset+elements]
# calculate number of rows to generate
if elements % columns == 0:
rows = int(elements/columns)
else:
rows = int(elements/columns) + 1
# fill cells with search results
cols = st.beta_columns(columns)
for r in range(rows):
for c in range(columns):
ordinal = r*columns+c
if ordinal < elements:
img = urls.iloc[ordinal]
id = ids.iloc[ordinal]
title = articles_df[articles_df['id'] == id]['title'].to_string(index=False)
hyperlink = articles_df[articles_df['id'] == id]['url'].to_string(index=False)
cols[c].subheader(title)
cols[c].write(f'[Read article...]({hyperlink})')
cols[c].image(img, use_column_width=True)
cols[c].markdown('---')
def ObjectGrid(class_name, sort, elements, columns, page_number=0):
'''
Takes the class name to find together with search parameters and outputs
search results as a images grid.
Parameters:
class_name (str): Name of the class to find
sort (str): sorting method
`quantity` sort according to quantity of objects found (default)
`confidence` sort according to find confidence
output (tuple of integers): size of the image grid (rows, columns)
Returns:
An image grid with object search results
'''
st.write(f'''---
## Search results (page {page_number+1})''')
# sort according to specified method
if sort == 'quantity':
df_search = objects_df.sort_values([class_name, class_name+'_conf'], ascending=False)
elif sort == 'confidence':
df_search = objects_df.sort_values([class_name+'_conf'], ascending=False)
# fetch lists of image paths and article ids for search results
offset = page_number*elements
urls = df_search['url'][offset:offset+elements]
ids = df_search['id'][offset:offset+elements]
if elements % columns == 0:
rows = int(elements/columns)
else:
rows = int(elements/columns) + 1
# fill cells with search results
cols = st.beta_columns(columns)
for r in range(rows):
for c in range(columns):
ordinal = r*columns+c
# my_bar = st.progress(ordinal)
if ordinal < elements:
img = urls.iloc[ordinal]
id = ids.iloc[ordinal]
title = articles_df[articles_df['id'] == id]['title'].to_string(index=False)
hyperlink = articles_df[articles_df['id'] == id]['url'].to_string(index=False)
cols[c].subheader(title)
cols[c].write(f'[Read article...]({hyperlink})')
cols[c].image(img, use_column_width=True)
cols[c].markdown('---')
#
# DISPLAY SEARCH RESULTS
#
with st.spinner('Please wait. Loading search results...'):
if search_type == 'colors':
ColorGrid(selected_color, number_of_elements, number_of_columns, ss.page_number)
elif search_type == 'objects':
ObjectGrid(selected_class, sort_type, number_of_elements, number_of_columns, ss.page_number)
elif search_type == 'Choose one...':
st.info('Please specify your search criteria in the left sidebar.')