import sensor, image, time, os, tf, math, uos, gc, ustruct, pyb
from pyb import UART,LED
from image import SEARCH_EX, SEARCH_DS
sensor.reset()
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)
led = pyb.LED(4) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
'''
template1 = image.Image("/1.pgm")
template2 = image.Image("/2.pgm")
template3 = image.Image("/3.pgm")
template4 = image.Image("/4.pgm")
templates = ["/1.pgm","/2.pgm","/3.pgm","/4.pgm"]
'''
rois = [(20,20,100,150),(140,50,90,100)]
net = None
labels = None
min_confidence = 0.5
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
num = 0
temp_num = 0
while(True):
clock.tick()
img = sensor.snapshot()
UartReceiveDate()
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)
if(num==2):
break
led.on()
time.sleep_ms(150)
led.off()
time.sleep_ms(100)
led.on()
time.sleep_ms(150)
led.off()
time.sleep_ms(600)
''' else:
print(temp_num)
temp_num = 0
if (Target_Num == temp_num): #发送回去
print("ok")
outuart("ok")
#print(clock.fps(), "fps", end="\n\n")'''第170行到116行,你加了sleep,当然帧率就慢了。