PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/demo_face.py

https://bitbucket.org/lilyzhang622/cozmo-demos
Python | 106 lines | 77 code | 16 blank | 13 comment | 18 complexity | 57be9f7b4007b6cf5626841a58255930 MD5 | raw file
  1. #!/usr/bin/env python3
  2. '''
  3. Cozmo will recognize new faces and old faces and try to follow the faces.
  4. Cozmo will display these on the screen.
  5. Currently, these actions are done on the same thread, which slows cozmo down.
  6. '''
  7. import sys
  8. import time
  9. import asyncio
  10. try:
  11. from PIL import ImageDraw, ImageFont
  12. except ImportError:
  13. sys.exit('run `pip3 install --user Pillow numpy` to run this example')
  14. import cozmo
  15. # Define an annotator using the annotator decorator
  16. @cozmo.annotate.annotator
  17. def clock(image, scale, annotator=None, world=None, **kw):
  18. d = ImageDraw.Draw(image)
  19. bounds = (0, 0, image.width, image.height)
  20. text = cozmo.annotate.ImageText(time.strftime("%H:%m:%S"),
  21. position=cozmo.annotate.TOP_LEFT)
  22. text.render(d, bounds)
  23. # Define another decorator as a subclass of Annotator
  24. class Battery(cozmo.annotate.Annotator):
  25. def apply(self, image, scale):
  26. d = ImageDraw.Draw(image)
  27. bounds = (0, 0, image.width, image.height)
  28. batt = self.world.robot.battery_voltage
  29. text = cozmo.annotate.ImageText('BATT %.1fv' % batt, color='green')
  30. text.render(d, bounds)
  31. def cozmo_program(robot: cozmo.robot.Robot):
  32. robot.world.image_annotator.add_static_text('text', 'Coz-Cam', position=cozmo.annotate.TOP_RIGHT)
  33. robot.world.image_annotator.add_annotator('clock', clock)
  34. robot.world.image_annotator.add_annotator('battery', Battery)
  35. robot.world.image_annotator.annotation_enabled = True
  36. # Move lift down and tilt the head up
  37. robot.move_lift(-3)
  38. robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed()
  39. faces = []
  40. current_face = None
  41. light_colors = [cozmo.lights.green_light,
  42. cozmo.lights.blue_light,
  43. cozmo.lights.red_light]
  44. print("Press CTRL-C to quit")
  45. while True:
  46. turn_action= None
  47. if len(faces) > 0:
  48. found_face = False
  49. for face, light in zip(faces, light_colors):
  50. if face and face.is_visible:
  51. found_face = True
  52. robot.set_all_backpack_lights(light)
  53. # start turning towards the face
  54. turn_action = robot.turn_towards_face(face)
  55. if not found_face:
  56. robot.set_backpack_lights_off()
  57. # Wait until we we can see another face
  58. try:
  59. face = None
  60. face = robot.world.wait_for_observed_face(timeout=30)
  61. if face in faces:
  62. robot.say_text("Hello again!").wait_for_completed()
  63. turn_action = robot.turn_towards_face(face)
  64. else:
  65. faces.append(face)
  66. robot.say_text("Hello New Person!").wait_for_completed()
  67. except asyncio.TimeoutError:
  68. print("Didn't find a face.")
  69. return
  70. else:
  71. robot.set_backpack_lights_off()
  72. # Wait until we we can see another face
  73. try:
  74. face = None
  75. face = robot.world.wait_for_observed_face(timeout=30)
  76. if face in faces:
  77. robot.say_text("Hello again!").wait_for_completed()
  78. turn_action = robot.turn_towards_face(face)
  79. else:
  80. faces.append(face)
  81. robot.say_text("Hello New Person!").wait_for_completed()
  82. except asyncio.TimeoutError:
  83. print("Didn't find a face.")
  84. return
  85. if turn_action:
  86. # Complete the turn action if one was in progress
  87. turn_action.wait_for_completed()
  88. time.sleep(.1)
  89. robot.world.image_annotator.disable_annotator('faces')
  90. cozmo.run_program(cozmo_program, use_viewer=True, force_viewer_on_top=True)