This program loads an image, applies a pretrained Faster R-CNN model to detect objects, and prints labels and confidence scores for detections above 50%.
import torch
from PIL import Image
import torchvision.transforms as T
import torchvision.models.detection as detection
# Load a sample image
img = Image.open("sample.jpg")
# Transform image to tensor
transform = T.Compose([T.ToTensor()])
img_tensor = transform(img)
# Load pretrained Faster R-CNN model
model = detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
# Run detection
with torch.no_grad():
predictions = model([img_tensor])
# Print detected labels and scores
labels = predictions[0]['labels']
scores = predictions[0]['scores']
print("Detected objects and scores:")
for label, score in zip(labels, scores):
if score > 0.5:
print(f"Label: {label.item()}, Score: {score.item():.2f}")