multitracker.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. print('Select 3 tracking targets')
  8. cv.namedWindow("tracking")
  9. camera = cv.VideoCapture(sys.argv[1])
  10. tracker = cv.MultiTracker_create()
  11. init_once = False
  12. ok, image=camera.read()
  13. if not ok:
  14. print('Failed to read video')
  15. exit()
  16. bbox1 = cv.selectROI('tracking', image)
  17. bbox2 = cv.selectROI('tracking', image)
  18. bbox3 = cv.selectROI('tracking', image)
  19. while camera.isOpened():
  20. ok, image=camera.read()
  21. if not ok:
  22. print 'no image to read'
  23. break
  24. if not init_once:
  25. ok = tracker.add(cv.TrackerMIL_create(), image, bbox1)
  26. ok = tracker.add(cv.TrackerMIL_create(), image, bbox2)
  27. ok = tracker.add(cv.TrackerMIL_create(), image, bbox3)
  28. init_once = True
  29. ok, boxes = tracker.update(image)
  30. print ok, boxes
  31. for newbox in boxes:
  32. p1 = (int(newbox[0]), int(newbox[1]))
  33. p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
  34. cv.rectangle(image, p1, p2, (200,0,0))
  35. cv.imshow('tracking', image)
  36. k = cv.waitKey(1)
  37. if k == 27 : break # esc pressed