Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,13 +10,6 @@ import numpy as np
|
|
10 |
from ultralytics import YOLO
|
11 |
import cv2
|
12 |
|
13 |
-
try:
|
14 |
-
from streamlit_img_label import st_img_label
|
15 |
-
from streamlit_img_label.manage import ImageManager
|
16 |
-
except ImportError:
|
17 |
-
st.error("Required modules are not installed. Please install 'streamlit-img-label' and 'pascal-voc-writer'.")
|
18 |
-
st.stop()
|
19 |
-
|
20 |
# Load CLIP model and tokenizer
|
21 |
@st.cache_resource
|
22 |
def load_clip_model():
|
@@ -113,14 +106,6 @@ def find_similar_images(query_embedding, top_k=5):
|
|
113 |
})
|
114 |
return results
|
115 |
|
116 |
-
# 먼저 라벨 리스트를 정의합니다
|
117 |
-
labels = [
|
118 |
-
"bag", "coat", "dress", "fabric", "hat", "Hoodie", "jacket", "Jacket",
|
119 |
-
"jean", "m2m", "Mid-lenght dress", "Pants", "plain", "shirt", "Shirt",
|
120 |
-
"shoe", "short", "shorts", "skirt", "slacks", "suit", "sunglass",
|
121 |
-
"sweat", "tie", "tracksuit", "tshirt"
|
122 |
-
]
|
123 |
-
|
124 |
def detect_clothing(image):
|
125 |
results = yolo_model(image)
|
126 |
detections = results[0].boxes.data.cpu().numpy()
|
@@ -128,7 +113,7 @@ def detect_clothing(image):
|
|
128 |
for detection in detections:
|
129 |
x1, y1, x2, y2, conf, cls = detection
|
130 |
category = yolo_model.names[int(cls)]
|
131 |
-
if category in
|
132 |
categories.append({
|
133 |
'category': category,
|
134 |
'bbox': [int(x1), int(y1), int(x2), int(y2)],
|
@@ -139,39 +124,6 @@ def detect_clothing(image):
|
|
139 |
def crop_image(image, bbox):
|
140 |
return image.crop((bbox[0], bbox[1], bbox[2], bbox[3]))
|
141 |
|
142 |
-
def adjust_bounding_boxes(image, detections):
|
143 |
-
img_height, img_width = image.size
|
144 |
-
rects = []
|
145 |
-
for detection in detections:
|
146 |
-
x1, y1, x2, y2 = detection['bbox']
|
147 |
-
rects.append({
|
148 |
-
"left": x1 / img_width,
|
149 |
-
"top": y1 / img_height,
|
150 |
-
"width": (x2 - x1) / img_width,
|
151 |
-
"height": (y2 - y1) / img_height,
|
152 |
-
"label": detection['category']
|
153 |
-
})
|
154 |
-
|
155 |
-
try:
|
156 |
-
adjusted_rects = st_img_label(image, box_color="red", rects=rects)
|
157 |
-
except Exception as e:
|
158 |
-
st.error(f"Error in st_img_label: {str(e)}")
|
159 |
-
return detections
|
160 |
-
|
161 |
-
adjusted_detections = []
|
162 |
-
for rect, detection in zip(adjusted_rects, detections):
|
163 |
-
x1 = rect["left"] * img_width
|
164 |
-
y1 = rect["top"] * img_height
|
165 |
-
x2 = x1 + (rect["width"] * img_width)
|
166 |
-
y2 = y1 + (rect["height"] * img_height)
|
167 |
-
adjusted_detections.append({
|
168 |
-
'category': rect["label"],
|
169 |
-
'bbox': [int(x1), int(y1), int(x2), int(y2)],
|
170 |
-
'confidence': detection['confidence']
|
171 |
-
})
|
172 |
-
|
173 |
-
return adjusted_detections
|
174 |
-
|
175 |
# 세션 상태 초기화
|
176 |
if 'step' not in st.session_state:
|
177 |
st.session_state.step = 'input'
|
@@ -206,11 +158,6 @@ if st.session_state.step == 'input':
|
|
206 |
elif st.session_state.step == 'select_category':
|
207 |
st.image(st.session_state.query_image, caption="Query Image", use_column_width=True)
|
208 |
st.subheader("Detected Clothing Items:")
|
209 |
-
|
210 |
-
# 경계 상자 조정 기능 추가
|
211 |
-
adjusted_detections = adjust_bounding_boxes(st.session_state.query_image, st.session_state.detections)
|
212 |
-
st.session_state.detections = adjusted_detections
|
213 |
-
|
214 |
options = [f"{d['category']} (Confidence: {d['confidence']:.2f})" for d in st.session_state.detections]
|
215 |
selected_option = st.selectbox("Select a category to search:", options)
|
216 |
if st.button("Search Similar Items"):
|
|
|
10 |
from ultralytics import YOLO
|
11 |
import cv2
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Load CLIP model and tokenizer
|
14 |
@st.cache_resource
|
15 |
def load_clip_model():
|
|
|
106 |
})
|
107 |
return results
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
def detect_clothing(image):
|
110 |
results = yolo_model(image)
|
111 |
detections = results[0].boxes.data.cpu().numpy()
|
|
|
113 |
for detection in detections:
|
114 |
x1, y1, x2, y2, conf, cls = detection
|
115 |
category = yolo_model.names[int(cls)]
|
116 |
+
if category in ['sunglass','hat','jacket','shirt','pants','shorts','skirt','dress','bag','shoe']:
|
117 |
categories.append({
|
118 |
'category': category,
|
119 |
'bbox': [int(x1), int(y1), int(x2), int(y2)],
|
|
|
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'
|
|
|
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"):
|