Spaces:
Sleeping
Sleeping
Víctor Sáez
commited on
Commit
·
0b1e00c
1
Parent(s):
c2f455f
error
Browse files
app.py
CHANGED
@@ -21,6 +21,7 @@ available_models = {
|
|
21 |
"DETR ResNet-50 Face Only": "esraakh/detr_fine_tune_face_detection_final"
|
22 |
}
|
23 |
|
|
|
24 |
def load_model(model_key):
|
25 |
global current_model, current_processor, current_model_name
|
26 |
model_name = available_models[model_key]
|
@@ -31,12 +32,14 @@ def load_model(model_key):
|
|
31 |
current_model_name = model_name
|
32 |
return current_model, current_processor
|
33 |
|
|
|
34 |
def get_font(size=12):
|
35 |
try:
|
36 |
return ImageFont.truetype("arial.ttf", size=size)
|
37 |
except:
|
38 |
return ImageFont.load_default()
|
39 |
|
|
|
40 |
translations = {
|
41 |
"English": {
|
42 |
"title": "## Enhanced Object Detection App\nUpload an image to detect objects using various DETR models.",
|
@@ -82,9 +85,11 @@ translations = {
|
|
82 |
}
|
83 |
}
|
84 |
|
|
|
85 |
def t(language, key):
|
86 |
return translations.get(language, translations["English"]).get(key, key)
|
87 |
|
|
|
88 |
def get_translated_model_choices(language):
|
89 |
model_mapping = {
|
90 |
"DETR ResNet-50": "model_fast",
|
@@ -102,6 +107,7 @@ def get_translated_model_choices(language):
|
|
102 |
translated_choices.append(translated_name)
|
103 |
return translated_choices
|
104 |
|
|
|
105 |
def get_model_key_from_translation(translated_name, language):
|
106 |
model_mapping = {
|
107 |
"DETR ResNet-50": "model_fast",
|
@@ -116,9 +122,11 @@ def get_model_key_from_translation(translated_name, language):
|
|
116 |
return translated_name
|
117 |
return "DETR ResNet-50"
|
118 |
|
|
|
119 |
# Translation logic (only if ENABLE_TRANSLATION and model is local)
|
120 |
translation_cache = {}
|
121 |
|
|
|
122 |
def translate_label(language_label, label):
|
123 |
if language_label == "English" or not ENABLE_TRANSLATION:
|
124 |
return label
|
@@ -129,6 +137,7 @@ def translate_label(language_label, label):
|
|
129 |
translation_cache[cache_key] = f"{label} (no translation)"
|
130 |
return translation_cache[cache_key]
|
131 |
|
|
|
132 |
def detect_objects(image, language_selector, translated_model_selector, threshold):
|
133 |
try:
|
134 |
if image is None:
|
@@ -200,10 +209,14 @@ def detect_objects(image, language_selector, translated_model_selector, threshol
|
|
200 |
traceback.print_exc()
|
201 |
return None, f"Error detecting objects: {e}"
|
202 |
|
|
|
203 |
def build_app():
|
|
|
|
|
|
|
204 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
205 |
-
|
206 |
-
|
207 |
with gr.Row():
|
208 |
with gr.Column(scale=1):
|
209 |
language_selector = gr.Dropdown(
|
@@ -225,6 +238,7 @@ def build_app():
|
|
225 |
step=0.05,
|
226 |
label=t("English", "threshold_label")
|
227 |
)
|
|
|
228 |
with gr.Row():
|
229 |
with gr.Column(scale=1):
|
230 |
input_image = gr.Image(type="pil", label=t("English", "input_label"))
|
@@ -236,39 +250,69 @@ def build_app():
|
|
236 |
lines=10,
|
237 |
max_lines=15
|
238 |
)
|
|
|
239 |
def update_interface(selected_language):
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
choices=translated_choices,
|
247 |
value=default_model,
|
248 |
label=t(selected_language, "dropdown_detection_model_label")
|
249 |
-
)
|
250 |
-
gr.update(label=t(selected_language, "threshold_label"))
|
251 |
-
gr.update(label=t(selected_language, "input_label"))
|
252 |
-
gr.update(value=t(selected_language, "button"))
|
253 |
-
gr.update(label=t(selected_language, "output_label"))
|
254 |
-
gr.update(label=t(selected_language, "info_label"))
|
255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
language_selector.change(
|
257 |
fn=update_interface,
|
258 |
-
inputs=language_selector,
|
259 |
outputs=[title, language_selector, model_selector, threshold_slider,
|
260 |
input_image, button, output_image, detection_info],
|
261 |
queue=False
|
262 |
)
|
|
|
|
|
263 |
button.click(
|
264 |
fn=detect_objects,
|
265 |
inputs=[input_image, language_selector, model_selector, threshold_slider],
|
266 |
outputs=[output_image, detection_info]
|
267 |
)
|
|
|
268 |
return app
|
269 |
|
|
|
|
|
270 |
load_model("DETR ResNet-50")
|
271 |
|
272 |
if __name__ == "__main__":
|
273 |
app = build_app()
|
274 |
-
app.launch()
|
|
|
21 |
"DETR ResNet-50 Face Only": "esraakh/detr_fine_tune_face_detection_final"
|
22 |
}
|
23 |
|
24 |
+
|
25 |
def load_model(model_key):
|
26 |
global current_model, current_processor, current_model_name
|
27 |
model_name = available_models[model_key]
|
|
|
32 |
current_model_name = model_name
|
33 |
return current_model, current_processor
|
34 |
|
35 |
+
|
36 |
def get_font(size=12):
|
37 |
try:
|
38 |
return ImageFont.truetype("arial.ttf", size=size)
|
39 |
except:
|
40 |
return ImageFont.load_default()
|
41 |
|
42 |
+
|
43 |
translations = {
|
44 |
"English": {
|
45 |
"title": "## Enhanced Object Detection App\nUpload an image to detect objects using various DETR models.",
|
|
|
85 |
}
|
86 |
}
|
87 |
|
88 |
+
|
89 |
def t(language, key):
|
90 |
return translations.get(language, translations["English"]).get(key, key)
|
91 |
|
92 |
+
|
93 |
def get_translated_model_choices(language):
|
94 |
model_mapping = {
|
95 |
"DETR ResNet-50": "model_fast",
|
|
|
107 |
translated_choices.append(translated_name)
|
108 |
return translated_choices
|
109 |
|
110 |
+
|
111 |
def get_model_key_from_translation(translated_name, language):
|
112 |
model_mapping = {
|
113 |
"DETR ResNet-50": "model_fast",
|
|
|
122 |
return translated_name
|
123 |
return "DETR ResNet-50"
|
124 |
|
125 |
+
|
126 |
# Translation logic (only if ENABLE_TRANSLATION and model is local)
|
127 |
translation_cache = {}
|
128 |
|
129 |
+
|
130 |
def translate_label(language_label, label):
|
131 |
if language_label == "English" or not ENABLE_TRANSLATION:
|
132 |
return label
|
|
|
137 |
translation_cache[cache_key] = f"{label} (no translation)"
|
138 |
return translation_cache[cache_key]
|
139 |
|
140 |
+
|
141 |
def detect_objects(image, language_selector, translated_model_selector, threshold):
|
142 |
try:
|
143 |
if image is None:
|
|
|
209 |
traceback.print_exc()
|
210 |
return None, f"Error detecting objects: {e}"
|
211 |
|
212 |
+
|
213 |
def build_app():
|
214 |
+
# Crear componentes con referencias globales
|
215 |
+
title = gr.Markdown(t("English", "title"))
|
216 |
+
|
217 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
218 |
+
title.render()
|
219 |
+
|
220 |
with gr.Row():
|
221 |
with gr.Column(scale=1):
|
222 |
language_selector = gr.Dropdown(
|
|
|
238 |
step=0.05,
|
239 |
label=t("English", "threshold_label")
|
240 |
)
|
241 |
+
|
242 |
with gr.Row():
|
243 |
with gr.Column(scale=1):
|
244 |
input_image = gr.Image(type="pil", label=t("English", "input_label"))
|
|
|
250 |
lines=10,
|
251 |
max_lines=15
|
252 |
)
|
253 |
+
|
254 |
def update_interface(selected_language):
|
255 |
+
try:
|
256 |
+
translated_choices = get_translated_model_choices(selected_language)
|
257 |
+
default_model = t(selected_language, "model_fast")
|
258 |
+
|
259 |
+
# Asegurar que default_model está en las opciones
|
260 |
+
if default_model not in translated_choices:
|
261 |
+
default_model = translated_choices[0] if translated_choices else "General Objects (fast)"
|
262 |
+
|
263 |
+
updates = []
|
264 |
+
updates.append(gr.update(value=t(selected_language, "title"))) # title
|
265 |
+
updates.append(gr.update(label=t(selected_language, "dropdown_label"))) # language_selector
|
266 |
+
updates.append(gr.update(
|
267 |
choices=translated_choices,
|
268 |
value=default_model,
|
269 |
label=t(selected_language, "dropdown_detection_model_label")
|
270 |
+
)) # model_selector
|
271 |
+
updates.append(gr.update(label=t(selected_language, "threshold_label"))) # threshold_slider
|
272 |
+
updates.append(gr.update(label=t(selected_language, "input_label"))) # input_image
|
273 |
+
updates.append(gr.update(value=t(selected_language, "button"))) # button
|
274 |
+
updates.append(gr.update(label=t(selected_language, "output_label"))) # output_image
|
275 |
+
updates.append(gr.update(label=t(selected_language, "info_label"))) # detection_info
|
276 |
+
|
277 |
+
return updates
|
278 |
+
except Exception as e:
|
279 |
+
print(f"Error in update_interface: {e}")
|
280 |
+
import traceback
|
281 |
+
traceback.print_exc()
|
282 |
+
# Retornar valores por defecto en caso de error
|
283 |
+
return [
|
284 |
+
gr.update(), # title
|
285 |
+
gr.update(), # language_selector
|
286 |
+
gr.update(), # model_selector
|
287 |
+
gr.update(), # threshold_slider
|
288 |
+
gr.update(), # input_image
|
289 |
+
gr.update(), # button
|
290 |
+
gr.update(), # output_image
|
291 |
+
gr.update() # detection_info
|
292 |
+
]
|
293 |
+
|
294 |
+
# Configurar el evento de cambio de idioma
|
295 |
language_selector.change(
|
296 |
fn=update_interface,
|
297 |
+
inputs=[language_selector],
|
298 |
outputs=[title, language_selector, model_selector, threshold_slider,
|
299 |
input_image, button, output_image, detection_info],
|
300 |
queue=False
|
301 |
)
|
302 |
+
|
303 |
+
# Configurar el botón de detección
|
304 |
button.click(
|
305 |
fn=detect_objects,
|
306 |
inputs=[input_image, language_selector, model_selector, threshold_slider],
|
307 |
outputs=[output_image, detection_info]
|
308 |
)
|
309 |
+
|
310 |
return app
|
311 |
|
312 |
+
|
313 |
+
# Precargar modelo por defecto
|
314 |
load_model("DETR ResNet-50")
|
315 |
|
316 |
if __name__ == "__main__":
|
317 |
app = build_app()
|
318 |
+
app.launch()
|