-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabeling.py
More file actions
21 lines (16 loc) · 773 Bytes
/
Copy pathlabeling.py
File metadata and controls
21 lines (16 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from ultralytics import YOLO
import cv2
model = YOLO("runs/detect/train3/weights/best.pt") # Load trained model
image_path = "new_images/"
output_labels = "auto_labels/" # Directory to save labels
import os
os.makedirs(output_labels, exist_ok=True)
for img_name in os.listdir(image_path):
img_path = os.path.join(image_path, img_name)
results = model(img_path)
# Save auto-generated labels
with open(os.path.join(output_labels, img_name.replace(".jpg", ".txt")), "w") as f:
for r in results[0].boxes.data.tolist():
x_center, y_center, width, height, conf, cls = r # Get bbox details
if conf > 0.3: # Confidence threshold to avoid noise
f.write(f"{int(cls)} {x_center} {y_center} {width} {height}\n")