face_tracker.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. """ face_tracker.py - Version 1.1 2013-12-20
  3. Combines the OpenCV Haar face detector with Good Features to Track and Lucas-Kanade
  4. optical flow tracking.
  5. Created for the Pi Robot Project: http://www.pirobot.org
  6. Copyright (c) 2012 Patrick Goebel. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details at:
  15. http://www.gnu.org/licenses/gpl.html
  16. """
  17. import rospy
  18. import cv2
  19. import cv2.cv as cv
  20. import numpy as np
  21. from rbx1_vision.face_detector import FaceDetector
  22. from rbx1_vision.lk_tracker import LKTracker
  23. class FaceTracker(FaceDetector, LKTracker):
  24. def __init__(self, node_name):
  25. super(FaceTracker, self).__init__(node_name)
  26. self.n_faces = rospy.get_param("~n_faces", 1)
  27. self.show_text = rospy.get_param("~show_text", True)
  28. self.feature_size = rospy.get_param("~feature_size", 1)
  29. self.face_tracking = True
  30. self.keypoints = list()
  31. self.detect_box = None
  32. self.track_box = None
  33. self.grey = None
  34. self.prev_grey = None
  35. def process_image(self, cv_image):
  36. try:
  37. # Create a greyscale version of the image
  38. self.grey = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
  39. # Equalize the grey histogram to minimize lighting effects
  40. self.grey = cv2.equalizeHist(self.grey)
  41. # STEP 1: Detect the face if we haven't already
  42. if self.detect_box is None:
  43. self.detect_box = self.detect_face(self.grey)
  44. else:
  45. # STEP 2: If we aren't yet tracking keypoints, get them now
  46. if self.track_box is None or not self.is_rect_nonzero(self.track_box):
  47. self.track_box = self.detect_box
  48. self.keypoints = self.get_keypoints(self.grey, self.track_box)
  49. # STEP 3: If we have keypoints, track them using optical flow
  50. if len(self.keypoints) > 0:
  51. # Store a copy of the current grey image used for LK tracking
  52. if self.prev_grey is None:
  53. self.prev_grey = self.grey
  54. self.track_box = self.track_keypoints(self.grey, self.prev_grey)
  55. else:
  56. # We have lost all keypoints so re-detect he face
  57. self.detect_box = None
  58. # Process any special keyboard commands for this module
  59. if self.keystroke != -1:
  60. try:
  61. cc = chr(self.keystroke & 255).lower()
  62. if cc == 'c':
  63. self.keypoints = list()
  64. self.track_box = None
  65. self.detect_box = None
  66. except:
  67. pass
  68. # Set store a copy of the current image used for LK tracking
  69. self.prev_grey = self.grey
  70. except AttributeError:
  71. pass
  72. return cv_image
  73. if __name__ == '__main__':
  74. try:
  75. node_name = "face_tracker"
  76. FaceTracker(node_name)
  77. rospy.spin()
  78. except KeyboardInterrupt:
  79. print "Shutting down face tracker node."
  80. cv.DestroyAllWindows()