Skip to content

Instantly share code, notes, and snippets.

@rubenhorn
Created April 16, 2020 21:46
Show Gist options
  • Save rubenhorn/273fd4d241be5a3891e4cfd2c27525a0 to your computer and use it in GitHub Desktop.
Save rubenhorn/273fd4d241be5a3891e4cfd2c27525a0 to your computer and use it in GitHub Desktop.
Simple OpenCV object tracking demo
import cv2
cap = cv2.VideoCapture(0)
tracker = cv2.TrackerMOSSE_create()
roi = None
while True:
_, frame = cap.read()
if roi is not None:
success, box = tracker.update(frame)
if success:
x, y, w, h = [int(v) for v in box]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('webcam', frame)
key = cv2.waitKey(25) & 0xFF
if key == ord('s'):
roi = cv2.selectROI('webcam', frame, fromCenter=False, showCrosshair=True)
tracker.init(frame, roi)
elif key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment