这篇博文针对SDK教程中的第二部分cozmo_face进行简单介绍,如下:

face是cozmo显示的核心部分:

来学习一下,如何操作吧~


分为3个文件,如上图所示。


1. face image

cozmo面部显示png图片信息。

#!/usr/bin/env python3# Copyright (c) 2016 Anki, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the file LICENSE.txt or at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.'''Display images on Cozmo's face (oled screen)
'''import os
import sys
import timetry:from PIL import Image
except ImportError:sys.exit("Cannot import from PIL: Do `pip3 install --user Pillow` to install")import cozmodef get_in_position(robot: cozmo.robot.Robot):'''If necessary, Move Cozmo's Head and Lift to make it easy to see Cozmo's face'''if (robot.lift_height.distance_mm > 45) or (robot.head_angle.degrees < 40):with robot.perform_off_charger():lift_action = robot.set_lift_height(0.0, in_parallel=True)head_action = robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE,in_parallel=True)lift_action.wait_for_completed()head_action.wait_for_completed()def cozmo_program(robot: cozmo.robot.Robot):current_directory = os.path.dirname(os.path.realpath(__file__))get_in_position(robot)sdk_png = os.path.join(current_directory, "..", "..", "face_images", "cozmosdk.png")hello_png = os.path.join(current_directory, "..", "..", "face_images", "csdn_relay.png")# load some images and convert them for display cozmo's faceimage_settings = [(sdk_png, Image.BICUBIC),(hello_png, Image.NEAREST)]face_images = []for image_name, resampling_mode in image_settings:image = Image.open(image_name)# resize to fit on Cozmo's face screenresized_image = image.resize(cozmo.oled_face.dimensions(), resampling_mode)# convert the image to the format used by the oled screenface_image = cozmo.oled_face.convert_image_to_screen_data(resized_image,invert_image=True)face_images.append(face_image)# display each image on Cozmo's face for duration_s seconds (Note: this# is clamped at 30 seconds max within the engine to prevent burn-in)# repeat this num_loops timesnum_loops = 10duration_s = 2.0print("Press CTRL-C to quit (or wait %s seconds to complete)" % int(num_loops*duration_s) )for _ in range(num_loops):for image in face_images:robot.display_oled_face_image(image, duration_s * 1000.0)time.sleep(duration_s)# Cozmo is moved off his charger contacts by default at the start of any program.
# This is because not all motor movement is possible whilst drawing current from
# the charger. In cases where motor movement is not required, such as this example
# we can specify that Cozmo can stay on his charger at the start:
cozmo.robot.Robot.drive_off_charger_on_connect = Falsecozmo.run_program(cozmo_program)

2. face mirror

这是一个镜像功能,将摄像头看到的通过cozmo的face显示出来。

#!/usr/bin/env python3# Copyright (c) 2016 Anki, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the file LICENSE.txt or at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.'''Display Cozmo's camera feed back on his face (like a mirror)
'''import sys
import timetry:import numpy as np
except ImportError:sys.exit("Cannot import numpy: Do `pip3 install --user numpy` to install")try:from PIL import Image
except ImportError:sys.exit("Cannot import from PIL: Do `pip3 install --user Pillow` to install")import cozmodef get_in_position(robot: cozmo.robot.Robot):'''If necessary, Move Cozmo's Head and Lift to make it easy to see Cozmo's face.'''if (robot.lift_height.distance_mm > 45) or (robot.head_angle.degrees < 40):with robot.perform_off_charger():lift_action = robot.set_lift_height(0.0, in_parallel=True)head_action = robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE,in_parallel=True)lift_action.wait_for_completed()head_action.wait_for_completed()def calc_pixel_threshold(image: Image):'''Calculate a pixel threshold based on the image.Anything brighter than this will be shown on (light blue).Anything darker will be shown off (black).'''# Convert image to gray scalegrayscale_image = image.convert('L')# Calculate the mean (average) valuemean_value = np.mean(grayscale_image.getdata())return mean_valuedef cozmo_face_mirror(robot: cozmo.robot.Robot):'''Continuously display Cozmo's camera feed back on his face.'''robot.camera.image_stream_enabled = Trueget_in_position(robot)face_dimensions = cozmo.oled_face.SCREEN_WIDTH, cozmo.oled_face.SCREEN_HALF_HEIGHTprint("Press CTRL-C to quit")while True:duration_s = 0.1  # time to display each camera frame on Cozmo's facelatest_image = robot.world.latest_imageif latest_image is not None:# Scale the camera image down to fit on Cozmo's faceresized_image = latest_image.raw_image.resize(face_dimensions,Image.BICUBIC)# Flip the image left/right so it displays mirroredresized_image = resized_image.transpose(Image.FLIP_LEFT_RIGHT)# Calculate the pixel threshold for this image. This threshold# will define how bright a pixel needs to be in the source image# for it to be displayed as lit-up on Cozmo's face.pixel_threshold = calc_pixel_threshold(resized_image)# Convert the image to the format to display on Cozmo's face.screen_data = cozmo.oled_face.convert_image_to_screen_data(resized_image,pixel_threshold=pixel_threshold)# display the image on Cozmo's facerobot.display_oled_face_image(screen_data, duration_s * 1000.0)time.sleep(duration_s)cozmo.robot.Robot.drive_off_charger_on_connect = False  # Cozmo can stay on his charger for this example
cozmo.run_program(cozmo_face_mirror)


