• v831
import time, math

from maix import nn, camera, display, image
import serial class YOLOv2:
def __init__(self, model_path, labels, anchors, net_in_size, net_out_size):
self.labels = labels
self.anchors = anchors
self.net_in_size = net_in_size
self.net_out_size = net_out_size
print("-- load model:", model_path)
self.model = nn.load(model_path)
print("-- load ok")
print("-- init yolo2 decoder")
self._decoder = nn.decoder.Yolo2(len(labels), anchors, net_in_size=net_in_size, net_out_size=net_out_size)
print("-- init complete") def run(self, img, nms=0.3, threshold=0.5):
out = self.model.forward(img, layout="hwc")
boxes, probs = self._decoder.run(out, nms=nms, threshold=threshold, img_size=self.net_in_size)
return boxes, probs def draw(self, img, boxes, probs):
for i, box in enumerate(boxes):
class_id = probs[i][0]
prob = probs[i][1][class_id]
msg = "{}:{:.2f}%".format(self.labels[class_id], prob * 100)
img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3], color=(255, 255, 255), thickness=2)
img.draw_string(box[0] + 2, box[1] + 2, msg, scale=1.2, color=(255, 255, 255), thickness=2) class RobotControl(object):
def __init__(self):
self.img_code_length = 560 # 640
self.img_barr_length = 120
# self.red_threshold = [(2, 95, -51, 20, -33, 24)]
self.red_threshold = [(0, 95, -90, 20, -66, 24)]
self.img = None # 存图片
camera.config(size=(224, 224))
# 通信
self.ser = serial.Serial("/dev/ttyS1", 19200, timeout=1)
# YOLO模型基本参数
self.input_size = (224, 224)
# self.model = "model-88559.awnn.mud"
# self.model = "/mnt/UDISK/app/model-88559.awnn.mud" # 要改
self.model = "model-88559.awnn.mud" if __name__ == '__main__' else "/mnt/UDISK/app/model-88559.awnn.mud" # 三目运算符取模型参数
self.labels = ['code']
self.anchors = [1.44, 0.97, 1.47, 1.16, 1.62, 1.31, 1.25, 0.94, 1.81, 1.53]
# 初始化YOLOv2模型
self.yolov2 = YOLOv2(self.model, self.labels, self.anchors, self.input_size,
(self.input_size[0] // 32, self.input_size[1] // 32))
# yolo检测出东西后的值与标志位
self.is_detect = False
# v831模式切换
self.mode = 0 # 0 YOLO检测 1 二维码检测 2 障碍物检测
self.need_change_mode = 0 # 需要变换模式的标志位
self.y_low = 100 # 障碍物靠近的距离
self.thou_pixels = 1000 # 障碍物的像素点,还未具体测量 def send_to_arduino(self, mes):
print("send_message:", mes) # 调试用
send_str = "{" + str(mes) + "&}"
self.ser.write(send_str.encode("utf-8")) def recv_from_arduino(self):
recv = self.ser.read()
if recv:
return recv.decode("utf-8")
else:
return None def change_mode_qrcode(self):
camera.config(size=(self.img_code_length, self.img_code_length))
print("mode_change_success! qrcode") def change_mode_barr(self):
camera.config(size=(self.img_barr_length, self.img_barr_length))
print("mode_change_success! barr") def change_mode_yolo(self):
camera.config(size=self.input_size)
print("mode_change_success! yolo") def test_yolo(self):
self.change_mode_yolo()
while 1:
img = camera.capture()
boxes, probs = self.yolov2.run(img, nms=0.3, threshold=0.5)
# boxes: [[46, 156, 46, 31], [45, 124, 51, 41], [0, 56, 57, 48]] x, y, w, h
self.yolov2.draw(img, boxes, probs)
# 放在机器人上后,middle_y越小二维码越靠近机器人
if boxes:
boxes = boxes[0]
middle_y = boxes[1] + boxes[3] // 2
middle_x = boxes[0] + boxes[2] // 2 # 这个值越大二维码在越左边
print(middle_y, middle_x)
display.show(img) def test_qrcode(self):
self.change_mode_qrcode()
while 1:
img = camera.capture()
mks = img.find_qrcodes()
for mk in mks:
# 二维码信息
string = mk['payload']
# 内框数据
x1, y1 = mk['corners'][0] # 访问字典的列表
x2, y2 = mk['corners'][1]
x3, y3 = mk['corners'][2]
x4, y4 = mk['corners'][3]
# 画内框
img.draw_line(x1, y1, x2, y2, color=(0, 255, 0), thickness=3)
img.draw_line(x2, y2, x3, y3, color=(0, 255, 0), thickness=3)
img.draw_line(x3, y3, x4, y4, color=(0, 255, 0), thickness=3)
img.draw_line(x4, y4, x1, y1, color=(0, 255, 0), thickness=3)
display.show(img) def test_barr(self):
self.change_mode_barr()
while 1:
img = camera.capture()
blobs = img.find_blobs(self.red_threshold, merge=True, invert=True)
most_pixels = 0
max_x = 0
# print("blobs:", blobs)
"""
{'x': 0, 'y': 0, 'w': 50, 'h': 177, 'pixels': 6343,
'centroid_x': 18.807661056518555, 'centroid_y': 76.3976058959961, 'rotation': 1.645219087600708,
'code': 1, 'count': 1}
"""
if blobs:
for n in range(len(blobs)):
if blobs[n]["pixels"] > most_pixels:
most_pixels = blobs[n]["pixels"]
max_x = n
i = blobs[max_x]
img.draw_rectangle(i["x"], i["y"], i["x"] + i["w"], i["y"] + i["h"],
color=(0, 0, 255), thickness=1) # 将找到的颜色区域画出来
cx = i["x"] + i["w"] // 2
cy = i["y"] + i["h"] // 2
# pixels = i["pixels"]
rotation = i["rotation"]
rotation = math.degrees(rotation) - 90
print(cx, cy, rotation, pixels)
display.show(img) # 此函数专门用于找二维码并返回二维码的中点值
def yolo_find(self) -> int:
boxes, probs = self.yolov2.run(self.img, nms=0.3, threshold=0.5)
self.yolov2.draw(self.img, boxes, probs)
if boxes:
boxes = boxes
# middle_y = boxes[1] + boxes[3]//2
return boxes[1] + boxes[3] // 2
else:
return 0 # 当获取到二维码位置后与mega的状态通信
def yolo_chat_arduino(self):
while 1:
recv = self.recv_from_arduino()
self.img = camera.capture()
if recv:
print("yolo获取的信息为:", recv) # 调试用
if recv == "b":
self.need_change_mode = 1
self.mode = 1
break
elif recv == "f":
self.img = camera.capture()
middle_y = self.yolo_find()
self.send_to_arduino("a" + str(middle_y)) def run(self):
while 1:
if self.mode == 0:
self.change_mode_yolo()
while 1:
self.img = camera.capture()
middle_y = self.yolo_find()
display.show(self.img)
if middle_y:
self.yolo_chat_arduino() # 将整个流程装到一个函数,不做复用,这样写方便状态机推进,不容易乱
def just_run(self):
# 第一阶段,先找二维码
print("second one!")
self.change_mode_yolo()
# is_true = 0
while 1:
self.img = camera.capture()
boxes, probs = self.yolov2.run(self.img, nms=0.3, threshold=0.5)
self.yolov2.draw(self.img, boxes, probs)
display.show(self.img)
# recv = self.recv_from_arduino()
# if recv:
# # is_true = 1
# break
if boxes:
boxes = boxes[0]
middle_y = boxes[1] + boxes[3]//2
if middle_y <= 135:
self.send_to_arduino("a"+str(middle_y)) # 找到二维码后发送找到的标志位
break # 特殊情况直接进行其他判断
# if is_true:
# while 1:
# self.img = camera.capture()
# display.show(self.img)
# recv = self.recv_from_arduino()
# if recv:
# print("recv:", recv)
# if recv == 's':
# time.sleep(2) # 停两秒
# self.img = camera.capture()
# mks = self.img.find_qrcodes()
# if mks:
# mk = mks[0]
# # 二维码信息
# string = mk['payload']
# self.send_to_arduino("c" + string)
# # 内框数据
# x1, y1 = mk['corners'][0] # 访问字典的列表
# x2, y2 = mk['corners'][1]
# x3, y3 = mk['corners'][2]
# x4, y4 = mk['corners'][3]
# # 画内框
# self.img.draw_line(x1, y1, x2, y2, color=(0, 255, 0), thickness=3)
# self.img.draw_line(x2, y2, x3, y3, color=(0, 255, 0), thickness=3)
# self.img.draw_line(x3, y3, x4, y4, color=(0, 255, 0), thickness=3)
# self.img.draw_line(x4, y4, x1, y1, color=(0, 255, 0), thickness=3)
# display.show(self.img)
# else:
# self.send_to_arduino("c" + "100") # 第二阶段, 靠近二维码
print("second two!")
while 1:
self.img = camera.capture()
recv = self.recv_from_arduino()
if recv:
print("recv:", recv)
# time.sleep(3)
if recv == "f":
for i in range(15):
self.img = camera.capture()
boxes, probs = self.yolov2.run(self.img, nms=0.3, threshold=0.5)
self.yolov2.draw(self.img, boxes, probs)
display.show(self.img)
# 另写一个扫码检测的运动控制
if boxes:
boxes = boxes[0]
middle_y = boxes[1] + boxes[3] // 2
self.send_to_arduino("a" + str(middle_y)) # 找到二维码后发送找到的标志位
else:
self.send_to_arduino("a" + str(1000)) # 看不到二维码了
elif recv == "b":
break
elif recv == "n":
break # 第三阶段,扫码
print("second three!")
self.change_mode_qrcode()
while 1:
self.img = camera.capture()
# 下面指令用来预防没扫到码的情况
recv = self.recv_from_arduino()
if recv:
if recv == "n":
break
mks = self.img.find_qrcodes()
if mks:
mk = mks[0]
# 二维码信息
string = mk['payload']
self.send_to_arduino("c" + string)
# 内框数据
x1, y1 = mk['corners'][0] # 访问字典的列表
x2, y2 = mk['corners'][1]
x3, y3 = mk['corners'][2]
x4, y4 = mk['corners'][3]
# 画内框
self.img.draw_line(x1, y1, x2, y2, color=(0, 255, 0), thickness=3)
self.img.draw_line(x2, y2, x3, y3, color=(0, 255, 0), thickness=3)
self.img.draw_line(x3, y3, x4, y4, color=(0, 255, 0), thickness=3)
self.img.draw_line(x4, y4, x1, y1, color=(0, 255, 0), thickness=3)
break
display.show(self.img) # # 第四阶段,找障碍物
# print("second four!")
# self.change_mode_barr()
# while 1:
# self.img = camera.capture()
# blobs = self.img.find_blobs(self.red_threshold, merge=True, invert=True)
# display.show(self.img)
# most_pixels = 0
# max_x = 0
# if blobs:
# for n in range(len(blobs)):
# if blobs[n]["pixels"] > most_pixels:
# most_pixels = blobs[n]["pixels"]
# max_x = n
# i = blobs[max_x]
# # print("""i["pixels"]""", i["pixels"])
# if i["y"] <= self.y_low and i["pixels"] > self.thou_pixels:
# self.send_to_arduino("r" + "111")
# break
# # 第五阶段,靠近障碍物
# print("second five!")
# while 1:
# self.img = camera.capture()
# recv = self.recv_from_arduino()
# if recv:
# print("recv:", recv)
# if recv == "c":
# for i in range(5):
# self.img = camera.capture()
# blobs = self.img.find_blobs(self.red_threshold, merge=True, invert=True)
# most_pixels = 0
# max_x = 0
# if blobs:
# for n in range(len(blobs)):
# if blobs[n]["pixels"] > most_pixels:
# most_pixels = blobs[n]["pixels"]
# max_x = n
# i = blobs[max_x]
# self.img.draw_rectangle(i["x"], i["y"], i["x"] + i["w"], i["y"] + i["h"],
# color=(0, 0, 255), thickness=1) # 将找到的颜色区域画出来
# cx = i["x"] + i["w"] // 2 # 调整左右
# cy = i["y"] + i["h"] // 2 # 调整距离远近的
# # pixels = i["pixels"]
# rotation = math.degrees(i["rotation"]) - 90 # 朝向角度
# # 先考虑距离,再考虑角度,最后考虑左右横移
# if cy
#
# if cy >= 130:
# self.send_to_arduino(2)
# elif cy >= 105:
# self.send_to_arduino(20) # 组合运动 巡线前行+小小前进
# elif cy >= 80:
# self.send_to_arduino(4)
# else:
# if cx >= 115 and abs(rotation) >= 85:
# self.send_to_arduino(17) # 巡线前进
# elif cx <= 115:
# self.send_to_arduino(10) # 右胯一步
# elif cx >= 125:
# self.send_to_arduino(9) # 左胯一步
# elif 0 <= rotation <= 90:
# self.send_to_arduino(19) # 左转
# elif -80 <= rotation <= 0:
# self.send_to_arduino(18) # 原地右转
# else:
# self.send_to_arduino(4) # 小小前进
#
# else:
# self.send_to_arduino(11) # 越障
# display.show(self.img)
# # 障碍物判断加在这里
# """
# {'x': 0, 'y': 0, 'w': 50, 'h': 177, 'pixels': 6343,
# 'centroid_x': 18.807661056518555, 'centroid_y': 76.3976058959961, 'rotation': 1.645219087600708,
# 'code': 1, 'count': 1}
# 注意,y是越小越靠近底部 x是越小越靠近右边"""
# # define GO_STRAIGHT 2 // 直行
# # define GO_LITTLE_STRAIGHT 3 // 小直行
# # define GO_MICRO_STRAIGHT 4 // 小小直行
# # define GO_RIGHT 5 // 右转
# # define GO_LITTLE_RIGHT 6 // 小右转
# # define GO_LEFT 7 // 左转
# # define GO_LITTLE_LEFT 8 // 小左转
# # define GO_LEFT_TRANSVERSE 9 // 原地左胯一步
# # define GO_RIGHT_TRANSVERSE 10 // 原地右胯一步
# # define GO_OBSTACLE 11 // 越障
# elif recv == "b":
# break time.sleep(3) # 休眠3秒,防止一直扫一个二维码
# 第六阶段,重复上面找二维码的阶段
ok = 0
print("second six!")
self.change_mode_yolo()
while 1:
self.img = camera.capture()
recv = self.recv_from_arduino()
if recv:
if recv == "o":
ok = 1
if ok:
self.img = camera.capture()
boxes, probs = self.yolov2.run(self.img, nms=0.3, threshold=0.5)
self.yolov2.draw(self.img, boxes, probs)
display.show(self.img)
if boxes:
boxes = boxes[0]
middle_y = boxes[1] + boxes[3]//2
if middle_y <= 135:
self.send_to_arduino("a"+str(middle_y)) # 找到二维码后发送找到的标志位
break # 第七阶段,靠近二维码
print("second seven!")
while 1:
self.img = camera.capture()
recv = self.recv_from_arduino()
if recv:
print("recv:", recv)
# time.sleep(3)
if recv == "f":
for i in range(15): # 跳帧,防止用到了旧的图像,导致误识别
self.img = camera.capture()
boxes, probs = self.yolov2.run(self.img, nms=0.3, threshold=0.5)
self.yolov2.draw(self.img, boxes, probs)
display.show(self.img)
if boxes:
boxes = boxes[0]
middle_y = boxes[1] + boxes[3] // 2
self.send_to_arduino("a" + str(middle_y)) # 找到二维码后发送找到的标志位
else:
self.send_to_arduino("a" + str(1000)) # 看不到二维码了
elif recv == "b":
break
# 第八阶段,扫二维码
print("second eight!")
self.change_mode_qrcode()
while 1:
self.img = camera.capture()
# 下面指令用来预防没扫到码的情况
recv = self.recv_from_arduino()
if recv:
if recv == "n":
break
mks = self.img.find_qrcodes()
if mks:
mk = mks[0]
# 二维码信息
string = mk['payload']
self.send_to_arduino("c" + string)
# 内框数据
x1, y1 = mk['corners'][0] # 访问字典的列表
x2, y2 = mk['corners'][1]
x3, y3 = mk['corners'][2]
x4, y4 = mk['corners'][3]
# 画内框
self.img.draw_line(x1, y1, x2, y2, color=(0, 255, 0), thickness=3)
self.img.draw_line(x2, y2, x3, y3, color=(0, 255, 0), thickness=3)
self.img.draw_line(x3, y3, x4, y4, color=(0, 255, 0), thickness=3)
self.img.draw_line(x4, y4, x1, y1, color=(0, 255, 0), thickness=3)
break
display.show(self.img) if __name__ == 'rpyc.core.protocol' or __name__ == '__main__':
robot = RobotControl()
# robot.test_yolo()
# robot.test_qrcode()
# robot.test_barr()
robot.just_run()

2023人形全能赛v831代码(包括YOLOv2识别和扫码以及颜色识别)的更多相关文章

  1. 颜色传感器TCS230及颜色识别电路(转)

    摘要 TCS230是美国TAOS公司生产的一种可编程彩色光到频率的传感器.该传感器具有分辨率高.可编程的颜色选择与输出定标.单电源供电等特点:输出为数字量,可直接与微处理器连接.文中主要介绍TCS23 ...

  2. opencv颜色识别代码分享

    android 平台 opencv 实现颜色识别代码:http://www.eyesourcecode.com/thread-40682-1-1.htmlopencv的颜色识别简单实现的代码:http ...

  3. 50行Python代码实现视频中物体颜色识别和跟踪(必须以红色为例)

    目前计算机视觉(CV)与自然语言处理(NLP)及语音识别并列为人工智能三大热点方向,而计算机视觉中的对象检测(objectdetection)应用非常广泛,比如自动驾驶.视频监控.工业质检.医疗诊断等 ...

  4. 在Android用ZXing.jar识别二维码的精简版(简化了配置和代码)

            近期公司做了一款OTP令牌激活的产品,因为之前激活手机令牌须要输入非常多的激活信息才干进行激活. 经过一段使用后,发现易用性不是非常强,考虑假设添加二维码的的扫码功能岂不是大大添加了易 ...

  5. php 识别二维码(转载)

    近段需要写一个通过PHP来识别二维码的功能,在网上查了很久才解决问题.以此来记录下来解决问题的方法. 最开始找的方法是一个叫 php-zbarcode 的扩展,自己照着网上的安装步骤安装了 Image ...

  6. 使用ZXing.Net生成与识别二维码(QR Code)

    Google ZXing是目前一个常用的基于Java实现的多种格式的1D/2D条码图像处理库,出于其开源的特性其现在已有多平台版本.比如今天要用到的ZXing.Net就是针对微软.Net平台的版本.使 ...

  7. PHP 生成、识别二维码及安装相关扩展/工具

    2018-02-20 00:30:26  更新:推荐新扩展(极力推荐) 这篇文章里用的两个二维码扩展都有些问题和麻烦:phpqrcode(生成二维码)的源码有点小 bug: 而 php-zbarcod ...

  8. 配置zbar识别二维码(转载)

    原文地址:http://blog.csdn.net/dcrmg/article/details/52108258  二维码解码器Zbar+VS2012开发环境配置 Zbar条码解码器是一个开源的二维码 ...

  9. 【雕爷学编程】Arduino动手做(63)---TCS3200D颜色识别传感器

    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的.鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为 ...

  10. Pyqt+QRcode 生成 识别 二维码

    1.生成二维码 python生成二维码是件很简单的事,使用第三方库Python QRCode就可生成二维码,我用Pyqt给QRcode打个壳 一.python-qrcode介绍 python-qrco ...

随机推荐

  1. 关于computed

    前言: 前言就是有了前几篇的基础对于vue相应式原理的初步了解之后,再去看这两个东西会方便很多.写这篇文章是为了一个梳理,还有一些其他的原因,年底再说. 先看computed computed是在in ...

  2. go declared and not used

    Go语言在代码规范中定义未使用的变量会报"declared and not used"错误 package main import "fmt" func mai ...

  3. 【前端开发】记一次Echart 内存泄露问题的排查

    最近发现一个web项目总是莫名其妙的内存增长,然后进行定位后来发现问题大概率出在Eharts上. 于是乎就开始搜索关于echarts内存增长的一些例子,但是都没有结果. 其中翻博客时发现甚至有人换成一 ...

  4. 面试题-计算机网络-HTTP部分

    前言 计算机网络2-HTTP部分的题目,是我根据Java Guide的面试突击版本V3.0再整理出来的,其中,我选择了一些比较重要的问题,并重新做出相应回答,并添加了一些比较重要的问题,希望对大家起到 ...

  5. 【SpringCloud】Zookeeper服务注册与发现

    Zookeeper服务注册与发现 Eureka停止更新了,你怎么办 https://github.com/Netflix/eureka/wiki SpringCloud整合Zookeeper替代Eur ...

  6. CoreOS 发行版本介绍

    大多数的软件通常都有什么内测版.公测版什么的. CoreOS 发行版本 而在 CoreOS 中, 有以下3个版本: alpha - α版,音译:阿尔法,俗称尝鲜版,是最新的版本,但是容易出现bug,最 ...

  7. 基于AST实现国际化文本提取

    我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:霜序 前言 在阅读本文之前,需要读者有一些 babel 的 ...

  8. 第五届新型功能材料国际会议(ICNFM 2025)

    第五届新型功能材料国际会议(ICNFM 2025) 2025年5月16日-17日 曼谷,泰国 http://www.icnfm.net/ 会议简介 第五届新型功能材料国际会议(ICNFM 2025)将 ...

  9. PHP采集图片实例(PHP采集)

    以下为引用的内容: <?php/** *  采集图片php程序**  Copyright(c) 2008 by 小超(ccxxcc) All rights reserved**  To cont ...

  10. hadoop部署安装(六)hive

    5.配置hive 5.1 hive下载地址 http://mirror.bit.edu.cn/apache/hive/ 解压缩 [root@master ~]# tar xf apache-hive- ...