/examples/tutorials/03_vision/02_face_follower.py

https://github.com/anki/cozmo-python-sdk · Python · 60 lines · 22 code · 14 blank · 24 comment · 7 complexity · 85c128fc5614d010a1001e89e0ddf0a9 MD5 · raw file

  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016 Anki, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License in the file LICENSE.txt or at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. '''Make Cozmo turn toward a face.
  16. This script shows off the turn_towards_face action. It will wait for a face
  17. and then constantly turn towards it to keep it in frame.
  18. '''
  19. import asyncio
  20. import time
  21. import cozmo
  22. def follow_faces(robot: cozmo.robot.Robot):
  23. '''The core of the follow_faces program'''
  24. # Move lift down and tilt the head up
  25. robot.move_lift(-3)
  26. robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed()
  27. face_to_follow = None
  28. print("Press CTRL-C to quit")
  29. while True:
  30. turn_action = None
  31. if face_to_follow:
  32. # start turning towards the face
  33. turn_action = robot.turn_towards_face(face_to_follow)
  34. if not (face_to_follow and face_to_follow.is_visible):
  35. # find a visible face, timeout if nothing found after a short while
  36. try:
  37. face_to_follow = robot.world.wait_for_observed_face(timeout=30)
  38. except asyncio.TimeoutError:
  39. print("Didn't find a face - exiting!")
  40. return
  41. if turn_action:
  42. # Complete the turn action if one was in progress
  43. turn_action.wait_for_completed()
  44. time.sleep(.1)
  45. cozmo.run_program(follow_faces, use_viewer=True, force_viewer_on_top=True)