Bashu333 commited on
Commit
04ffbc2
·
1 Parent(s): 4c22e88

Create MAin

Browse files
Files changed (1) hide show
  1. MAin +25 -0
MAin ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoImageProcessor, DeformableDetrForObjectDetection
2
+ import torch
3
+ from PIL import Image
4
+ import requests
5
+
6
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
7
+ image = Image.open(requests.get(url, stream=True).raw)
8
+
9
+ processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr")
10
+ model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr")
11
+
12
+ inputs = processor(images=image, return_tensors="pt")
13
+ outputs = model(**inputs)
14
+
15
+ # convert outputs (bounding boxes and class logits) to COCO API
16
+ # let's only keep detections with score > 0.7
17
+ target_sizes = torch.tensor([image.size[::-1]])
18
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
19
+
20
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
21
+ box = [round(i, 2) for i in box.tolist()]
22
+ print(
23
+ f"Detected {model.config.id2label[label.item()]} with confidence "
24
+ f"{round(score.item(), 3)} at location {box}"
25
+ )