import sensor, image, time, os, tf, math, uos, gc, ustruct,pyb#,thread
from pyb import UART,LED
from image import SEARCH_EX, SEARCH_DS
from pyb import LED
from pid import PID
rho_pid = PID(p=0.4, i=0)
theta_pid = PID(p=0.001, i=0)
THRESHOLD = (0, 36, -46, 50, 44, -83)
threshold = (245, 255)
sensor.reset()
sensor.set_vflip(False)
sensor.set_hmirror(False)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.set_windowing((240,240))
sensor.skip_frames(time=2000)
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_auto_gain(False) # 颜色跟踪必须关闭自动增益
sensor.set_auto_whitebal(False) # 颜色跟踪必须关闭白平衡
clock = time.clock()
roi = (10,0,30,30)#,(140,50,90,100)]
led = pyb.LED(4)
net = None
labels = None
min_confidence = 0.3
flag = 0
rho = 0
theta = 0
def track(rho,theta):
img = sensor.snapshot().binary([THRESHOLD])
line = img.get_regression([(100,100)], robust = True)
if (line):
rho_err = abs(line.rho())-img.width()/2
if line.theta()>90:
theta_err = line.theta()-180
else:
theta_err = line.theta()
img.draw_line(line.line(), color = 127)
print(rho_err,line.magnitude(),rho_err)
if line.magnitude()>8:
#if -40<b_err<40 and -30<t_err<30:
rho_output = rho_pid.get_pid(rho_err,1)
theta_output = theta_pid.get_pid(theta_err,1)
output = rho_output+theta_output
# car.run(50+output, 50-output)
else:
car.run(0,0)
else:
car.run(50,-50)
pass
#print(clock.fps())
print(rho_err,line.magnitude(),rho_err)
outuart(rho,theta)
try:
net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
except Exception as e:
raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')
try:
labels = [line.rstrip('\n') for line in open("labels.txt")]
except Exception as e:
raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')
colors = [
(255, 0, 0),
( 0, 255, 0),
(255, 255, 0),
( 0, 0, 255),
(255, 0, 255),
( 0, 255, 255),
(255, 255, 255),
]
uart = UART(3,115200)
uart.init(9600, bits=8, parity=None, stop=1)
def outuart(num):
global uart
data = ustruct.pack("<bbhhhhb",
0x2C,
0x12,
int(num),
int(88),
0x5B)
uart.write(data)
time.sleep_ms(1)
data = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
def UartReceiveDate():
global Find_Task
global Target_Num
global x
global data
data[0] = uart.readchar()
data[1] = uart.readchar()
Target_Num=int(data[0]-48)*10+int(data[1]-48)
#outuart(Target_Num)
clock = time.clock()
flag = 0
rho = 0
theta = 0
def track(rho,theta):
img = sensor.snapshot().binary([THRESHOLD])
line = img.get_regression([(100,100)], robust = True)
if (line):
rho_err = abs(line.rho())-img.width()/2
if line.theta()>90:
theta_err = line.theta()-180
else:
theta_err = line.theta()
img.draw_line(line.line(), color = 127)
print(rho_err,line.magnitude(),rho_err)
num = 0
temp_num = 0
UartReceiveDate
#track(rho,theta)
Target_Num = 0
def find_number(temp_num):
global tem
global num
while(True):
clock.tick()
img = sensor.snapshot()
if(num == 0):
sensor.set_windowing(1,1,100,100)
sensor.set_windowing(10,20,50,60)
if(num == 1):
sensor.set_windowing(60,40,180,180)
sensor.set_windowing(90,40,50,60)
for i, detection_list in enumerate(net.detect(img, thresholds=[(math.ceil(min_confidence * 255), 255)])):
if (i == 0): continue
if (len(detection_list) == 0): continue
print(" %s " % labels[i])
tem = labels[i]
templatei ="/"+str(labels[i])+".pgm"
print(templatei)
outuart(tem)
num = num + 1
for d in detection_list:
[x, y, w, h] = d.rect()
center_x = math.floor(x + (w / 2))
center_y = math.floor(y + (h / 2))
print('x %d\ty %d' % (center_x, center_y))
sensor.set_windowing(240,240)
if(num == 1):
temp_num=int(tem)*10
print(temp_num)
elif(num == 2):
temp_num+=int(tem)
print(temp_num)
outuart(temp_num)
break
if(num ==2):
break
if(Target_Num==temp_num):
print("okk")
outuart(1)
led = pyb.LED(2)
else:
print("error")
UartReceiveDate()
while(True):
clock.tick()
img = sensor.snapshot() # 拍一张照片并返回图像
blobs = img.find_blobs([threshold=(245, 255),roi=(10,0,30,30)])
if blobs==1:
#如果找到了目标颜色
for b in blobs:
#迭代找到的目标颜色区域
track(rho,theta)
else:
outuart("stop")
find_number(temp_num)
#track(rho,theta)
#find_number(temp_num)emmm原因贴上来
你代码写错了
blobs = img.find_blobs([threshold=(245, 255),roi=(10,0,30,30)])
改为
blobs = img.find_blobs(threshold=[(245, 255)],roi=(10,0,30,30))