Views
No views yet
| ID | Class |
|---|---|
| 0 | Apple |
| 1 | Cherry |
| 2 | Figs |
| 3 | Olive |
| 4 | Pomegranate |
| 5 | Orange |
| 6 | Rockmelon |
| 7 | Strawberry |
| 8 | Potato |
| 9 | Tomato |
| 10 | Watermelon |
| 11 | Bell-pepper |
1from transformers import AutoImageProcessor, AutoModelForObjectDetection
2from PIL import Image
3import torch
4
5# Load model and processor
6processor = AutoImageProcessor.from_pretrained("MohamedKhayat/fruit-detector-rtdetrv2-50")
7model = AutoModelForObjectDetection.from_pretrained("MohamedKhayat/fruit-detector-rtdetrv2-50")
8
9# Load and process image
10image = Image.open("fruit_image.jpg")
11inputs = processor(images=image, return_tensors="pt")
12
13# Run inference
14with torch.no_grad():
15 outputs = model(**inputs)
16
17# Post-process results
18target_sizes = torch.tensor([[image.height, image.width]])
19results = processor.post_process_object_detection(
20 outputs,
21 threshold=0.5,
22 target_sizes=target_sizes
23)[0]
24
25for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
26 box = box.tolist()
27 print(f"Detected {model.config.id2label[label.item()]} with confidence {score:.2f} at {box}")