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
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
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
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: 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 = 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
|