Skip to content
Snippets Groups Projects

initial commit

Merged Iwan Porojkow requested to merge master into main
12 files
+ 907
0
Compare changes
  • Side-by-side
  • Inline
Files
12
finetuning.py 0 → 100644
+ 25
0
from transformers import DetrImageProcessor, DetrForObjectDetection
import torch
from PIL import Image
import requests
url = r"C:\Users\Porojkow\Documents\Projekte\Dissertation\Entwurf\lookaround\11335454076423127576_1203855228_2.jpeg"
image = Image.open(url)
# you can specify the revision tag if you don't want the timm dependency
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
# convert outputs (bounding boxes and class logits) to COCO API
# let's only keep detections with score > 0.9
target_sizes = torch.tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
box = [round(i, 2) for i in box.tolist()]
print(
f"Detected {model.config.id2label[label.item()]} with confidence "
f"{round(score.item(), 3)} at location {box}"
\ No newline at end of file
Loading