Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
base_model:
|
| 6 |
+
- qualcomm/RF-DETR
|
| 7 |
+
pipeline_tag: object-detection
|
| 8 |
+
tags:
|
| 9 |
+
- surveillance
|
| 10 |
+
- Threat_detection
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# Transformers for Object detection
|
| 14 |
+
|
| 15 |
+
### Inference Instructions
|
| 16 |
+
|
| 17 |
+
```python
|
| 18 |
+
import numpy as np
|
| 19 |
+
import supervision as sv
|
| 20 |
+
import torch
|
| 21 |
+
import requests
|
| 22 |
+
from PIL import Image
|
| 23 |
+
import os
|
| 24 |
+
|
| 25 |
+
from rfdetr import RFDETRNano
|
| 26 |
+
|
| 27 |
+
THREAT_CLASSES = {
|
| 28 |
+
1: "Gun",
|
| 29 |
+
2: "Explosive",
|
| 30 |
+
3: "Grenade",
|
| 31 |
+
4: "Knife"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
image = Image.open("Path_to_image")
|
| 35 |
+
|
| 36 |
+
# pre-trained weights
|
| 37 |
+
weights_url = "https://huggingface.co/Subh775/Threat-Detection-Rf-Detr-v2/resolve/main/checkpoint_best_total.pth"
|
| 38 |
+
weights_filename = "checkpoint_best_total.pth"
|
| 39 |
+
|
| 40 |
+
# Download weights if not already present
|
| 41 |
+
if not os.path.exists(weights_filename):
|
| 42 |
+
print(f"Downloading weights from {weights_url}")
|
| 43 |
+
response = requests.get(weights_url, stream=True)
|
| 44 |
+
response.raise_for_status()
|
| 45 |
+
with open(weights_filename, 'wb') as f:
|
| 46 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 47 |
+
f.write(chunk)
|
| 48 |
+
print("Download complete.")
|
| 49 |
+
|
| 50 |
+
model = RFDETRNano(resolution=640, pretrain_weights=weights_filename)
|
| 51 |
+
model.optimize_for_inference()
|
| 52 |
+
|
| 53 |
+
detections = model.predict(image, threshold=0.5)
|
| 54 |
+
|
| 55 |
+
color = sv.ColorPalette.from_hex([
|
| 56 |
+
"#1E90FF", "#32CD32", "#FF0000", "#FF8C00"
|
| 57 |
+
])
|
| 58 |
+
|
| 59 |
+
text_scale = sv.calculate_optimal_text_scale(resolution_wh=image.size)
|
| 60 |
+
thickness = sv.calculate_optimal_line_thickness(resolution_wh=image.size)
|
| 61 |
+
|
| 62 |
+
bbox_annotator = sv.BoxAnnotator(color=color, thickness=thickness)
|
| 63 |
+
label_annotator = sv.LabelAnnotator(
|
| 64 |
+
color=color,
|
| 65 |
+
text_color=sv.Color.BLACK,
|
| 66 |
+
text_scale=text_scale,
|
| 67 |
+
smart_position=True
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
labels = []
|
| 71 |
+
for class_id, confidence in zip(detections.class_id, detections.confidence):
|
| 72 |
+
class_name = THREAT_CLASSES.get(class_id, f"unknown_class_{class_id}")
|
| 73 |
+
labels.append(f"{class_name} {confidence:.2f}")
|
| 74 |
+
|
| 75 |
+
annotated_image = image.copy()
|
| 76 |
+
annotated_image = bbox_annotator.annotate(annotated_image, detections)
|
| 77 |
+
annotated_image = label_annotator.annotate(annotated_image, detections, labels)
|
| 78 |
+
annotated_image.thumbnail((800, 800))
|
| 79 |
+
annotated_image
|
| 80 |
+
```
|