Unit-v detects the target ball, passes the coordinates to m5stack, and displays the coordinates of the ball on the screen, so as to experience deep learning and target detection.
Unit-V Code:
import image
#import lcd
import sensor
import sys
import time
import KPU as kpu
from fpioa_manager import *
import math
import KPU as kpu
from Maix import GPIO
import utime
#===========uart init
fm.register(34,fm.fpioa.UART1_TX)
fm.register(35,fm.fpioa.UART1_RX)
uart_out = UART(UART.UART1, 115200, 8, None, 1, timeout=1000, read_buf_len=4096)
#Load trained model
task=kpu.load(0x00300000)
#There is only one target ball, 1 is the ball
labels=["1"] #You can check the numbers here to real names.
anchor = (0.33340788 * 16, 0.70065861 * 16, 0.18124964 * 16,0.38986752 * 16, 0.08497349 * 16,0.1527057 * 16)
a = kpu.init_yolo2(task, 0.05, 0.05, 3, anchor)
#Note: the first is the threshold of the accuracy of the interest box
#The second parameter is the accuracy threshold of the target. V-train sends back 0.2, now 0.05, which is more reliable if it is adjusted higher, but the recognition rate may be low
#It's easy to recognize with low parameter, but it's also easy to misidentify.
print("Load Done.")
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((320, 224))
sensor.run(1)
print("Init Done.")
counter = 1 #Counter
code_stake = [] #Recognition set
ball_x = 0 #X coordinate of ball
ball_y = 0 #Y coordinate of ball
while(True):
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)
if code:
if counter < 3:
'''Cumulatively 3 times of recognition, and add the recognition results to the code "stick []'''
code_stake = code_stake + code
counter = counter + 1
else:
counter = 0
ball_x = 0
ball_y = 0
#注:i.rect()[0] i.rect()[1] i.rect()[2] i.rect()[3] 分别是兴趣框的x,y,w,h
for i in code_stake:
ball_x=i.x()
#ball_x=i.rect()[0]
ball_y=i.y()
#ball_y=i.rect()[1]
if counter == 0: #3 Identification completed
#send data to uart
#==== send json str for ball_x and ball_y
s_json="{\"ball_x\":"+str(round(ball_x))+",\"ball_y\":"+str(round(ball_y))+"}"
uart_out.write(s_json+"\r\n")
print(s_json)
code_stake = [] #Clear identification set
utime.sleep_ms(50) #This delay should not be too low
for i in code: #Mark the interest box and origin on the screen
img.draw_rectangle(i.rect())
else:
#lcd.display(img)
pass
a = kpu.deinit(task)
M5Stack Code:
from m5stack import *
from m5ui import *
from uiflow import *
import json
setScreenColor(0x222222)
#rectangle0 = M5Rect(40, 5, 200, 170, 0xFFFFFF, 0xFFFFFF)
label1 = M5TextBox(39, 10, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
label2 = M5TextBox(231, 12, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
circle0 = M5Circle(81, 144, 10, 0xfdd708, 0xFFFFFF)
uart = None
dict2 = None
data1 = None
atom_x = None
atom_y = None
uart = machine.UART(1, tx=17, rx=16)
uart.init(115200, bits=8, parity=None, stop=1)
while True:
if uart.any():
dict2 = uart.readline()
data1 = json.loads(dict2)
atom_x=data1['ball_x']
atom_y=data1['ball_y']
label1.setText(str(atom_x))
label2.setText(str(atom_y))
circle0.setPosition((320-atom_x), (atom_y+20))
CangHai
Posted by vany5921
Comments