leedoming commited on
Commit
b757e27
β€’
1 Parent(s): ce67f34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -42
app.py CHANGED
@@ -124,29 +124,30 @@ def detect_clothing(image):
124
  def crop_image(image, bbox):
125
  return image.crop((bbox[0], bbox[1], bbox[2], bbox[3]))
126
 
 
 
 
 
 
 
 
 
 
 
127
  # Streamlit app
128
  st.title("Advanced Fashion Search App")
129
 
130
- search_type = st.radio("Search by:", ("Image URL", "Text"))
131
-
132
- if search_type == "Image URL":
133
- query_image_url = st.text_input("Enter image URL:")
134
  if st.button("Detect Clothing"):
135
- if query_image_url:
136
- query_image = load_image_from_url(query_image_url)
137
  if query_image is not None:
138
- st.image(query_image, caption="Query Image", use_column_width=True)
139
- detections = detect_clothing(query_image)
140
-
141
- if detections:
142
- st.session_state.detections = detections # μ„Έμ…˜ μƒνƒœμ— detections μ €μž₯
143
- st.subheader("Detected Clothing Items:")
144
- selected_category = st.selectbox("Select a category to search:",
145
- [f"{d['category']} (Confidence: {d['confidence']:.2f})" for d in detections])
146
-
147
- if st.button("Search Similar Items"):
148
- st.session_state.selected_category = selected_category # μ„ νƒλœ μΉ΄ν…Œκ³ λ¦¬ μ €μž₯
149
- st.session_state.search_clicked = True # 검색 λ²„νŠΌ 클릭 μƒνƒœ μ €μž₯
150
  else:
151
  st.warning("No clothing items detected in the image.")
152
  else:
@@ -154,30 +155,42 @@ if search_type == "Image URL":
154
  else:
155
  st.warning("Please enter an image URL.")
156
 
157
- # 검색 κ²°κ³Ό ν‘œμ‹œ 둜직
158
- if 'search_clicked' in st.session_state and st.session_state.search_clicked:
159
- selected_detection = next(d for d in st.session_state.detections
160
- if f"{d['category']} (Confidence: {d['confidence']:.2f})" == st.session_state.selected_category)
161
- cropped_image = crop_image(query_image, selected_detection['bbox'])
162
- st.image(cropped_image, caption="Cropped Image", use_column_width=True)
163
- query_embedding = get_image_embedding(cropped_image)
164
- similar_images = find_similar_images(query_embedding)
165
-
166
- st.subheader("Similar Items:")
167
- for img in similar_images:
168
- col1, col2 = st.columns(2)
169
- with col1:
170
- st.image(img['info']['image_url'], use_column_width=True)
171
- with col2:
172
- st.write(f"Name: {img['info']['name']}")
173
- st.write(f"Brand: {img['info']['brand']}")
174
- st.write(f"Category: {img['info']['category']}")
175
- st.write(f"Price: {img['info']['price']}")
176
- st.write(f"Discount: {img['info']['discount']}%")
177
- st.write(f"Similarity: {img['similarity']:.2f}")
178
-
179
- # 검색 μ™„λ£Œ ν›„ μƒνƒœ μ΄ˆκΈ°ν™”
180
- st.session_state.search_clicked = False
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  else: # Text search
183
  query_text = st.text_input("Enter search text:")
 
124
  def crop_image(image, bbox):
125
  return image.crop((bbox[0], bbox[1], bbox[2], bbox[3]))
126
 
127
+ # μ„Έμ…˜ μƒνƒœ μ΄ˆκΈ°ν™”
128
+ if 'step' not in st.session_state:
129
+ st.session_state.step = 'input'
130
+ if 'query_image_url' not in st.session_state:
131
+ st.session_state.query_image_url = ''
132
+ if 'detections' not in st.session_state:
133
+ st.session_state.detections = []
134
+ if 'selected_category' not in st.session_state:
135
+ st.session_state.selected_category = None
136
+
137
  # Streamlit app
138
  st.title("Advanced Fashion Search App")
139
 
140
+ # 단계별 처리
141
+ if st.session_state.step == 'input':
142
+ st.session_state.query_image_url = st.text_input("Enter image URL:", st.session_state.query_image_url)
 
143
  if st.button("Detect Clothing"):
144
+ if st.session_state.query_image_url:
145
+ query_image = load_image_from_url(st.session_state.query_image_url)
146
  if query_image is not None:
147
+ st.session_state.query_image = query_image
148
+ st.session_state.detections = detect_clothing(query_image)
149
+ if st.session_state.detections:
150
+ st.session_state.step = 'select_category'
 
 
 
 
 
 
 
 
151
  else:
152
  st.warning("No clothing items detected in the image.")
153
  else:
 
155
  else:
156
  st.warning("Please enter an image URL.")
157
 
158
+ elif st.session_state.step == 'select_category':
159
+ st.image(st.session_state.query_image, caption="Query Image", use_column_width=True)
160
+ st.subheader("Detected Clothing Items:")
161
+ options = [f"{d['category']} (Confidence: {d['confidence']:.2f})" for d in st.session_state.detections]
162
+ selected_option = st.selectbox("Select a category to search:", options)
163
+ if st.button("Search Similar Items"):
164
+ st.session_state.selected_category = selected_option
165
+ st.session_state.step = 'show_results'
166
+
167
+ elif st.session_state.step == 'show_results':
168
+ st.image(st.session_state.query_image, caption="Query Image", use_column_width=True)
169
+ selected_detection = next(d for d in st.session_state.detections
170
+ if f"{d['category']} (Confidence: {d['confidence']:.2f})" == st.session_state.selected_category)
171
+ cropped_image = crop_image(st.session_state.query_image, selected_detection['bbox'])
172
+ st.image(cropped_image, caption="Cropped Image", use_column_width=True)
173
+ query_embedding = get_image_embedding(cropped_image)
174
+ similar_images = find_similar_images(query_embedding)
175
+
176
+ st.subheader("Similar Items:")
177
+ for img in similar_images:
178
+ col1, col2 = st.columns(2)
179
+ with col1:
180
+ st.image(img['info']['image_url'], use_column_width=True)
181
+ with col2:
182
+ st.write(f"Name: {img['info']['name']}")
183
+ st.write(f"Brand: {img['info']['brand']}")
184
+ st.write(f"Category: {img['info']['category']}")
185
+ st.write(f"Price: {img['info']['price']}")
186
+ st.write(f"Discount: {img['info']['discount']}%")
187
+ st.write(f"Similarity: {img['similarity']:.2f}")
188
+
189
+ if st.button("Start New Search"):
190
+ st.session_state.step = 'input'
191
+ st.session_state.query_image_url = ''
192
+ st.session_state.detections = []
193
+ st.session_state.selected_category = None
194
 
195
  else: # Text search
196
  query_text = st.text_input("Enter search text:")