camshift.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. '''
  3. Camshift tracker
  4. ================
  5. This is a demo that shows mean-shift based tracking
  6. You select a color objects such as your face and it tracks it.
  7. This reads from video camera (0 by default, or the camera number the user enters)
  8. http://www.robinhewitt.com/research/track/camshift.html
  9. Usage:
  10. ------
  11. camshift.py [<video source>]
  12. To initialize tracking, select the object with mouse
  13. Keys:
  14. -----
  15. ESC - exit
  16. b - toggle back-projected probability visualization
  17. '''
  18. # Python 2/3 compatibility
  19. from __future__ import print_function
  20. import sys
  21. PY3 = sys.version_info[0] == 3
  22. if PY3:
  23. xrange = range
  24. import numpy as np
  25. import cv2
  26. # local module
  27. import video
  28. class App(object):
  29. def __init__(self, video_src):
  30. self.cam = video.create_capture(video_src)
  31. ret, self.frame = self.cam.read()
  32. cv2.namedWindow('camshift')
  33. cv2.setMouseCallback('camshift', self.onmouse)
  34. self.selection = None
  35. self.drag_start = None
  36. self.tracking_state = 0
  37. self.show_backproj = False
  38. def onmouse(self, event, x, y, flags, param):
  39. x, y = np.int16([x, y]) # BUG
  40. if event == cv2.EVENT_LBUTTONDOWN:
  41. self.drag_start = (x, y)
  42. self.tracking_state = 0
  43. return
  44. if self.drag_start:
  45. if flags & cv2.EVENT_FLAG_LBUTTON:
  46. h, w = self.frame.shape[:2]
  47. xo, yo = self.drag_start
  48. x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y]))
  49. x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y]))
  50. self.selection = None
  51. if x1-x0 > 0 and y1-y0 > 0:
  52. self.selection = (x0, y0, x1, y1)
  53. else:
  54. self.drag_start = None
  55. if self.selection is not None:
  56. self.tracking_state = 1
  57. def show_hist(self):
  58. bin_count = self.hist.shape[0]
  59. bin_w = 24
  60. img = np.zeros((256, bin_count*bin_w, 3), np.uint8)
  61. for i in xrange(bin_count):
  62. h = int(self.hist[i])
  63. cv2.rectangle(img, (i*bin_w+2, 255), ((i+1)*bin_w-2, 255-h), (int(180.0*i/bin_count), 255, 255), -1)
  64. img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
  65. cv2.imshow('hist', img)
  66. def run(self):
  67. while True:
  68. ret, self.frame = self.cam.read()
  69. vis = self.frame.copy()
  70. hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
  71. mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
  72. if self.selection:
  73. x0, y0, x1, y1 = self.selection
  74. self.track_window = (x0, y0, x1-x0, y1-y0)
  75. hsv_roi = hsv[y0:y1, x0:x1]
  76. mask_roi = mask[y0:y1, x0:x1]
  77. hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
  78. cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
  79. self.hist = hist.reshape(-1)
  80. self.show_hist()
  81. vis_roi = vis[y0:y1, x0:x1]
  82. cv2.bitwise_not(vis_roi, vis_roi)
  83. vis[mask == 0] = 0
  84. if self.tracking_state == 1:
  85. self.selection = None
  86. prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
  87. prob &= mask
  88. term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
  89. track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)
  90. if self.show_backproj:
  91. vis[:] = prob[...,np.newaxis]
  92. try:
  93. cv2.ellipse(vis, track_box, (0, 0, 255), 2)
  94. except:
  95. print(track_box)
  96. cv2.imshow('camshift', vis)
  97. ch = 0xFF & cv2.waitKey(5)
  98. if ch == 27:
  99. break
  100. if ch == ord('b'):
  101. self.show_backproj = not self.show_backproj
  102. cv2.destroyAllWindows()
  103. if __name__ == '__main__':
  104. import sys
  105. try:
  106. video_src = sys.argv[1]
  107. except:
  108. video_src = 0
  109. print(__doc__)
  110. App(video_src).run()