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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# VGG16
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
from tensorflow.keras.preprocessing import image as kimage
|
5 |
+
from cryptography.fernet import Fernet
|
6 |
+
import os
|
7 |
+
|
8 |
+
from io import BytesIO
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
|
15 |
+
dec_key =os.getenv("FERNET_KEY")
|
16 |
+
|
17 |
+
cipher_suite=Fernet(dec_key)
|
18 |
+
|
19 |
+
# Read the encrypted content from model.py.enc file
|
20 |
+
with open('model.py.enc', 'rb') as file:
|
21 |
+
encrypted_model = file.read()
|
22 |
+
|
23 |
+
decrypted_model = cipher_suite.decrypt(encrypted_model)
|
24 |
+
decrypted_model_str = decrypted_model.decode()
|
25 |
+
|
26 |
+
# Execute the decrypted model string
|
27 |
+
exec(decrypted_model_str)
|
28 |
+
st.set_page_config(
|
29 |
+
layout="wide",
|
30 |
+
initial_sidebar_state="expanded",
|
31 |
+
)
|
32 |
+
|
33 |
+
@st.cache_data
|
34 |
+
def load_data():
|
35 |
+
# Read the encrypted content from the Excel file
|
36 |
+
with open('pantolon-v3.xlsx.enc', 'rb') as file:
|
37 |
+
encrypted_data = file.read()
|
38 |
+
|
39 |
+
# Decrypt the data
|
40 |
+
decrypted_data = cipher_suite.decrypt(encrypted_data)
|
41 |
+
|
42 |
+
# Load the decrypted data into a pandas DataFrame
|
43 |
+
df = pd.read_excel(BytesIO(decrypted_data))
|
44 |
+
|
45 |
+
return df
|
46 |
+
|
47 |
+
|
48 |
+
# Read the encrypted content from model.py.enc file
|
49 |
+
with open('model_takemura.py.enc', 'rb') as file:
|
50 |
+
encrypted_model_takemura = file.read()
|
51 |
+
|
52 |
+
decrypted_model_takemura = cipher_suite.decrypt(encrypted_model_takemura)
|
53 |
+
decrypted_model_str_takemura = decrypted_model_takemura.decode()
|
54 |
+
|
55 |
+
# Execute the decrypted model string
|
56 |
+
exec(decrypted_model_str_takemura)
|
57 |
+
|
58 |
+
# from model_takemura import *
|
59 |
+
|
60 |
+
|
61 |
+
# Read the encrypted content from model.py.enc file
|
62 |
+
with open('model_hayabusa.py.enc', 'rb') as file:
|
63 |
+
encrypted_model_hayabusa = file.read()
|
64 |
+
|
65 |
+
decrypted_model_hayabusa = cipher_suite.decrypt(encrypted_model_hayabusa)
|
66 |
+
decrypted_model_str_hayabusa = decrypted_model_hayabusa.decode()
|
67 |
+
|
68 |
+
# Execute the decrypted model string
|
69 |
+
exec(decrypted_model_str_hayabusa)
|
70 |
+
|
71 |
+
#
|
72 |
+
# from model_hayabusa import *
|
73 |
+
|
74 |
+
|
75 |
+
def page1():
|
76 |
+
st.title("Ürün Benzerlik Analizi")
|
77 |
+
st.write(
|
78 |
+
"Ürün benzerlik analizi, ürününüzün fotoğrafını yükleyerek benzer ürünleri ve verilerini bulmanızı sağlar.")
|
79 |
+
|
80 |
+
image = st.sidebar.file_uploader("Lütfen ürününüzün fotoğrafını yükleyin:")
|
81 |
+
|
82 |
+
st.markdown("""
|
83 |
+
<style>
|
84 |
+
|
85 |
+
.stTabs [data-baseweb="tab-list"] {
|
86 |
+
gap: 20px;
|
87 |
+
padding: 10px/* Increase the gap between tabs */
|
88 |
+
|
89 |
+
}
|
90 |
+
|
91 |
+
.stTabs [data-baseweb="tab-list"] button [data-testid="stMarkdownContainer"] p {
|
92 |
+
font-size:1.5rem;
|
93 |
+
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
|
94 |
+
}
|
95 |
+
|
96 |
+
.stTabs [data-baseweb="tab"] {
|
97 |
+
height: 50px;
|
98 |
+
white-space: pre-wrap;
|
99 |
+
border-radius: 12px; /* Make the tabs look like pills */
|
100 |
+
padding: 10px 20px; /* Add padding to the tabs */
|
101 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); /* Add shadow to the tabs */
|
102 |
+
transition: background-color 0.3s ease; /* Add transition effect */
|
103 |
+
color: #333; /* Change the text color to a soft black */
|
104 |
+
}
|
105 |
+
|
106 |
+
.stTabs [aria-selected="true"] {
|
107 |
+
background-color: #e0e0e0; /* Change the background color to a soft gray */
|
108 |
+
border-color: #3d5afe; /* Add border color to the selected tab */
|
109 |
+
color: #ffffff; /* Change the text color to a soft blue */;
|
110 |
+
}
|
111 |
+
|
112 |
+
.stTabs [aria-selected="true"]:hover {
|
113 |
+
background-color: #d0d0d0; /* Change background color when hover on the selected tab */
|
114 |
+
}
|
115 |
+
|
116 |
+
</style>""", unsafe_allow_html=True)
|
117 |
+
|
118 |
+
tab1, tab2 = st.tabs(["Takemura", "Hayabusa"])
|
119 |
+
|
120 |
+
if image is not None:
|
121 |
+
st.sidebar.success("Görsel başarıyla yüklendi.")
|
122 |
+
|
123 |
+
product_category = st.sidebar.selectbox("Lütfen ürün kategorisi seçin:",
|
124 |
+
["Pantolon", "Gömlek - (Test)", "Elbise - (Test)", "Ceket - (Test)", "Hırka - (Test)"])
|
125 |
+
if product_category == "Pantolon":
|
126 |
+
|
127 |
+
default_product_details = ["Desen", "Bel", "Paça"] # Assign a default value
|
128 |
+
|
129 |
+
product_details = st.sidebar.multiselect("Benzerlik için öncelik sırasına göre detay seçin:",
|
130 |
+
["Bel", "Desen", "Paça"],
|
131 |
+
default=default_product_details)
|
132 |
+
|
133 |
+
if not product_details: # If product_details is an empty list
|
134 |
+
st.sidebar.error("En az 1 özellik seçilmelidir.")
|
135 |
+
|
136 |
+
with tab1:
|
137 |
+
if st.button("Takemura ile Analiz Yap"):
|
138 |
+
status_placeholder = st.empty()
|
139 |
+
status_placeholder.status("Analizi yapılıyor...")
|
140 |
+
|
141 |
+
filenames = model_1(image)
|
142 |
+
st.session_state['filenames'] = filenames
|
143 |
+
st.session_state['image'] = image
|
144 |
+
st.session_state['analysis_done'] = True
|
145 |
+
|
146 |
+
status_placeholder.success("Analiz tamamlandı.")
|
147 |
+
|
148 |
+
if 'analysis_done' in st.session_state and st.session_state['analysis_done']:
|
149 |
+
show_results_button = st.button("Sonuçları Göster", key='button1')
|
150 |
+
|
151 |
+
if show_results_button and ('show_results' not in st.session_state or not st.session_state['show_results']):
|
152 |
+
st.session_state['show_results'] = True
|
153 |
+
|
154 |
+
if 'show_results' in st.session_state:
|
155 |
+
image_dir = "general/PANTOLON"
|
156 |
+
df = load_data()
|
157 |
+
st.empty()
|
158 |
+
|
159 |
+
for _ in range(5):
|
160 |
+
try:
|
161 |
+
takemura_output = takemura(st.session_state['filenames'], image)
|
162 |
+
filenames = takemura_output.split('\n')
|
163 |
+
|
164 |
+
for filename in filenames:
|
165 |
+
filename_without_extension = os.path.splitext(filename)[0]
|
166 |
+
filename_without_extension = filename_without_extension.split('_')[0]
|
167 |
+
|
168 |
+
matching_rows = df.loc[df['ItemOption'] == filename_without_extension]
|
169 |
+
|
170 |
+
if not matching_rows.empty:
|
171 |
+
for _, row in matching_rows.iterrows():
|
172 |
+
cols = st.columns([2, 9]) # Adjust these values for desired widths
|
173 |
+
img_path = os.path.join(image_dir, filename)
|
174 |
+
img = kimage.load_img(img_path)
|
175 |
+
cols[0].image(img, width=200)
|
176 |
+
half = len(row) // 2 # Find the midpoint of the row
|
177 |
+
|
178 |
+
# Split the row into two parts
|
179 |
+
row_upper_half = row.iloc[:half]
|
180 |
+
row_lower_half = row.iloc[half:]
|
181 |
+
|
182 |
+
# Display the two parts in two separate dataframes
|
183 |
+
cols[1].dataframe(pd.DataFrame(row_upper_half).T)
|
184 |
+
cols[1].dataframe(pd.DataFrame(row_lower_half).T)
|
185 |
+
else:
|
186 |
+
st.write(f"Ürün isimlerini maalesef eşleştiremedim {filename_without_extension}")
|
187 |
+
st.write(f"Ürün ismi: {filename_without_extension}")
|
188 |
+
|
189 |
+
break
|
190 |
+
|
191 |
+
except Exception as e:
|
192 |
+
st.write(f"Lütfen 'Sonuçları Göster' butonuna tekrar basınız.. ...")
|
193 |
+
|
194 |
+
st.session_state['show_results'] = False
|
195 |
+
|
196 |
+
with tab2:
|
197 |
+
if st.button("Hayabusa ile Analiz Yap"):
|
198 |
+
status_placeholder = st.empty()
|
199 |
+
status_placeholder.status("Analizi yapılıyor...")
|
200 |
+
|
201 |
+
filenames = model_2(image)
|
202 |
+
st.session_state['filenames'] = filenames
|
203 |
+
st.session_state['image'] = image
|
204 |
+
st.session_state['analysis_done'] = True
|
205 |
+
|
206 |
+
status_placeholder.success("Analiz tamamlandı.")
|
207 |
+
|
208 |
+
if 'analysis_done' in st.session_state and st.session_state['analysis_done']:
|
209 |
+
show_results_button = st.button("Hayabusa Sonuçlarını Göster", key='button2')
|
210 |
+
if show_results_button and ('show_results' not in st.session_state or not st.session_state['show_results']):
|
211 |
+
st.session_state['show_results'] = True
|
212 |
+
|
213 |
+
if 'show_results' in st.session_state:
|
214 |
+
image_dir = "general/PANTOLON"
|
215 |
+
df = load_data()
|
216 |
+
st.empty()
|
217 |
+
|
218 |
+
for _ in range(3):
|
219 |
+
try:
|
220 |
+
takemura_output = takemura(st.session_state['filenames'], image, product_details)
|
221 |
+
filenames = takemura_output.split('\n')
|
222 |
+
filenames = [filename[filename.rfind(' ') + 1:] for filename in filenames if ' ' in filename]
|
223 |
+
|
224 |
+
for filename in filenames:
|
225 |
+
filename_without_extension = os.path.splitext(filename)[0]
|
226 |
+
filename_without_extension = filename_without_extension.split('_')[0]
|
227 |
+
|
228 |
+
matching_rows = df.loc[df['ItemOption'] == filename_without_extension]
|
229 |
+
|
230 |
+
if not matching_rows.empty:
|
231 |
+
for _, row in matching_rows.iterrows():
|
232 |
+
cols = st.columns([2, 9]) # Adjust these values for desired widths
|
233 |
+
img_path = os.path.join(image_dir, filename)
|
234 |
+
img = kimage.load_img(img_path)
|
235 |
+
cols[0].image(img, width=200)
|
236 |
+
# cols[1].dataframe(pd.DataFrame(row).T)
|
237 |
+
half = len(row) // 2 # Find the midpoint of the row
|
238 |
+
|
239 |
+
# Split the row into two parts
|
240 |
+
row_upper_half = row.iloc[:half]
|
241 |
+
row_lower_half = row.iloc[half:]
|
242 |
+
|
243 |
+
# Display the two parts in two separate dataframes
|
244 |
+
cols[1].dataframe(pd.DataFrame(row_upper_half).T)
|
245 |
+
cols[1].dataframe(pd.DataFrame(row_lower_half).T)
|
246 |
+
else:
|
247 |
+
st.write(f"No matching rows found for filename {filename_without_extension}")
|
248 |
+
break
|
249 |
+
|
250 |
+
except Exception as e:
|
251 |
+
st.write(f"Lütfen 'Sonuçları Göster' butonuna tekrar basınız.. ...")
|
252 |
+
|
253 |
+
st.session_state['show_results'] = False
|
254 |
+
|
255 |
+
|
256 |
+
|
257 |
+
else:
|
258 |
+
st.write("Başlamak için sol tarafa lütfen ürün fotoğrafı yükleyin.")
|