Upload files to "/"
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
"""
|
||||
============================================================
|
||||
BASIC YOLO TASK 6 : ORIENTED BOUNDING BOX (OBB) DETECTION
|
||||
============================================================
|
||||
WHAT THIS FILE DOES:
|
||||
- Same idea as 01_detection.py, but instead of an axis-aligned
|
||||
rectangle (box that's always "straight", never rotated),
|
||||
it draws a ROTATED rectangle that hugs the object at
|
||||
whatever ANGLE it's actually lying at
|
||||
- Each detection returns 4 corner points + a rotation angle
|
||||
instead of just (x1, y1, x2, y2)
|
||||
|
||||
WHEN TO USE THIS:
|
||||
Use OBB (instead of plain detection) any time objects can
|
||||
appear at an angle and a straight box would either miss
|
||||
part of the object or include a lot of empty background —
|
||||
classic cases:
|
||||
- aerial / drone / satellite imagery (ships, planes,
|
||||
vehicles, storage tanks — the default OBB model here
|
||||
is trained on the DOTA aerial dataset)
|
||||
- top-down warehouse or parking-lot cameras where
|
||||
vehicles/pallets can be at any angle
|
||||
- conveyor-belt items that rotate as they move
|
||||
- text detection where lines of text are skewed
|
||||
|
||||
Do NOT use this for a normal front-facing camera looking at
|
||||
people/everyday objects (COCO classes) — the default OBB
|
||||
weights won't recognize those classes. Use 01_detection.py
|
||||
for that instead, unless you fine-tune OBB on your own data.
|
||||
|
||||
HOW IT DIFFERS FROM 01_detection.py:
|
||||
- results[0].boxes -> normal detection, (x1,y1,x2,y2)
|
||||
- results[0].obb -> OBB detection, 4 rotated corner points
|
||||
(xyxyxyxy) + angle (in radians)
|
||||
|
||||
Install deps:
|
||||
pip install ultralytics opencv-python numpy
|
||||
============================================================
|
||||
"""
|
||||
|
||||
import cv2
|
||||
import time
|
||||
import numpy as np
|
||||
from ultralytics import YOLO
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# SETTINGS
|
||||
# ------------------------------------------------------------
|
||||
CAMERA_INDEX = 0
|
||||
FRAME_W = 1280
|
||||
FRAME_H = 720
|
||||
CONF_THRESHOLD = 0.45
|
||||
MODEL_WEIGHTS = 'yolo26n-obb.pt' # '-obb' = oriented-box variant of YOLO26n
|
||||
# (pretrained on DOTA aerial-imagery classes,
|
||||
# e.g. plane, ship, storage-tank, vehicle...)
|
||||
|
||||
|
||||
def load_model():
|
||||
print("Loading YOLO26n-obb model...")
|
||||
model = YOLO(MODEL_WEIGHTS)
|
||||
return model
|
||||
|
||||
|
||||
def draw_obb(frame, results):
|
||||
"""Draw each rotated box (4-corner polygon) + class label."""
|
||||
result = results[0]
|
||||
names = result.names
|
||||
|
||||
if result.obb is None or len(result.obb) == 0:
|
||||
return frame, 0
|
||||
|
||||
# xyxyxyxy: (N, 4, 2) -> 4 corner points per detection, already
|
||||
# rotated to match the object's real orientation
|
||||
corners = result.obb.xyxyxyxy.cpu().numpy()
|
||||
confs = result.obb.conf.cpu().numpy()
|
||||
clss = result.obb.cls.cpu().numpy().astype(int)
|
||||
|
||||
for pts, conf, cls_id in zip(corners, confs, clss):
|
||||
pts_int = pts.astype(np.int32)
|
||||
|
||||
# draw the 4-sided rotated polygon instead of a straight rectangle
|
||||
cv2.polylines(frame, [pts_int], isClosed=True, color=(0, 255, 0), thickness=2)
|
||||
|
||||
# small dot at the first corner, just to visually anchor the label
|
||||
label = f"{names[cls_id]} {conf:.2f}"
|
||||
label_pos = tuple(pts_int[0])
|
||||
cv2.putText(frame, label, (label_pos[0], max(label_pos[1] - 8, 15)),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
||||
|
||||
return frame, len(clss)
|
||||
|
||||
|
||||
def run():
|
||||
model = load_model()
|
||||
cap = cv2.VideoCapture(CAMERA_INDEX)
|
||||
cap.set(cv2.CAP_PROP_FRAME_WIDTH, FRAME_W)
|
||||
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, FRAME_H)
|
||||
|
||||
if not cap.isOpened():
|
||||
print(f"ERROR: could not open camera {CAMERA_INDEX}")
|
||||
return
|
||||
|
||||
print("Running OBB detection — press Q to quit")
|
||||
print("NOTE: default weights are trained on aerial/top-down classes (DOTA),")
|
||||
print(" point the camera at a top-down / drone-style view or swap in")
|
||||
print(" your own fine-tuned '-obb' weights for other use cases.\n")
|
||||
|
||||
prev_t = time.time()
|
||||
|
||||
while True:
|
||||
ok, frame = cap.read()
|
||||
if not ok:
|
||||
continue
|
||||
|
||||
# ---- Core OBB call ----------------------------------------
|
||||
# Because MODEL_WEIGHTS points at an '-obb' checkpoint, output
|
||||
# includes .obb (rotated corner points) instead of plain .boxes
|
||||
results = model(frame, conf=CONF_THRESHOLD, verbose=False)
|
||||
|
||||
frame, count = draw_obb(frame, results)
|
||||
|
||||
now = time.time()
|
||||
fps = 1.0 / (now - prev_t) if now != prev_t else 0.0
|
||||
prev_t = now
|
||||
cv2.putText(frame, f"FPS: {fps:.1f} Objects: {count}",
|
||||
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
|
||||
|
||||
cv2.imshow("YOLO - OBB Detection", frame)
|
||||
if cv2.waitKey(1) & 0xFF in (ord('q'), ord('Q')):
|
||||
break
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
||||
Reference in New Issue
Block a user