Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,28 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 9 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
outputs = model(**inputs)
|
| 15 |
-
target_sizes = torch.tensor([image.size[::-1]])
|
| 16 |
-
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 17 |
-
|
| 18 |
-
detections = []
|
| 19 |
-
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 20 |
-
box = [round(i, 2) for i in box.tolist()]
|
| 21 |
-
detections.append(
|
| 22 |
-
f"Detected {model.config.id2label[label.item()]} with confidence "
|
| 23 |
-
f"{round(score.item(), 3)} at location {box}"
|
| 24 |
-
)
|
| 25 |
-
return "\n".join(detections)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
inputs="text", # Entrée: une URL d'image
|
| 31 |
-
outputs="text", # Sortie: liste des détections
|
| 32 |
-
description="Paste an image URL to detect objects."
|
| 33 |
-
)
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
# URL de l'image d'exemple
|
| 7 |
+
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 8 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 9 |
+
|
| 10 |
+
# Téléchargement du modèle sans la dépendance 'timm'
|
| 11 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 12 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 13 |
|
| 14 |
+
# Préparer l'image pour le modèle
|
| 15 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 16 |
+
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Post-traitement des résultats pour obtenir les boîtes englobantes et les labels
|
| 19 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 20 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Afficher les résultats
|
| 23 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 24 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 25 |
+
print(
|
| 26 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
| 27 |
+
f"{round(score.item(), 3)} at location {box}"
|
| 28 |
+
)
|