3. alarm clock

时钟,在face显示当前时间呢~

#!/usr/bin/env python3# Copyright (c) 2016 Anki, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the file LICENSE.txt or at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.'''Cozmo Alarm ClockUse Cozmo's face to display the current time
Play an alarm (Cozmo tells you to wake up) at a set timeNOTE: This is an example program. Anki takes no responsibility
if Cozmo fails to wake you up on time!
'''import datetime
import math
import sys
import timetry:from PIL import Image, ImageDraw, ImageFont
except ImportError:sys.exit("Cannot import from PIL. Do `pip3 install --user Pillow` to install")import cozmo#: bool: Set to True to display the clock as analog
#: (with a small digital readout below)
SHOW_ANALOG_CLOCK = Falsedef make_text_image(text_to_draw, x, y, font=None):'''Make a PIL.Image with the given text printed on itArgs:text_to_draw (string): the text to draw to the imagex (int): x pixel locationy (int): y pixel locationfont (PIL.ImageFont): the font to useReturns::class:(`PIL.Image.Image`): a PIL image with the text drawn on it'''# make a blank image for the text, initialized to opaque blacktext_image = Image.new('RGBA', cozmo.oled_face.dimensions(), (0, 0, 0, 255))# get a drawing contextdc = ImageDraw.Draw(text_image)# draw the textdc.text((x, y), text_to_draw, fill=(255, 255, 255, 255), font=font)return text_image# get a font - location depends on OS so try a couple of options
# failing that the default of None will just use a default font
_clock_font = None
try:_clock_font = ImageFont.truetype("arial.ttf", 20)
except IOError:try:_clock_font = ImageFont.truetype("/Library/Fonts/Arial.ttf", 20)except IOError:passdef draw_clock_hand(dc, cen_x, cen_y, circle_ratio, hand_length):'''Draw a single clock hand (hours, minutes or seconds)Args:dc: (:class:`PIL.ImageDraw.ImageDraw`): drawing context to usecen_x (float): x coordinate of center of handcen_y (float): y coordinate of center of handcircle_ratio (float): ratio (from 0.0 to 1.0) that hand has travelledhand_length (float): the length of the hand'''hand_angle = circle_ratio * math.pi * 2.0vec_x = hand_length * math.sin(hand_angle)vec_y = -hand_length * math.cos(hand_angle)# x_scalar doubles the x size to compensate for the interlacing# in y that would otherwise make the screen appear 2x tallx_scalar = 2.0# pointy end of handhand_end_x = int(cen_x + (x_scalar * vec_x))hand_end_y = int(cen_y + vec_y)# 2 points, perpendicular to the direction of the hand,# to give a triangle with some widthhand_width_ratio = 0.1hand_end_x2 = int(cen_x - ((x_scalar * vec_y) * hand_width_ratio))hand_end_y2 = int(cen_y + (vec_x * hand_width_ratio))hand_end_x3 = int(cen_x + ((x_scalar * vec_y) * hand_width_ratio))hand_end_y3 = int(cen_y - (vec_x * hand_width_ratio))dc.polygon([(hand_end_x, hand_end_y), (hand_end_x2, hand_end_y2),(hand_end_x3, hand_end_y3)], fill=(255, 255, 255, 255))def make_clock_image(current_time):'''Make a PIL.Image with the current time displayed on itArgs:text_to_draw (:class:`datetime.time`): the time to displayReturns::class:(`PIL.Image.Image`): a PIL image with the time displayed on it'''time_text = time.strftime("%I:%M:%S %p")if not SHOW_ANALOG_CLOCK:return make_text_image(time_text, 8, 6, _clock_font)# make a blank image for the text, initialized to opaque blackclock_image = Image.new('RGBA', cozmo.oled_face.dimensions(), (0, 0, 0, 255))# get a drawing contextdc = ImageDraw.Draw(clock_image)# calculate position of clock elementstext_height = 9screen_width, screen_height = cozmo.oled_face.dimensions()analog_width = screen_widthanalog_height = screen_height - text_heightcen_x = analog_width * 0.5cen_y = analog_height * 0.5# calculate size of clock handssec_hand_length = (analog_width if (analog_width < analog_height) else analog_height) * 0.5min_hand_length = 0.85 * sec_hand_lengthhour_hand_length = 0.7 * sec_hand_length# calculate rotation for each handsec_ratio = current_time.second / 60.0min_ratio = (current_time.minute + sec_ratio) / 60.0hour_ratio = (current_time.hour + min_ratio) / 12.0# draw the clock handsdraw_clock_hand(dc, cen_x, cen_y, hour_ratio, hour_hand_length)draw_clock_hand(dc, cen_x, cen_y, min_ratio, min_hand_length)draw_clock_hand(dc, cen_x, cen_y, sec_ratio, sec_hand_length)# draw the digital time_text at the bottomx = 32y = screen_height - text_heightdc.text((x, y), time_text, fill=(255, 255, 255, 255), font=None)return clock_imagedef convert_to_time_int(in_value, time_unit):'''Convert in_value to an int and ensure it is in the valid range for that time unit(e.g. 0..23 for hours)'''max_for_time_unit = {'hours': 23, 'minutes': 59, 'seconds': 59}max_val = max_for_time_unit[time_unit]try:int_val = int(in_value)except ValueError:raise ValueError("%s value '%s' is not an int" % (time_unit, in_value))if int_val < 0:raise ValueError("%s value %s is negative" % (time_unit, int_val))if int_val > max_val:raise ValueError("%s value %s exceeded %s" % (time_unit, int_val, max_val))return int_valdef extract_time_from_args():''' Extract a (24-hour-clock) user-specified time from the command-lineSupports colon and space separators - e.g. all 3 of "11 22 33", "11:22:33" and "11 22:33"would map to the same time.The seconds value is optional and defaults to 0 if not provided.'''# split sys.argv further for any args that contain a ":"split_time_args = []for i in range(1, len(sys.argv)):arg = sys.argv[i]split_args = arg.split(':')for split_arg in split_args:split_time_args.append(split_arg)if len(split_time_args) >= 2:try:hours = convert_to_time_int(split_time_args[0], 'hours')minutes = convert_to_time_int(split_time_args[1], 'minutes')seconds = 0if len(split_time_args) >= 3:seconds = convert_to_time_int(split_time_args[2], 'seconds')return datetime.time(hours, minutes, seconds)except ValueError as e:print("ValueError %s" % e)# Default to no alarmreturn Nonedef get_in_position(robot: cozmo.robot.Robot):'''If necessary, Move Cozmo's Head and Lift to make it easy to see Cozmo's face'''if (robot.lift_height.distance_mm > 45) or (robot.head_angle.degrees < 40):with robot.perform_off_charger():lift_action = robot.set_lift_height(0.0, in_parallel=True)head_action = robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE,in_parallel=True)lift_action.wait_for_completed()head_action.wait_for_completed()def alarm_clock(robot: cozmo.robot.Robot):'''The core of the alarm_clock program'''alarm_time = extract_time_from_args()if alarm_time:print("Alarm set for %s" % alarm_time)else:print("No Alarm time provided. Usage example: 'alarm_clock.py 17:23' to set alarm for 5:23 PM. (Input uses the 24-hour clock.)")print("Press CTRL-C to quit")get_in_position(robot)was_before_alarm_time = Falselast_displayed_time = Nonewhile True:# Check the current time, and see if it's time to play the alarmcurrent_time = datetime.datetime.now().time()do_alarm = Falseif alarm_time:is_before_alarm_time = current_time < alarm_timedo_alarm = was_before_alarm_time and not is_before_alarm_time  # did we just cross the alarm timewas_before_alarm_time = is_before_alarm_timeif do_alarm:# Cancel the latest image display action so that the alarm actions can playrobot.abort_all_actions()# Speak The Time (off the charger as it's an animation)with robot.perform_off_charger():short_time_string = str(current_time.hour) + ":" + str(current_time.minute)robot.say_text("Wake up lazy human! it's " + short_time_string).wait_for_completed()else:# See if the displayed time needs updatingif (last_displayed_time is None) or (current_time.second != last_displayed_time.second):# Create the updated image with this timeclock_image = make_clock_image(current_time)oled_face_data = cozmo.oled_face.convert_image_to_screen_data(clock_image)# display for 1 secondrobot.display_oled_face_image(oled_face_data, 1000.0)last_displayed_time = current_time# only sleep for a fraction of a second to ensure we update the seconds as soon as they changetime.sleep(0.1)cozmo.robot.Robot.drive_off_charger_on_connect = False  # Cozmo can stay on his charger for this example
cozmo.run_program(alarm_clock)

Fin


Cozmo人工智能机器人SDK使用笔记(2)-显示部分face相关推荐

  1. Cozmo人工智能机器人SDK使用笔记(1)-基础部分basics

    APP和SDK有对应关系 如(3.0.0和1.4.6)或(3.1.0和1.4.7).不严格对应,无法正常使用SDK. cozmosdk.anki.com/docs/ Cozmo SDK经常更新,以便提 ...

  2. Cozmo人工智能机器人SDK使用笔记(4)-任务部分cubes_and_objects

    先简单总结一下使用笔记1-3: 基础部分介绍了一些常用功能,比如运动控制.LED显示和扬声器交互等 人机接口显示部分--输出,cozmo面部显示屏输出一些基本信息 人机接口视觉部分--输入,cozmo ...

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

    关于机器人感知-视觉部分,有过一次公开分享,讲稿全文和视屏实录,参考如下CSDN链接: 机器人感知-视觉部分(Robotic Perception-Vision Section): https://b ...

  4. Cozmo人工智能机器人SDK使用笔记(6)-并行部分Parallel_Action

    Cozmo并行动作示例. 此示例演示如何并行(而不是按顺序)执行动作. import sys import timetry:from PIL import Image except ImportErr ...

  5. Cozmo人工智能机器人SDK使用笔记(5)-时序部分async_sync

    Cozmo首先寻找一个立方体. 找到立方体后,立方体的灯以循环方式绿色闪烁,然后等待轻敲立方体. 此时,程序分别为同步和异步两种类型,注意区分. 1. 同步 立方体闪烁同步示例 import asyn ...

  6. Vector人工智能机器人SDK使用笔记

    Cozmo是2016年推出的,2两年后的2018年Vector上市,具备语音助手和更多功能,元件数由300+升级到700+. Vector的SDK具体说明在:developer.anki.com/ve ...

  7. ROS2GO之手机连接Cozmo人工智能机器人玩具

    ROS2GO之手机连接Cozmo人工智能机器人玩具 参考anki的开发者页面,硬件基础: 1. Cozmo机器人:: 2. 兼容Cozmo APP的手机或平板:: 3. 运行Windows.MacOS ...

  8. Cozmo人工智能机器人玩具/教具完整版中文说明书和介绍(附应用下载链接)

    Cozmo(Anki)人工智能AI机器人Robot完整版中文说明书和介绍 (附应用下载链接)https://download.csdn.net/download/zhangrelay/10854427 ...

  9. 曾推出Anki Drive和Cozmo人工智能机器人的独角兽企业Anki谢幕

    Anki DRIVE | 极客东东 听说 Anki 出事了,即将在未来的一周关闭,公司近200名员工将获得一周薪酬补偿.很遗憾这家小林很欣赏的初创公司,前后融资多达2亿美元,其实他们已经很有钱了,但却 ...

最新文章

  1. boost库 tbb_boost库常用库介绍
  2. python 字典循环_Python字典遍历操作实例小结
  3. Metal之基本简介及常用组件说明
  4. 利用xcode6做出牛的一逼的计算器
  5. 3.1)深度学习笔记:机器学习策略(1)
  6. 图解Java中的18 把锁!
  7. 小马智行完成D轮融资首次交割 估值达85亿美元
  8. 【Elasticsearch】wildcard、fuzzy、regexp、prefix
  9. 关于python_关于Python,你必须知道的事!
  10. 多线程,异步委托,同步委托几种方式的区别
  11. java什么会引用传递_在java中为什么很多人说有值传递和引用传递?引用传递的本质...
  12. 【2019杭电多校第二场1009=HDU6599】I Love Palindrome String(回文树的fail树)
  13. windows下杀死关不掉的进程
  14. 1.0 信息化与信息系统
  15. 如何使用Charles进行APP抓包
  16. Linux 系统投屏显示
  17. 无法ping 对方计算机,ping通对方ip,却不能访问对方电脑?
  18. 迈阿密大学的计算机系咋样,迈阿密大学计算机科学专业
  19. 普通相机也能实现超级夜景?一种普适的图像防抖算法
  20. (R68s,R66s)OpenWRT设置ipv6网络(以光猫拨号场景为例)

热门文章

  1. 世界500强企业中的中国企业
  2. FileInputStream中read()及read(byte b[])的用法
  3. android旋转动画惯性,android 之旋转罗盘 风车 开发
  4. 写一首关于教育的诗,500字
  5. 仓库盘点,金蝶安卓盘点机PDA金蝶精斗云PDA轻松扫描商品条码盘点
  6. 计算机编程方式有哪些,如何自学编程?有什么方法
  7. 休闲游戏“停不下来”,欢迎拍砖^_^
  8. cadence line 删除_PCB中如何删除特定的线
  9. iccv2020论文汇总_【论文相关】历年CVPR、ICCV、ECCV论文合集下载
  10. WAF---Web应用防火墙 功能梳理