Upload files to "/"
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
|||||||
|
"""
|
||||||
|
============================================================
|
||||||
|
BASIC YOLO TASK 1 : OBJECT DETECTION
|
||||||
|
============================================================
|
||||||
|
WHAT THIS FILE DOES:
|
||||||
|
- Opens a webcam / camera feed
|
||||||
|
- Runs YOLO26n object detection on every frame
|
||||||
|
- Draws a bounding box + class name + confidence around
|
||||||
|
every object it finds (person, car, chair, etc. — 80
|
||||||
|
COCO classes)
|
||||||
|
- Shows live FPS on screen
|
||||||
|
|
||||||
|
WHEN TO USE THIS:
|
||||||
|
This is the "hello world" of YOLO — use it any time you
|
||||||
|
just need to know WHAT is in the frame and WHERE (as a
|
||||||
|
box), but you don't care about tracking the same object
|
||||||
|
across frames, its exact pixel shape, or its pose.
|
||||||
|
|
||||||
|
HOW IT DIFFERS FROM THE OTHER FILES IN THIS FOLDER:
|
||||||
|
- 02_tracking.py -> same as this, but also gives each
|
||||||
|
object a persistent ID across frames
|
||||||
|
- 03_segmentation.py -> gives the exact pixel outline of the
|
||||||
|
object, not just a box
|
||||||
|
- 04_pose_estimation.py-> finds body joints/keypoints (for people)
|
||||||
|
- 05_classification.py -> labels the WHOLE frame with one class,
|
||||||
|
no boxes at all
|
||||||
|
|
||||||
|
Install deps:
|
||||||
|
pip install ultralytics opencv-python
|
||||||
|
============================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import time
|
||||||
|
from ultralytics import YOLO
|
||||||
|
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
# SETTINGS — change these to tune behaviour
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
CAMERA_INDEX = 0 # which camera to open (0 = default webcam)
|
||||||
|
FRAME_W = 1280
|
||||||
|
FRAME_H = 720
|
||||||
|
CONF_THRESHOLD = 0.45 # ignore detections below this confidence
|
||||||
|
IOU_THRESHOLD = 0.45 # Non-Max-Suppression threshold (removes duplicate boxes)
|
||||||
|
MODEL_WEIGHTS = 'yolo26n.pt' # 'n' = nano = smallest/fastest, good for Jetson
|
||||||
|
|
||||||
|
|
||||||
|
def load_model():
|
||||||
|
"""Load the YOLO detection model once, and 'warm it up' with a
|
||||||
|
dummy image so the very first real frame isn't slow."""
|
||||||
|
print("Loading YOLO26n detection model...")
|
||||||
|
model = YOLO(MODEL_WEIGHTS)
|
||||||
|
import numpy as np
|
||||||
|
dummy = np.zeros((640, 640, 3), dtype='uint8')
|
||||||
|
model(dummy, verbose=False) # warm-up pass (builds GPU kernels etc.)
|
||||||
|
print("Model ready.")
|
||||||
|
return model
|
||||||
|
|
||||||
|
|
||||||
|
def draw_detections(frame, results, fps):
|
||||||
|
"""Draw a box + label for every detected object, plus an FPS counter."""
|
||||||
|
boxes = results[0].boxes # all detections in this frame
|
||||||
|
names = results[0].names # class-id -> class-name lookup
|
||||||
|
|
||||||
|
for box in boxes:
|
||||||
|
cls_id = int(box.cls[0]) # which class (0=person, 2=car, ...)
|
||||||
|
conf = float(box.conf[0]) # how confident the model is (0-1)
|
||||||
|
x1, y1, x2, y2 = map(int, box.xyxy[0]) # box corners in pixels
|
||||||
|
|
||||||
|
label = f"{names[cls_id]} {conf:.2f}"
|
||||||
|
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
||||||
|
cv2.putText(frame, label, (x1, y1 - 6),
|
||||||
|
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
||||||
|
|
||||||
|
cv2.putText(frame, f"FPS: {fps:.1f} Objects: {len(boxes)}",
|
||||||
|
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
|
||||||
|
return frame
|
||||||
|
|
||||||
|
|
||||||
|
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 detection — press Q to quit")
|
||||||
|
prev_t = time.time()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
ok, frame = cap.read()
|
||||||
|
if not ok:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# ---- Core detection call -------------------------------
|
||||||
|
# This one line is the whole "task": feed a frame in,
|
||||||
|
# get back boxes + classes + confidences.
|
||||||
|
results = model(frame, conf=CONF_THRESHOLD, iou=IOU_THRESHOLD, verbose=False)
|
||||||
|
|
||||||
|
# ---- FPS calc -------------------------------------------
|
||||||
|
now = time.time()
|
||||||
|
fps = 1.0 / (now - prev_t) if now != prev_t else 0.0
|
||||||
|
prev_t = now
|
||||||
|
|
||||||
|
frame = draw_detections(frame, results, fps)
|
||||||
|
cv2.imshow("YOLO - 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