实验五、利用智能小车探测环境

一、实验简介

在病毒环境、生化环境、爆炸环境、火灾环境等特殊环境下,人不能直接进入探测,考虑借助于智能设备完成探测环境、搜救、搬运等任务,考虑采用带摄像头的智能小车实现具体探测功能。

二、实验目的

通过本实验能够综合利用智能小车行走、避障、照相、识别的功能。

三、实验步骤

1. 实现智能小车在给定环境(场景)中运动,识别颜色,退回到原始位置,播报识别结果。

实验步骤

通过之前编写的二维码识别程序识别二维码并将指令传回小车并命令小车启动,在运动到指定位置小车停止,后再次使用二维码识别程序,并传回返回指令。

实验代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import cv2
import traitlets
import inspect
import ctypes
from PIL import Image
import pygame
from aip import AipSpeech
import numpy as np
from Raspblock import Raspblock
import ipywidgets.widgets as widgets
import matplotlib.pyplot as plt
%matplotlib inline

# 摄像头组件初始化
origin_widget = widgets.Image(format='jpeg', width=320, height=240)
display(origin_widget)
# 运动参数
run_time = 2500
turn_time = 1000
speed = 5

# HSV颜色阈值
yellow_lower = np.array([26, 43, 46])
yellow_upper = np.array([34, 255, 255])
red_lower = np.array([0, 127, 128])
red_upper = np.array([10, 255, 255])
orange_lower = np.array([11, 43, 46])
orange_upper = np.array([25, 255, 255])

# 语音播报
SpeechAPP_ID = '17852430'
SpeechAPI_KEY = 'eGeO4iQGAjHCrzBTYd1uvTtf'
SpeechSECRET_KEY = 'Cn1EVsUngZDbRLv4OxAFrDHSo8PsvFVP'

# 连接客户端
Speechclient = AipSpeech(SpeechAPP_ID, SpeechAPI_KEY, SpeechSECRET_KEY)

# 语音播报初始化
pygame.mixer.init()


# 小车运动
class Sport:
def Run(s, t): # 直行
robot = Raspblock()
for i in range(0, t):
robot.Speed_Wheel_control(s, s, s, s)
del robot

def Turn(a1, a2, a3, a4, t): # 转弯
robot = Raspblock()
for i in range(0, t):
robot.Speed_Wheel_control(a1, a2, a3, a4)
del robot


# X:2500正前 1500正侧面 500正后 Z:1950最低 1500水平 500最高
class Camera:
def X_Z(x, z):
robot = Raspblock()
robot.Servo_control(x, z)
del robot

def X(x):
robot = Raspblock()
robot.Servo_control_single(1, x)
del robot

def Z(z):
robot = Raspblock()
robot.Servo_control_single(2, z)
del robot


# bgr8转jpeg格式
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])


# 线程相关函数
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)


class Video:
def Color_Recongnize(camera_id):
while True:
cap = cv2.VideoCapture(camera_id)
cap.set(3, 640)
cap.set(4, 480)
cap.set(5, 120)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('F', 'L', 'V', '1'))

ret, frame = cap.read()
origin_widget.value = bgr8_to_jpeg(frame)
# 转换为HSV格式
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

mask_yellow = cv2.inRange(hsv, yellow_lower, yellow_upper)
mask_red = cv2.inRange(hsv, red_lower, red_upper)
mask_orange = cv2.inRange(hsv, orange_lower, orange_upper)
# 中值滤波
mask_yellow = cv2.medianBlur(mask_yellow, 7)
mask_red = cv2.medianBlur(mask_red, 7)
mask_orange = cv2.medianBlur(mask_orange, 7)
# 提取轮廓
mask_yellow, contours_1 = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
mask_red, contours_2 = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
mask_orange, contours_3 = cv2.findContours(mask_orange, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 遍历颜色轮廓
if mask_yellow is not None:
result = Speechclient.synthesis("黄色", 'zh', 1, {'spd': 2, 'vol': 5, 'per': 4})
open('./01.mp3', 'wb').write(result)
pygame.mixer.init()
pygame.mixer.music.load('./01.mp3')
pygame.mixer.music.play()
print("yellow")
if mask_red is not None:
result = Speechclient.synthesis("红色", 'zh', 1, {'spd': 2, 'vol': 5, 'per': 4})
open('./01.mp3', 'wb').write(result)
pygame.mixer.init()
pygame.mixer.music.load('./01.mp3')
pygame.mixer.music.play()
print("red")
if mask_orange is not None:
result = Speechclient.synthesis("橙色", 'zh', 1, {'spd': 2, 'vol': 5, 'per': 4})
open('./01.mp3', 'wb').write(result)
pygame.mixer.init()
pygame.mixer.music.load('./01.mp3')
pygame.mixer.music.play()
print("orange")
plt.imshow(frame)
cap.release()

def Qr_Recongnize(camera_id):
while True:
cap = cv2.VideoCapture(camera_id)
cap.set(3, 640)
cap.set(4, 480)
cap.set(5, 1)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('F', 'L', 'V', '1'))
ret, frame = cap.read()
origin_widget.value = bgr8_to_jpeg(frame)
src_image = frame
qrcoder = cv2.QRCodeDetector()
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(src_image)
print(codeinfo)
cap.release()
return codeinfo


while 1:
Camera.X(500)
out_1 = Video.Qr_Recongnize(0)
if out_1 == 'begin':
Sport.Turn(5, 5, -5, -5, turn_time) # 左旋
Sport.Run(speed, run_time)
Sport.Turn(-5, -5, 5, 5, turn_time) # 右旋
Camera.X(1500)
Sport.Run(-speed, 15000)
while 1:
out_2 = Video.Qr_Recongnize(0)
if out_2 == 'stop':
Sport.Run(speed, 15000)
Sport.Turn(-5, -5, 5, 5, turn_time) # 右旋
Sport.Run(speed, run_time)
Sport.Turn(5, 5, -5, -5, turn_time) # 左旋
break

四、思考分析

  1. 对于多流程同时进行的程序无法熟练运用。
  2. 成功实现了将二维码识别和小车运动循环结合在了一起。

实验五、利用智能小车探测环境
https://flowerdown.org/posts/20221022-210932.html
作者
Unrealfeather
发布于
2022年10月22日
许可协议