import sensor, image, time, math,os, tf
from pyb import UART
from pyb import Pin, Timer
uart = UART(3, 9600)
buff=0
while(True):
while( uart.any() ):
temp = uart.readline().decode()
buff=temp
if temp=='1':
threshold_index = 0
thresholds = [(41, 82, -18, 11, -20, 11)]
area=(54,21,156,201)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(True)
sensor.set_auto_whitebal(True)
clock = time.clock()
light = Timer(2, freq=50000).channel(1, Timer.PWM, pin=Pin("P6"))
light.pulse_width_percent(100)
print("1初始化完成")
elif temp=='2':
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((240, 240))
sensor.skip_frames(time=2000)
net = "trained.tflite"
labels = [line.rstrip('\n') for line in open("labels.txt")]
clock = time.clock()
light = Timer(2, freq=50000).channel(1, Timer.PWM, pin=Pin("P6"))
light.pulse_width_percent(100)
print("2初始化完成")
if buff == '1' :
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([thresholds[threshold_index]],roi=area,invert=True,pixels_threshold=200, area_threshold=200, merge=True):
img.draw_edges(blob.min_corners(), color=(255,0,0))
img.draw_line(blob.major_axis_line(), color=(0,255,0))
img.draw_line(blob.minor_axis_line(), color=(0,0,255))
uart.write("\r\n%d %d" % (blob.cx(),blob.cy()))
img.draw_string(240, 0, "FPS:%.2f"%(clock.fps()))
img.draw_string(0,0,"%d %d" % (blob.cx(),blob.cy()))
elif buff == '2' :
clock.tick()
img = sensor.snapshot()
for obj in tf.classify(net, img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
img.draw_rectangle(obj.rect())
predictions_list = list(zip(labels, obj.output()))
for i in range(len(predictions_list)):
print("%s = %f" % (predictions_list[i][0], predictions_list[i][1]))
item = [0,0,0,0,0]
img.draw_string(170, 0, "FPS:%.2f"%(clock.fps()))
for j in range(len(predictions_list)):
img.draw_string(0, j*8, "%s = %d %%" % (predictions_list[j][0], predictions_list[j][1]*100) )
item[j]= predictions_list[j][1]*100
if item[0] < 90 and item[1] < 90 and item[2] < 90 and item[3] < 90 and item[4] < 90:
uart.write("%s" % 0)
else:
if item[0] > 90:
uart.write("%s" % 1)
elif item[1]>90:
uart.write("%s" % 8)
elif item[2]>90:
uart.write("%s" % 9)
elif item[3]>90:
uart.write("%s" % 6)
elif item[4]>90:
uart.write("%s" % 5)
else:
uart.write("%s" % 0)