根据以下代码实现实验场景:起始位置放置“start”二维码及纸箱,小车识别二维码后,绕过纸箱,向前行进;小车行进中侧方放置各种颜色图片,小车边运动边识别;终点放置“stop”二维码,小车停止运动,返回到起始位置,并播报识别的颜色。
(1)基本任务1:小车识别二维码
小车通过摄像头获取图像数据,当屏幕中出现含有 start 字样的二维码时,小车开始行进,当屏幕中出现含有 stop 字样的二维码时,小车停止行进。
(2)基本任务2:小车自主行走
小车读取二维码内容为 start 时,小车向右开始运动,随后再向前运动,并旋转摄像头云台以对准小车侧面的各种颜色图片。
(3)基本任务3:颜色识别
小车在前进过程中,需要通过摄像头采集小车侧面的图片,识别位于小车侧面图片颜色,并记录下小车识别到的颜色。
(4)基本任务4:语音播报所识别的颜色
当小车行进时,记录下识别到的颜色,当图像中出现内容含有 stop 的二维码时,控制小车返回,并语音播报出识别到的颜色。给我一个Python代码使其能在jupyter环境中正常运行import time
import pygame
from aip import AipSpeech
#下面的key要换成自己的
""" 语音技术 APPID AK SK """
SpeechAPP_ID = '17852430'
SpeechAPI_KEY ='eGeO4iQGAjHCrzBTYd1uvTtf'
SpeechSECRET_KEY = 'Cn1EVsUngZDbRLv4OxAFrDHSo8PsvFVP'
#连接客户端
Speechclient = AipSpeech(SpeechAPP_ID, SpeechAPI_KEY, SpeechSECRET_KEY)
#语音播报初始化
pygame.mixer.init()
#语音合成
result = Speechclient.synthesis("请输入要播报的文字", 'zh', 1, {'spd': 2, 'vol': 2, 'per': 1})
#写文件然后播报
if not isinstance(result, dict):
with open('./01.mp3', 'wb') as f:
f.write(result)
pygame.mixer.init()
pygame.mixer.music.load('./01.mp3')
pygame.mixer.music.play()任务一 读取USB摄像头图像
import cv2
import ipywidgets.widgets as widgets
import threading
import time
#设置摄像头显示组件
image_widget = widgets.Image(format='jpeg', width=600, height=500)
display(image_widget) #显示摄像头组件
#bgr8转jpeg格式
import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
image = cv2.VideoCapture(0) #打开摄像头
# width=1280
# height=960
# cap.set(cv2.CAP_PROP_FRAME_WIDTH,width)#设置图像宽度
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT,height)#设置图像高度
image.set(3,600) #3 视频流中帧的宽度
image.set(4,500) #4 视频流中帧的高度
image.set(5, 30) #设置帧率
image.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
image.set(cv2.CAP_PROP_BRIGHTNESS, 40) #设置亮度 -64 - 64 0.0
image.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64 2.0
image.set(cv2.CAP_PROP_EXPOSURE, 156) #设置曝光值 1.0 - 5000 156.0
ret, frame = image.read() #读取摄像头数据
image_widget.value = bgr8_to_jpeg(frame)
while 1:
ret, frame = image.read()
image_widget.value = bgr8_to_jpeg(frame)
time.sleep(0.010)
image.release() #使用完成对象记住释放掉对象,不然下一个程序使用这个对象模块会被占用,导致无法使用
保存读取的摄像头图像:
任务二 树莓派颜色识别
#bgr8转jpeg格式
import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
#摄像头组件显示
import traitlets
import ipywidgets.widgets as widgets
import time
# 线程功能操作库
import threading
import inspect
import ctypes
origin_widget = widgets.Image(format='jpeg', width=320, height=240)
mask_widget = widgets.Image(format='jpeg',width=320, height=240)
result_widget = widgets.Image(format='jpeg',width=320, height=240)
# create a horizontal box container to place the image widget next to eachother
image_container = widgets.HBox([origin_widget, mask_widget, result_widget])
display(image_container)
#线程相关函数
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
#主进程函数
import cv2
import numpy as np
import ipywidgets.widgets as widgets
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
cap.set(5, 120) #设置帧率
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
# image.set(cv2.CAP_PROP_BRIGHTNESS, 40) #设置亮度 -64 - 64 0.0
# image.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64 2.0
# image.set(cv2.CAP_PROP_EXPOSURE, 156) #设置曝光值 1.0 - 5000 156.0
#默认选择红色的,想识别其他请注释下面红色区间代码,放开后面其他区间代码段
# 红色区间
color_lower = np.array([0, 43, 46])
color_upper = np.array([10, 255, 255])
# #绿色区间
# color_lower = np.array([35, 43, 46])
# color_upper = np.array([77, 255, 255])
# #蓝色区间
# color_lower=np.array([100, 43, 46])
# color_upper = np.array([124, 255, 255])
# #黄色区间
# color_lower = np.array([26, 43, 46])
# color_upper = np.array([34, 255, 255])
# #橙色区间
# color_lower = np.array([11, 43, 46])
# color_upper = np.array([25, 255, 255])
def Color_Recongnize():
while(1):
# get a frame and show 获取视频帧并转成HSV格式, 利用cvtColor()将BGR格式转成HSV格式,参数为cv2.COLOR_BGR2HSV。
ret, frame = cap.read()
#cv2.imshow('Capture', frame)
origin_widget.value = bgr8_to_jpeg(frame)
# 颜色空间转换
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# get mask 利用inRange()函数和HSV模型中蓝色范围的上下界获取mask,mask中原视频中的蓝色部分会被弄成白色,其他部分黑色。
mask = cv2.inRange(hsv, color_lower, color_upper)
#cv2.imshow('Mask', mask)
mask_widget.value = bgr8_to_jpeg(mask)
# detect blue 将mask于原视频帧进行按位与操作,则会把mask中的白色用真实的图像替换:
res = cv2.bitwise_and(frame, frame, mask=mask)
#cv2.imshow('Result', res)
result_widget.value = bgr8_to_jpeg(res)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
time.sleep(0.01)
cap.release()
#cv2.destroyAllWindows()
#启动进程
thread1 = threading.Thread(target=Color_Recongnize)
thread1.setDaemon(True)
thread1.start()
#结束进程,只有在结束时才需要执行此段代码
stop_thread(thread1)
保存颜色识别图像:
任务三 颜色识别与云台追踪
#底层驱动方法
from Raspblock import Raspblock
robot = Raspblock()
#bgr8转jpeg格式
import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
#显示摄像头组件
import cv2
import traitlets
import ipywidgets.widgets as widgets
from IPython.display import display
import time
# 线程功能操作库
import threading
import inspect
import ctypes
image_widget = widgets.Image(format='jpeg', width=300, height=300)
display(image_widget)
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
image = cv2.VideoCapture(0)
image.set(3, 320)
image.set(4, 240)
image.set(5, 120) #设置帧率
image.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
# image.set(cv2.CAP_PROP_BRIGHTNESS, 40) #设置亮度 -64 - 64 0.0
# image.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64 2.0
# image.set(cv2.CAP_PROP_EXPOSURE, 156) #设置曝光值 1.0 - 5000 156.0
ret, frame = image.read()
image_widget.value = bgr8_to_jpeg(frame)
global color_x, color_y, color_radius
color_x = color_y = color_radius = 0
global target_valuex
target_valuex = 1500
global target_valuey
target_valuey = 1500
global g_mode
g_mode = 0
## 创建存储HSV色域颜色分类数据的数组
import numpy as np
global color_lower
color_lower = np.array([156, 43, 46])
global color_upperv
color_upper = np.array([180, 255, 255])
## 创建PID控制实例
import PID
xservo_pid = PID.PositionalPID(1.1, 0.2, 0.8)
yservo_pid = PID.PositionalPID(0.8, 0.2, 0.8)
## 颜色选择按钮配置
Redbutton = widgets.Button(
value=False,
description='红色',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='uncheck' )
# 设置绿色按钮
Greenbutton = widgets.Button(
value=False,
description='绿色',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='uncheck' )
Bluebutton = widgets.Button(
value=False,
description='蓝色',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='uncheck' )
Yellowbutton = widgets.Button(
value=False,
description='黄色',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='uncheck' )
Orangebutton = widgets.Button(
value=False,
description='橙色',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='uncheck' )
Closebutton = widgets.Button(
value=False,
description='关闭',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='uncheck' )
output = widgets.Output()
display(Redbutton, Greenbutton, Bluebutton, Yellowbutton, Orangebutton, Closebutton, output)
def ALL_Uncheck():
Redbutton.icon = 'uncheck'
Greenbutton.icon = 'uncheck'
Bluebutton.icon = 'uncheck'
Yellowbutton.icon = 'uncheck'
Orangebutton.icon = 'uncheck'
def on_Redbutton_clicked(b):
global color_lower, color_upper, g_mode
global target_valuex, target_valuey
ALL_Uncheck()
b.icon = 'check'
color_lower = np.array([0, 43, 46])
color_upper = np.array([10, 255, 255])
target_valuex = target_valuey = 2048
robot.Servo_control(1000,1500)
g_mode = 1
with output:
print(g_mode)
print(color_lower)
print("RedButton clicked.")
def on_Greenbutton_clicked(b):
global color_lower, color_upper, g_mode
global target_valuex, target_valuey
ALL_Uncheck()
b.icon = 'check'
color_lower = np.array([35, 43, 46])
color_upper = np.array([77, 255, 255])
target_valuex = target_valuey = 2048
robot.Servo_control(1500,1500)
g_mode = 1
with output:
print("GreenButton clicked.")
def on_Bluebutton_clicked(b):
global color_lower, color_upper, g_mode
global target_valuex, target_valuey
ALL_Uncheck()
b.icon = 'check'
color_lower=np.array([100, 43, 46])
color_upper = np.array([124, 255, 255])
target_valuex = target_valuey = 2048
robot.Servo_control(1500,1500)
g_mode = 1
with output:
print("Bluebutton clicked.")
def on_Yellowbutton_clicked(b):
global color_lower, color_upper, g_mode
global target_valuex, target_valuey
ALL_Uncheck()
b.icon = 'check'
color_lower = np.array([26, 43, 46])
color_upper = np.array([34, 255, 255])
target_valuex = target_valuey = 150
robot.Servo_control(1500,1500)
g_mode = 1
with output:
print("Yellowbutton clicked.")
def on_Orangebutton_clicked(b):
global color_lower, color_upper, g_mode
global target_valuex, target_valuey
ALL_Uncheck()
b.icon = 'check'
color_lower = np.array([11, 43, 46])
color_upper = np.array([25, 255, 255])
target_valuex = target_valuey = 2048
robot.Servo_control(1500,1500)
g_mode = 1
with output:
print("Orangebutton clicked.")
def on_Closebutton_clicked(b):
global g_mode
ALL_Uncheck()
g_mode = 0
with output:
print("CloseButton clicked.")
Redbutton.on_click(on_Redbutton_clicked)
Greenbutton.on_click(on_Greenbutton_clicked)
Bluebutton.on_click(on_Bluebutton_clicked)
Yellowbutton.on_click(on_Yellowbutton_clicked)
Orangebutton.on_click(on_Orangebutton_clicked)
Closebutton.on_click(on_Closebutton_clicked)
## 启动进程
thread1 = threading.Thread(target=Color_track)
thread1.setDaemon(True)
thread1.start()
## 关闭进程
stop_thread(thread1)
保存图像处理的效果图:
任务一 树莓派二维码识别
#bgr8转jpeg格式
import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
# 导入库并显示摄像头显示组件
# import the necessary packages
#import simple_barcode_detection
import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar
from PIL import Image
import ipywidgets.widgets as widgets
image_widget = widgets.Image(format='jpeg', width=320, height=240)
display(image_widget) #显示摄像头组件
# 定义解析二维码接口
def decodeDisplay(image):
barcodes = pyzbar.decode(image)
for barcode in barcodes:
# 提取二维码的边界框的位置
# 画出图像中条形码的边界框
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (225, 225, 225), 2)
# 提取二维码数据为字节对象,所以如果想在输出图像上画出来,就需要先将它转换成字符串
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# 绘出图像上条形码的数据和条形码类型
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,0.5, (225, 225, 225), 2)
# 向终端打印条形码数据和条形码类型
print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
return image
def detect():
camera = cv2.VideoCapture(0)
camera.set(3, 320)
camera.set(4, 240)
camera.set(5, 120) #设置帧率
# fourcc = cv2.VideoWriter_fourcc(*"MPEG")
camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
camera.set(cv2.CAP_PROP_BRIGHTNESS, 40) #设置亮度 -64 - 64 0.0
camera.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64 2.0
camera.set(cv2.CAP_PROP_EXPOSURE, 156) #设置曝光值 1.0 - 5000 156.0
ret, frame = camera.read()
image_widget.value = bgr8_to_jpeg(frame)
while True:
# 读取当前帧
ret, frame = camera.read()
# 转为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
im = decodeDisplay(gray)
cv2.waitKey(5)
image_widget.value = bgr8_to_jpeg(im)
# 如果按键q则跳出本次循环
if cv2.waitKey(10) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
while 1:
detect()
任务二 树莓派二维码识别运动
#bgr8转jpeg格式
import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
# 导入库并显示摄像头显示组件
# import the necessary packages
#import simple_barcode_detection
import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar
from PIL import Image
import ipywidgets.widgets as widgets
#底层驱动方法
from Raspblock import Raspblock
robot = Raspblock()
image_widget = widgets.Image(format='jpeg', width=320, height=240)
display(image_widget) #显示摄像头组件
# 定义识别运动函数
def detect_control(info):
if info == "forward":
robot.Speed_axis_Yawhold_control(0, 2) #前进
elif info == "back":
robot.Speed_axis_Yawhold_control(0, -2) #后退
elif info == "left":
robot.Speed_axis_Yawhold_control(-2, 0) #左平移
elif info == "right":
robot.Speed_axis_Yawhold_control(2, 0) #右平移
else:
robot.Speed_axis_Yawhold_control(0, 0) #停车
# 定义解析二维码接口
def decodeDisplay(image):
barcodes = pyzbar.decode(image)
for barcode in barcodes:
# 提取二维码的边界框的位置
# 画出图像中条形码的边界框
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (225, 225, 225), 2)
# 提取二维码数据为字节对象,所以如果想在输出图像上
# 画出来,就需要先将它转换成字符串
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# 绘出图像上条形码的数据和条形码类型
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (225, 225, 225), 2)
# 向终端打印条形码数据和条形码类型
print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
detect_control(barcodeData)
return image
def detect():
camera = cv2.VideoCapture(0)
camera.set(3, 320)
camera.set(4, 240)
camera.set(5, 120) #设置帧率
# fourcc = cv2.VideoWriter_fourcc(*"MPEG")
camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
camera.set(cv2.CAP_PROP_BRIGHTNESS, 40) #设置亮度 -64 - 64 0.0
camera.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64 2.0
camera.set(cv2.CAP_PROP_EXPOSURE, 156) #设置曝光值 1.0 - 5000 156.0
ret, frame = camera.read()
image_widget.value = bgr8_to_jpeg(frame)
while True:
# 读取当前帧
ret, frame = camera.read()
# 转为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
im = decodeDisplay(gray)
cv2.waitKey(5)
image_widget.value = bgr8_to_jpeg(im)
# 如果按键q则跳出本次循环
if cv2.waitKey(10) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
while 1:
detect()
通过对forward、back、left、right、stop五个二维码的识别控制机器人运动。尝试自己生成一些带有运动指令的二维码,更改代码实现小车的其他运动。
保存小车识别二维码运动的结果视频:
下一节上一节
time.sleep(2)