-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
185 lines (140 loc) · 7.5 KB
/
app.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
import streamlit as st
import cv2
import numpy as np
from ultralytics import YOLO
from segment_anything import sam_model_registry, SamPredictor
import tempfile
import os
from PIL import Image
import replicate
import dotenv
from urllib.parse import urlparse
st.set_page_config(layout="wide")
from src.products_parse import get_best_links_within_budget
from src.product_search import product_search
from src.stabledesign import stabledesign
from src.product_search import product_search
from src.stabledesign import stabledesign
dotenv.load_dotenv()
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
if not REPLICATE_API_TOKEN:
st.error("Missing Replicate API token. Please check your .env file.")
else:
replicate.client = replicate.Client(api_token=REPLICATE_API_TOKEN)
if 'yolo_model' not in st.session_state:
import time
times = [time.time()]
print('Loading yolo_model')
st.session_state['yolo_model'] = YOLO("yolov8m.pt")
times.append(time.time())
print(f'Loaded yolo_model in {times[-1] - times[-2]:.2f}s')
st.session_state['sam_checkpoint'] = "sam_vit_h_4b8939.pth"
st.session_state['model_type'] = "vit_h"
st.session_state['sam'] = sam_model_registry[st.session_state['model_type']](checkpoint=st.session_state['sam_checkpoint'])
times.append(time.time())
print(f'Loaded sam_model in {times[-1] - times[-2]:.2f}s')
st.session_state['sam_predictor'] = SamPredictor(st.session_state['sam'])
times.append(time.time())
print(f'Loaded sam_predictor in {times[-1] - times[-2]:.2f}s')
st.title("Decorate Your Room")
INTRO = """
*Developed by Anthony Zang, Chandreyi (Zini) Chakraborty, Isaac Song, Kieran Slattery*
*Hacklytics 2025 @ Georgia Tech*
"""
st.write(INTRO)
budget = st.text_input("Budget", 1000)
prompt = st.text_input("Style", placeholder="Modern and minimalistic")
uploaded_file = st.file_uploader("Choose a room...", type=["jpg", "jpeg", "png"])
if st.button('Reimagine Your Room', type="primary") and uploaded_file:
container = st.container()
with container:
col1, col2 = st.columns([1, 1])
image = Image.open(uploaded_file)
image_path = tempfile.NamedTemporaryFile(delete=False, suffix='.png').name
image.save(image_path)
with col1:
st.image(image, caption="Before", width=400)
with st.spinner('### Reimagining your room...', show_time=True):
gen_image = stabledesign(image, prompt, optimize=True)
with col1:
st.image(gen_image, caption="After", width=400)
final_image = np.array(gen_image)
final_image = cv2.cvtColor(final_image, cv2.COLOR_RGB2BGR)
results = st.session_state['yolo_model'](final_image)
all_results = []
class_names = []
pri = []
last = []
for obj in results[0].boxes:
if 'plant' in (st.session_state['yolo_model'].names[int(obj.cls[0])]):
last.append(obj)
else:
pri.append(obj)
objects = pri + last
with col2:
for i, result in enumerate(objects):
x1, y1, x2, y2 = map(int, result.xyxy[0])
cropped_object = final_image[y1:y2, x1:x2]
class_id = int(result.cls[0])
class_name = st.session_state['yolo_model'].names[class_id]
if 'table' in class_name:
class_name = 'table'
class_name = class_name.capitalize()
class_names.append(class_name)
image_rgb = cv2.cvtColor(final_image, cv2.COLOR_RGB2BGR)
st.session_state['sam_predictor'].set_image(image_rgb)
input_box = np.array([[x1, y1, x2, y2]])
masks = st.session_state['sam_predictor'].predict(box=input_box)
mask = masks[0].astype(np.uint8) * 255
masked_object = cv2.bitwise_and(cropped_object, cropped_object, mask=mask[y1:y2, x1:x2])
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
temp_file_path = temp_file.name
cv2.imwrite(temp_file_path, masked_object)
object_results = product_search(temp_file_path)
all_results.append(object_results)
with st.expander(f"{class_name}"):
minicontainer = st.container()
with minicontainer:
minicol1, minicol2 = st.columns([1, 1])
minicol1.image(temp_file_path, width=150)
minicol2.markdown(f"<span style='font-weight: bold; text-transform: uppercase; font-size: 32px;'>{class_name}</span>", unsafe_allow_html=True)
if object_results:
for i, item in enumerate(object_results):
domain = urlparse(item['link']).netloc.split('.')[1].capitalize()
st.markdown(f"""
<div style="border: 2px solid #000; padding: 10px; margin-bottom: 10px; border-color: white; display: flex; align-items: center;">
<img src="{item['img']}" width="150" style="margin-right: 20px;" />
<div>
<p style="font-weight: bold;"><a href="{item['link']}">Product {i+1}</a></p>
<p><strong>Price:</strong> {item['price']}</p>
<p><strong>Domain:</strong> {domain}</p>
</div>
</div>
""", unsafe_allow_html=True)
else:
st.write("No relevant items found.")
os.remove(temp_file_path)
if all_results:
st.markdown("## Optimal Budgeted Products:")
selected, total_price, class_matches = get_best_links_within_budget(all_results, budget, class_names)
if selected:
st.write(f"Total Price: ${total_price:.2f}")
for itr, item in enumerate(selected):
domain = urlparse(item['link']).netloc.split('.')[-2].capitalize()
st.markdown(f"""
<div style="border: 2px solid #000; padding: 10px; margin-bottom: 10px; border-color: white; display: flex; flex-direction: column; align-items: flex-start;">
<div style="font-weight: bold; font-size: 18px; margin-bottom: 10px;">{class_matches[itr]}</div>
<div style="display: flex; align-items: center;">
<img src="{item['img']}" width="150" style="margin-right: 20px;" />
<div>
<p style="font-weight: bold;"><a href="{item['link']}">Product {itr+1}</a></p>
<p><strong>Price:</strong> {item['price']}</p>
<p><strong>Domain:</strong> {domain}</p>
</div>
</div>
</div>
""", unsafe_allow_html=True)
else:
st.write("No items fit within the budget.")
else:
st.write("No relevant items found.")