Upload files to "/"
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
"""
|
||||
============================================================
|
||||
BASIC YOLO TASK 4 : POSE ESTIMATION
|
||||
============================================================
|
||||
WHAT THIS FILE DOES:
|
||||
- Detects people in the frame
|
||||
- PLUS: finds 17 body keypoints per person (nose, eyes,
|
||||
shoulders, elbows, wrists, hips, knees, ankles — the
|
||||
standard COCO keypoint set) and draws a skeleton by
|
||||
connecting them
|
||||
|
||||
WHEN TO USE THIS:
|
||||
Use pose estimation any time you care about body posture
|
||||
or movement, not just "a person is here" — e.g.
|
||||
- drowsiness / head-nod detection (head keypoint position)
|
||||
- fall detection (sudden change in body keypoint layout)
|
||||
- exercise form / rep counting
|
||||
- gesture recognition
|
||||
|
||||
Install deps:
|
||||
pip install ultralytics opencv-python
|
||||
============================================================
|
||||
"""
|
||||
|
||||
import cv2
|
||||
import time
|
||||
from ultralytics import YOLO
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# SETTINGS
|
||||
# ------------------------------------------------------------
|
||||
CAMERA_INDEX = 0
|
||||
FRAME_W = 1280
|
||||
FRAME_H = 720
|
||||
CONF_THRESHOLD = 0.45
|
||||
MODEL_WEIGHTS = 'yolo26n-pose.pt' # '-pose' = pose variant of YOLO26n
|
||||
KEYPOINT_CONF_MIN = 0.5 # skip drawing low-confidence keypoints
|
||||
|
||||
# COCO 17-keypoint skeleton: pairs of keypoint indices to connect with a line
|
||||
# 0=nose 1=L-eye 2=R-eye 3=L-ear 4=R-ear 5=L-shoulder 6=R-shoulder
|
||||
# 7=L-elbow 8=R-elbow 9=L-wrist 10=R-wrist 11=L-hip 12=R-hip
|
||||
# 13=L-knee 14=R-knee 15=L-ankle 16=R-ankle
|
||||
SKELETON = [
|
||||
(5, 6), (5, 7), (7, 9), (6, 8), (8, 10), # arms + shoulders
|
||||
(5, 11), (6, 12), (11, 12), # torso
|
||||
(11, 13), (13, 15), (12, 14), (14, 16), # legs
|
||||
(0, 1), (0, 2), (1, 3), (2, 4) # face
|
||||
]
|
||||
|
||||
|
||||
def load_model():
|
||||
print("Loading YOLO26n-pose model...")
|
||||
model = YOLO(MODEL_WEIGHTS)
|
||||
return model
|
||||
|
||||
|
||||
def draw_pose(frame, results):
|
||||
result = results[0]
|
||||
if result.keypoints is None:
|
||||
return frame
|
||||
|
||||
kpts_xy = result.keypoints.xy.cpu().numpy() # (num_people, 17, 2)
|
||||
kpts_conf = result.keypoints.conf.cpu().numpy() # (num_people, 17)
|
||||
|
||||
for person_kpts, person_conf in zip(kpts_xy, kpts_conf):
|
||||
# draw each keypoint as a dot
|
||||
for (x, y), c in zip(person_kpts, person_conf):
|
||||
if c < KEYPOINT_CONF_MIN:
|
||||
continue
|
||||
cv2.circle(frame, (int(x), int(y)), 4, (0, 255, 0), -1)
|
||||
|
||||
# draw skeleton lines between connected keypoints
|
||||
for a, b in SKELETON:
|
||||
if person_conf[a] < KEYPOINT_CONF_MIN or person_conf[b] < KEYPOINT_CONF_MIN:
|
||||
continue
|
||||
pa = tuple(map(int, person_kpts[a]))
|
||||
pb = tuple(map(int, person_kpts[b]))
|
||||
cv2.line(frame, pa, pb, (255, 150, 0), 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 pose estimation — press Q to quit")
|
||||
prev_t = time.time()
|
||||
|
||||
while True:
|
||||
ok, frame = cap.read()
|
||||
if not ok:
|
||||
continue
|
||||
|
||||
# ---- Core pose call ---------------------------------------
|
||||
# Because MODEL_WEIGHTS points at a '-pose' checkpoint, output
|
||||
# includes .keypoints (per-person joint coordinates) in
|
||||
# addition to .boxes
|
||||
results = model(frame, conf=CONF_THRESHOLD, verbose=False)
|
||||
|
||||
frame = draw_pose(frame, results)
|
||||
|
||||
now = time.time()
|
||||
fps = 1.0 / (now - prev_t) if now != prev_t else 0.0
|
||||
prev_t = now
|
||||
people = len(results[0].boxes)
|
||||
cv2.putText(frame, f"FPS: {fps:.1f} People: {people}",
|
||||
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
|
||||
|
||||
cv2.imshow("YOLO - Pose Estimation", 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