Created
April 16, 2020 21:46
-
-
Save rubenhorn/273fd4d241be5a3891e4cfd2c27525a0 to your computer and use it in GitHub Desktop.
Simple OpenCV object tracking demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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