Cozmo人工智能机器人SDK使用笔记(3)-视觉部分vision

举报
zhangrelay 发表于 2021/07/15 05:09:02 2021/07/15
【摘要】 关于机器人感知-视觉部分,有过一次公开分享,讲稿全文和视屏实录,参考如下CSDN链接: 机器人感知-视觉部分(Robotic Perception-Vision Section): https://blog.csdn.net/ZhangRelay/article/details/81352622 Cozmo视觉Vision也可以完成很多功能,宠物、方块、人脸等识别和跟...

关于机器人感知-视觉部分,有过一次公开分享,讲稿全文和视屏实录,参考如下CSDN链接:

机器人感知-视觉部分(Robotic Perception-Vision Section):

https://blog.csdn.net/ZhangRelay/article/details/81352622


Cozmo视觉Vision也可以完成很多功能,宠物、方块、人脸等识别和跟踪等,非常有趣。

中文

英文

这就是教程tutorials中第三部分vision中的内容。


1. light when face

当检测到人脸在图像中识别并点亮cozmo背部的LED灯。


  
  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. '''Wait for Cozmo to see a face, and then turn on his backpack light.
  16. This is a script to show off faces, and how they are easy to use.
  17. It waits for a face, and then will light up his backpack when that face is visible.
  18. '''
  19. import asyncio
  20. import time
  21. import cozmo
  22. def light_when_face(robot: cozmo.robot.Robot):
  23. '''The core of the light_when_face 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 = None
  28. print("Press CTRL-C to quit")
  29. while True:
  30. if face and face.is_visible:
  31. robot.set_all_backpack_lights(cozmo.lights.blue_light)
  32. else:
  33. robot.set_backpack_lights_off()
  34. # Wait until we we can see another face
  35. try:
  36. face = robot.world.wait_for_observed_face(timeout=30)
  37. except asyncio.TimeoutError:
  38. print("Didn't find a face.")
  39. return
  40. time.sleep(.1)
  41. cozmo.run_program(light_when_face, use_viewer=True, force_viewer_on_top=True)

2. face follower

识别人脸并跟随,控制头部角度和履带运动调整是人脸处于采集图像的中间位置(x,y两轴)。


  
  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)

3. annotate

此示例使用tkviewer在屏幕上显示带注释的摄像头图像并使用两种不同的方法添加了一些自己的自定义注释。


  
  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. '''Display a GUI window showing an annotated camera view.
  16. Note:
  17. This example requires Python to have Tkinter installed to display the GUI.
  18. It also requires the Pillow and numpy python packages to be pip installed.
  19. The :class:`cozmo.world.World` object collects raw images from Cozmo's camera
  20. and makes them available as a property (:attr:`~cozmo.world.World.latest_image`)
  21. and by generating :class:`cozmo.world.EvtNewCamerImages` events as they come in.
  22. Each image is an instance of :class:`cozmo.world.CameraImage` which provides
  23. access both to the raw camera image, and to a scalable annotated image which
  24. can show where Cozmo sees faces and objects, along with any other information
  25. your program may wish to display.
  26. This example uses the tkviewer to display the annotated camera on the screen
  27. and adds a couple of custom annotations of its own using two different methods.
  28. '''
  29. import sys
  30. import time
  31. try:
  32. from PIL import ImageDraw, ImageFont
  33. except ImportError:
  34. sys.exit('run `pip3 install --user Pillow numpy` to run this example')
  35. import cozmo
  36. # Define an annotator using the annotator decorator
  37. @cozmo.annotate.annotator
  38. def clock(image, scale, annotator=None, world=None, **kw):
  39. d = ImageDraw.Draw(image)
  40. bounds = (0, 0, image.width, image.height)
  41. text = cozmo.annotate.ImageText(time.strftime("%H:%m:%S"),
  42. position=cozmo.annotate.TOP_LEFT)
  43. text.render(d, bounds)
  44. # Define another decorator as a subclass of Annotator
  45. class Battery(cozmo.annotate.Annotator):
  46. def apply(self, image, scale):
  47. d = ImageDraw.Draw(image)
  48. bounds = (0, 0, image.width, image.height)
  49. batt = self.world.robot.battery_voltage
  50. text = cozmo.annotate.ImageText('BATT %.1fv' % batt, color='green')
  51. text.render(d, bounds)
  52. def cozmo_program(robot: cozmo.robot.Robot):
  53. robot.world.image_annotator.add_static_text('text', 'Coz-Cam', position=cozmo.annotate.TOP_RIGHT)
  54. robot.world.image_annotator.add_annotator('clock', clock)
  55. robot.world.image_annotator.add_annotator('battery', Battery)
  56. time.sleep(2)
  57. print("Turning off all annotations for 2 seconds")
  58. robot.world.image_annotator.annotation_enabled = False
  59. time.sleep(2)
  60. print('Re-enabling all annotations')
  61. robot.world.image_annotator.annotation_enabled = True
  62. # Disable the face annotator after 10 seconds
  63. time.sleep(10)
  64. print("Disabling face annotations (light cubes still annotated)")
  65. robot.world.image_annotator.disable_annotator('faces')
  66. # Shutdown the program after 100 seconds
  67. time.sleep(100)
  68. cozmo.run_program(cozmo_program, use_viewer=True, force_viewer_on_top=True)

4. exposure

此示例演示了使用自动曝光和手动曝光Cozmo的摄像头图像。当前的摄像头设置会叠加到PC上查看器窗口。


  
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017 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. '''Demonstrate the manual and auto exposure settings of Cozmo's camera.
  16. This example demonstrates the use of auto exposure and manual exposure for
  17. Cozmo's camera. The current camera settings are overlayed onto the camera
  18. viewer window.
  19. '''
  20. import sys
  21. import time
  22. try:
  23. from PIL import ImageDraw, ImageFont
  24. import numpy as np
  25. except ImportError:
  26. sys.exit('run `pip3 install --user Pillow numpy` to run this example')
  27. import cozmo
  28. # A global string value to display in the camera viewer window to make it more
  29. # obvious what the example program is currently doing.
  30. example_mode = ""
  31. # An annotator for live-display of all of the camera info on top of the camera
  32. # viewer window.
  33. @cozmo.annotate.annotator
  34. def camera_info(image, scale, annotator=None, world=None, **kw):
  35. d = ImageDraw.Draw(image)
  36. bounds = [3, 0, image.width, image.height]
  37. camera = world.robot.camera
  38. text_to_display = "Example Mode: " + example_mode + "\n\n"
  39. text_to_display += "Fixed Camera Settings (Calibrated for this Robot):\n\n"
  40. text_to_display += 'focal_length: %s\n' % camera.config.focal_length
  41. text_to_display += 'center: %s\n' % camera.config.center
  42. text_to_display += 'fov: <%.3f, %.3f> degrees\n' % (camera.config.fov_x.degrees,
  43. camera.config.fov_y.degrees)
  44. text_to_display += "\n"
  45. text_to_display += "Valid exposure and gain ranges:\n\n"
  46. text_to_display += 'exposure: %s..%s\n' % (camera.config.min_exposure_time_ms,
  47. camera.config.max_exposure_time_ms)
  48. text_to_display += 'gain: %.3f..%.3f\n' % (camera.config.min_gain,
  49. camera.config.max_gain)
  50. text_to_display += "\n"
  51. text_to_display += "Current settings:\n\n"
  52. text_to_display += 'Auto Exposure Enabled: %s\n' % camera.is_auto_exposure_enabled
  53. text_to_display += 'Exposure: %s ms\n' % camera.exposure_ms
  54. text_to_display += 'Gain: %.3f\n' % camera.gain
  55. color_mode_str = "Color" if camera.color_image_enabled else "Grayscale"
  56. text_to_display += 'Color Mode: %s\n' % color_mode_str
  57. text = cozmo.annotate.ImageText(text_to_display,
  58. position=cozmo.annotate.TOP_LEFT,
  59. line_spacing=2,
  60. color="white",
  61. outline_color="black", full_outline=True)
  62. text.render(d, bounds)
  63. def demo_camera_exposure(robot: cozmo.robot.Robot):
  64. global example_mode
  65. # Ensure camera is in auto exposure mode and demonstrate auto exposure for 5 seconds
  66. camera = robot.camera
  67. camera.enable_auto_exposure()
  68. example_mode = "Auto Exposure"
  69. time.sleep(5)
  70. # Demonstrate manual exposure, linearly increasing the exposure time, while
  71. # keeping the gain fixed at a medium value.
  72. example_mode = "Manual Exposure - Increasing Exposure, Fixed Gain"
  73. fixed_gain = (camera.config.min_gain + camera.config.max_gain) * 0.5
  74. for exposure in range(camera.config.min_exposure_time_ms, camera.config.max_exposure_time_ms+1, 1):
  75. camera.set_manual_exposure(exposure, fixed_gain)
  76. time.sleep(0.1)
  77. # Demonstrate manual exposure, linearly increasing the gain, while keeping
  78. # the exposure fixed at a relatively low value.
  79. example_mode = "Manual Exposure - Increasing Gain, Fixed Exposure"
  80. fixed_exposure_ms = 10
  81. for gain in np.arange(camera.config.min_gain, camera.config.max_gain, 0.05):
  82. camera.set_manual_exposure(fixed_exposure_ms, gain)
  83. time.sleep(0.1)
  84. # Switch back to auto exposure, demo for a final 5 seconds and then return
  85. camera.enable_auto_exposure()
  86. example_mode = "Mode: Auto Exposure"
  87. time.sleep(5)
  88. def cozmo_program(robot: cozmo.robot.Robot):
  89. robot.world.image_annotator.add_annotator('camera_info', camera_info)
  90. # Demo with default grayscale camera images
  91. robot.camera.color_image_enabled = False
  92. demo_camera_exposure(robot)
  93. # Demo with color camera images
  94. robot.camera.color_image_enabled = True
  95. demo_camera_exposure(robot)
  96. cozmo.robot.Robot.drive_off_charger_on_connect = False # Cozmo can stay on his charger for this example
  97. cozmo.run_program(cozmo_program, use_viewer=True, force_viewer_on_top=True)

Fin


 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/86621319

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。