tracker.py 898 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import numpy as np
  2. import cv2 as cv
  3. import sys
  4. if len(sys.argv) != 2:
  5. print('Input video name is missing')
  6. exit()
  7. cv.namedWindow("tracking")
  8. camera = cv.VideoCapture(sys.argv[1])
  9. ok, image=camera.read()
  10. if not ok:
  11. print('Failed to read video')
  12. exit()
  13. bbox = cv.selectROI("tracking", image)
  14. tracker = cv.TrackerMIL_create()
  15. init_once = False
  16. while camera.isOpened():
  17. ok, image=camera.read()
  18. if not ok:
  19. print 'no image to read'
  20. break
  21. if not init_once:
  22. ok = tracker.init(image, bbox)
  23. init_once = True
  24. ok, newbox = tracker.update(image)
  25. print ok, newbox
  26. if ok:
  27. p1 = (int(newbox[0]), int(newbox[1]))
  28. p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
  29. cv.rectangle(image, p1, p2, (200,0,0))
  30. cv.imshow("tracking", image)
  31. k = cv.waitKey(1) & 0xff
  32. if k == 27 : break # esc pressed