pynaoqi库的安装
下载文件
下载完成后直接打开文件,运行安装即可
在Choregraphe上进行python编程
在主程序区新建python盒子
通过python让Nao说话
在Choregraphe中写入这样一段代码
def onInput_onStart(self):
#self.onStopped() #activate the output of the box
tts = ALProxy("ALTextToSpeech") # 创建ALProxy中“ALTextToSpeech”的代理
tts.say("Hello") # 通过调用其中的.say()方法使机器人说话
self.onStopped()
通过python让Nao行走
def onInput_onStart(self):
#self.onStopped() #activate the output of the box
motion = ALProxy("ALMotion") # 创建"ALMotion"的代理
motion.moveTo(0.1, 0, 0) # 调用moveTo()方法,传递三个参数,分别为前进的距离,向左的距离,身体转的角度
self.onStopped()
在Pycharm中对nao进行控制
在成功安装naoqi库以后,我们就可以在Pycharm中控制nao机器人
通过代理建立连接
# 通过调用naoqi中的ALProxy类 建立代理连接
from naoqi import ALProxy
ALExpression = ALProxy("模块名称", robot_IP, port)
模块名称通过查阅API文档获得: naoAPI
通过调用模块中的方法来实现对机器人的控制
# 实现Nao机器人说话
robot_IP = "192.168.31.206" # 输入Nao的IP地址
port = 9559 # 端口号默认为9559
tts = ALProxy("ALTextToSpeech", robot_IP, port)
tts.say("Hello World")
通过pyCharm调用Nao机器人的摄像头进行图像识别
需要类库:opencv-python、numpy、pynaoqi
下面是调用小nao识别红球的代码
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
from naoqi import ALProxy
import vision_definitions
import math
port = 9559 # crf 机器人端口
robot_ip = "192.168.31.206" # crf 机器人IP
cameraProxy = ALProxy("ALVideoDevice", robot_ip, port)
# 基本参数
resolution = vision_definitions.kVGA
colorSpace = vision_definitions.kBGRColorSpace
fps =20
frameHeight = 0
frameWidth = 0
frameChannels = 0
frameArray = None
cameraPitchRange = 47.64/180*math.pi
cameraYawRange = 60.97/180*math.pi
# 获取图片
cameraProxy.setActiveCamera(1)
videoClient = cameraProxy.subscribe("python_GVM",resolution, colorSpace,fps)
frame = cameraProxy.getImageRemote(videoClient)
cameraProxy.unsubscribe(videoClient)
#读取图片
frameWidth = frame[0]
frameHeight = frame[1]
frameChannels = frame[2]
frameArray = np.frombuffer(frame[6], dtype=np.uint8).reshape([frameHeight, frameWidth, frameChannels])
# 转换为HSV
hue_image = cv2.cvtColor(frameArray, cv2.COLOR_BGR2HSV)
# 用颜色分割图像
low_range = np.array([160, 83, 100])
high_range = np.array([180, 255, 255])
th = cv2.inRange(hue_image, low_range, high_range)
cv2.imshow('result', th)
cv2.waitKey(0)
# 平滑处理
gaus=cv2.GaussianBlur(th,(7,7),1.5)
cv2.imshow('result', gaus)
cv2.waitKey(0)
# 腐蚀
eroded = cv2.erode(gaus, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 4)), iterations=2)
cv2.imshow('result', eroded)
cv2.waitKey(0)
# 膨胀
dilated = cv2.dilate(eroded, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
cv2.imshow('result', dilated)
cv2.waitKey(0)
# Hough Circle
circles = cv2.HoughCircles(dilated, cv.CV_HOUGH_GRADIENT, 1, 100, param1=15, param2=7, minRadius=15, maxRadius=100)
# 绘制
if circles is not None:
x, y, radius = circles[0][0]
center = (x, y)
cv2.circle(frameArray, center, radius, (0, 255, 0), 2)
cv2.imshow('result', frameArray)
cv2.waitKey(0)
cv2.destroyAllWindows()
Nao的语音识别
科大讯飞网站:
https://www.xfyun.cn/?ch=bdtg&b_scene_zt=1&renqun_youhua=646957
- 可通过下载科大讯飞语音识别代码 自行学习语音识别 通过阅读API文档 自行将程序与机器人相结合