JeanCGuerrero commited on
Commit
2851e9e
verified
1 Parent(s): 270075a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -42
app.py CHANGED
@@ -1,48 +1,20 @@
 
1
  import gradio as gr
2
- import torch
3
- from transformers import AutoModelForObjectDetection, AutoImageProcessor
4
- from PIL import Image, ImageDraw
5
 
6
- # Definir el repositorio en Hugging Face
7
- repo_id = "JeanCGuerrero/Practica2"
8
-
9
- # Cargar el modelo de Hugging Face
10
- model = AutoModelForObjectDetection.from_pretrained(repo_id)
11
- image_processor = AutoImageProcessor.from_pretrained(repo_id)
12
-
13
- # Funci贸n para la inferencia
14
- def predict(img):
15
- img = img.convert("RGB") # Asegurar que la imagen est茅 en formato RGB
16
- inputs = image_processor(images=img, return_tensors="pt")
17
-
18
- with torch.no_grad():
19
- outputs = model(**inputs)
20
 
21
- # Procesar los resultados
22
- target_sizes = torch.tensor([img.size[::-1]])
23
- results = image_processor.post_process_object_detection(outputs, threshold=0.5, target_sizes=target_sizes)[0]
24
 
25
- # Dibujar las detecciones en la imagen
26
- draw = ImageDraw.Draw(img)
27
- detecciones = []
28
-
29
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
30
- box = [round(i, 2) for i in box.tolist()]
31
- x, y, x2, y2 = box
32
- draw.rectangle([x, y, x2, y2], outline="red", width=3)
33
- class_name = f"Clase {label.item()} - Confianza: {round(score.item(), 2)}"
34
- draw.text((x, y), class_name, fill="red")
35
- detecciones.append(class_name)
36
-
37
- return img, "\n".join(detecciones)
38
 
39
- # Crear la interfaz y lanzarla con Gradio
40
- gr.Interface(
41
- fn=predict,
42
- inputs=gr.Image(type="pil"),
43
- outputs=[gr.Image(), gr.Text()],
44
- title="Detector de Mapaches 馃",
45
- description="Este modelo detecta mapaches en im谩genes usando DETR con el dataset Raccoon.",
46
- examples=['raccoon-108.jpg', 'raccoon-108.jpg']
47
- ).launch(share=False)
48
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_fastai
2
  import gradio as gr
3
+ from fastai.vision.all import *
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
 
 
 
6
 
7
+ # repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
8
+ repo_id = "JeanCGuerrero/Practica2"
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ learner = from_pretrained_fastai(repo_id)
11
+ labels = learner.dls.vocab
 
 
 
 
 
 
 
12
 
13
+ # Definimos una funci贸n que se encarga de llevar a cabo las predicciones
14
+ def predict(img):
15
+ #img = PILImage.create(img)
16
+ pred,pred_idx,probs = learner.predict(img)
17
+ return {labels[i]: float(probs[i]) for i in range(len(labels))}
18
+
19
+ # Creamos la interfaz y la lanzamos.
20
+ gr.Interface(fn=predict, inputs=gr.Image(), outputs=gr.Label(num_top_classes=3),examples=['raccoon-133.jpg','raccoon-108.jpg']).launch(share=False)