File size: 2,314 Bytes
c9f6507 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | """
Maritime_Custom - Example Inference Script
Repository: MuayThaiLegz/Maritime_Custom
"""
from ultralytics import YOLO
import cv2
# ============================================
# 1. BASIC INFERENCE
# ============================================
# Load model
model = YOLO('MuayThaiLegz/Maritime_Custom')
# Single image
results = model.predict(
source='maritime_image.jpg',
conf=0.25,
save=True
)
# ============================================
# 2. VIDEO PROCESSING
# ============================================
# Process video with streaming
results = model.predict(
source='port_surveillance.mp4',
conf=0.25,
stream=True,
save=True
)
for i, result in enumerate(results):
print(f"Frame {i}: {len(result.boxes)} vessels detected")
# ============================================
# 3. REAL-TIME WEBCAM
# ============================================
# Live detection
results = model.predict(
source=0, # Webcam
conf=0.25,
show=True,
stream=True
)
# ============================================
# 4. BATCH PROCESSING
# ============================================
# Process directory
results = model.predict(
source='maritime_images/',
conf=0.25,
save=True,
project='detections',
name='batch_run'
)
# ============================================
# 5. CUSTOM POST-PROCESSING
# ============================================
# Advanced detection with custom handling
results = model.predict('image.jpg', conf=0.25)
for result in results:
boxes = result.boxes
img = result.orig_img
for box in boxes:
# Extract info
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
conf = float(box.conf[0])
# Draw custom annotations
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(img, f'Vessel: {conf:.2%}', (int(x1), int(y1)-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imwrite('annotated.jpg', img)
# ============================================
# 6. EXPORT FOR PRODUCTION
# ============================================
# Export to ONNX
model.export(format='onnx', dynamic=True)
# Export to TensorRT (NVIDIA)
model.export(format='engine', device=0, half=True)
print("✅ Inference examples complete!")
|