seayala commited on
Commit
33754f1
verified
1 Parent(s): a4c4205

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -1,27 +1,45 @@
1
  import gradio as gr
2
  from transformers import AutoImageProcessor, AutoModelForObjectDetection, pipeline
3
- import torch
4
 
5
  # Carga el procesador de im谩genes y el modelo
6
- image_processor = AutoImageProcessor.from_pretrained("seayala/practica_2") # Reemplaza con la ruta de tu modelo
7
- model = AutoModelForObjectDetection.from_pretrained("seayala/practica_2") # Reemplaza con la ruta de tu modelo
8
 
9
  # Crea el pipeline de detecci贸n de objetos
10
  detector = pipeline("object-detection", model=model, image_processor=image_processor)
11
 
12
- # Crea la funci贸n de predicci贸n
13
  def predict(image):
14
- results = detector(image)
15
- return results
16
 
17
- # Crea la interfaz de usuario
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  iface = gr.Interface(
19
- fn=predict,
20
- inputs=gr.Image(type="pil"),
21
- outputs=gr.Label(num_top_classes=5),
22
- title="Detector de objetos",
23
- description="Sube una imagen para detectar objetos.",
24
- )
25
-
26
- # Inicia la interfaz de usuario
27
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import AutoImageProcessor, AutoModelForObjectDetection, pipeline
 
3
 
4
  # Carga el procesador de im谩genes y el modelo
5
+ image_processor = AutoImageProcessor.from_pretrained("seayala/practica_2")
6
+ model = AutoModelForObjectDetection.from_pretrained("seayala/practica_2")
7
 
8
  # Crea el pipeline de detecci贸n de objetos
9
  detector = pipeline("object-detection", model=model, image_processor=image_processor)
10
 
11
+ # Funci贸n para procesar la imagen y generar anotaciones
12
  def predict(image):
13
+ results = detector(image)
 
14
 
15
+ # Extrae cajas en formato xmin, ymin, xmax, ymax
16
+ boxes = []
17
+ for obj in results:
18
+ box = obj["box"]
19
+ label = f'{obj["label"]} ({obj["score"]:.2f})'
20
+ # Convierte el formato si es necesario
21
+ if "x" in box and "y" in box and "width" in box and "height" in box:
22
+ xmin = box["x"]
23
+ ymin = box["y"]
24
+ xmax = xmin + box["width"]
25
+ ymax = ymin + box["height"]
26
+ else:
27
+ xmin = box.get("xmin", 0)
28
+ ymin = box.get("ymin", 0)
29
+ xmax = box.get("xmax", 0)
30
+ ymax = box.get("ymax", 0)
31
+
32
+ boxes.append({"label": label, "box": [xmin, ymin, xmax, ymax]})
33
+
34
+ return image, boxes
35
+
36
+ # Interfaz Gradio
37
  iface = gr.Interface(
38
+ fn=predict,
39
+ inputs=gr.Image(type="pil", label="Sube una imagen"),
40
+ outputs=gr.AnnotatedImage(label="Resultados de detecci贸n"),
41
+ title="Detector de objetos",
42
+ description="Sube una imagen para detectar objetos con tu modelo personalizado."
43
+ )
44
+
45
+ iface.launch()