-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_visage.py
More file actions
208 lines (151 loc) · 6.63 KB
/
test_visage.py
File metadata and controls
208 lines (151 loc) · 6.63 KB
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
import streamlit as st
from PIL import Image
import numpy as np
import cv2
from ultralytics import YOLO
import functions
import time
import requests
import time
from io import BytesIO
import base64
import cv2
import cv2
import numpy as np
def pil_to_base64(img):
buffer = BytesIO()
img.save(buffer, format="PNG")
return base64.b64encode(buffer.getvalue()).decode()
st.set_page_config(page_title="Trouve la Poké-pétite", layout="wide")
# Masquer toolbar et footer
hide_streamlit_style = """
<style>
div[data-testid="stToolbar"], div[data-testid="stDecoration"], div[data-testid="stStatusWidget"], #MainMenu, header, footer {
visibility: hidden;
height: 0%;
position: fixed;
}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
st.markdown("## Pokemon scanner")
# Charger modèles
@st.cache_resource(show_spinner=False)
def load_models():
collection, meta = functions.load_faiss_index()
embedding_model, preprocess, device = functions.load_custom_model("models/dino_small_lora_merged")
yolo_model = 'models/my-model12.pt'
model = YOLO(yolo_model)
return model, collection, meta, embedding_model, preprocess, device
model, collection, meta, embedding_model, preprocess, device = load_models()
print(collection)
image = None
# Inputs
img_file_buffer = st.camera_input("📸 Prends une photo ou sélectionne-en une", key="camera_input")
uploaded_file = None #st.file_uploader("Ou charge une image existante", type=["jpg", "jpeg", "png"])
# Nouvelle image sélectionnée
if img_file_buffer:
image = Image.open(img_file_buffer).convert("RGB")
# Réinitialiser les états
st.session_state.detected_cards = []
st.session_state.detected_collections = []
elif uploaded_file:
image = Image.open(uploaded_file).convert("RGB")
# Réinitialiser les états
st.session_state.detected_cards = []
st.session_state.detected_collections = []
# Container pour affichage progressif
# --- Initialisation session_state pour collections ---
if "detected_collections" not in st.session_state:
st.session_state.detected_collections = []
# Session state pour collection
if "detected_cards" not in st.session_state:
st.session_state.detected_cards = []
# --- Affichage de l'image principale ---
#image_placeholder = st.empty() # Placeholder pour l'image source annotée
# Placeholder unique pour toutes les collections
collections_placeholder = st.empty()
# --- Initialisation session_state pour collections ---
if "detected_collections" not in st.session_state:
st.session_state.detected_collections = []
# --- Traitement de l'image ---
if image is not None:
img = np.array(image, dtype=np.uint8)
crop_pil = Image.fromarray(img)
candidate_order_ids = [int(card["order"]) for card in meta]
distances, indices = functions.search_card_correspondance(
collection, crop_pil, embedding_model, preprocess , 10, candidate_order_ids
)
search_results = []
for i, idx in enumerate(indices):
search_results.append(meta[idx])
search_results[i]['distance_faiss'] = float(distances[i])
#Un gap est anormal si il est au moins N fois plus grand que la moyenne des 2–3 gaps suivants.
search_results = sorted(search_results, key=lambda x: x["distance_faiss"], reverse=False)
gaps = [search_results[i+1]['distance_faiss'] - search_results[i]['distance_faiss'] for i in range(len(search_results)-1)]
#Pour chaque on regarde les deux suivants
for j in range(len(gaps)-1):
# On regarde les 2 gaps suivants (si disponibles)
next_gaps = gaps[j+1:j+3]
if not next_gaps:
continue
mean_next = sum(next_gaps) / len(next_gaps)
# Gap anormal si il est >> des gaps suivants
if gaps[j] > 3 * mean_next:
search_results = search_results[0:j+1]
break
# --- Créer la collection ---
card_data = {
"crop": img,
"reference": search_results[0]['img'],
"set" : search_results[0]['set'],
"number" : search_results[0]['number'],
"name": search_results[0]['name'],
"price": 0 if search_results[0]['price_eur'] ==None else search_results[0]['price_eur'],
"tcgplayer": search_results[0]['tcgplayer_link'],
"cardmarket": search_results[0]['cardmarket_link'],
"history": search_results[0]['price_evolution_url'],
"distance_faiss": search_results[0]['distance_faiss'],
"img": search_results[0]['img'],
"distance_phash": 0
}
st.session_state.detected_collections.append({
"main_card": card_data,
"search_results": search_results
})
# --- Trier toutes les collections par prix décroissant ---
st.session_state.detected_collections.sort(
key=lambda c: c["main_card"]["price"], reverse=True
)
#image_placeholder.image(img_annotated, use_container_width=True)
# Mettre à jour l'affichage dans le placeholder
with collections_placeholder.container():
for c in st.session_state.detected_collections:
card_data = c["main_card"]
search_results = c["search_results"]
#img_b64 = pil_to_base64(card_data["crop"])
modal_id = f"modal_img_{i}" # ID unique pour chaque carte
md = f"""
<table style="margin-bottom:12px; border-radius:8px; padding:6px; width:100%;">
<tr>
<td style="width:400px;">
<img src="{card_data['img']}" width=150% style="border-radius:4px;"/>
</td>
<td style="vertical-align:top; padding-left:10px;">
<strong style="font-size:30px;">{card_data['name']}</strong><br/>
<span style="font-size:16px;">{card_data['set']}/{card_data['number']} </span><br/>
<span style="color:#e63946;font-size:20px; font-weight:bold;">💰 {card_data['price']} EUR</span><br/>
<span style="font-size:14px;">
<a href="{card_data['tcgplayer']}" target="_blank">TCGPlayer</a> |
<a href="{card_data['cardmarket']}" target="_blank">CardMarket</a> |
<a href="{card_data['history']}" target="_blank">Historique</a>
</span>
</td>
</tr>
</table>
"""
st.markdown(md, unsafe_allow_html=True)
st.markdown("## Analyse terminée")
# --- Valeur totale de la collection ---
total_value = sum([c["main_card"]['price'] for c in st.session_state.detected_collections])
st.markdown(f"##### 💰 Valeur totale : {total_value:.2f} €")