
c sensor, image, time, math, struct,random
from pyb import UART
red = (49, 97, -31, 67, 52, 94)
red1 = (39, 100, 20, 127, -128, 127)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 10)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
# 串口初始化
uart = UART(4, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
def find_max(blobs):
max_size = 0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
def blob_sort(blobs):
new_blobs = []
for i in range(0,len(blobs)):
max_blob = find_max(blobs)
new_blobs.append(blobs.pop(blobs.index(max_blob)))
return new_blobs
def send_message(blob):
#for blob in blobs:
x = int((blob.cx())*128/320)
y = int((blob.cy())*60/240)
z = int(blob.area()*(128*60)/(320*240))
num = 1
dateout = struct.pack("<bbhhhb", # 打包数据
0xaa, # 帧头1
0xae, # 帧头2
x, # 数据1
y, # 数据2
num, # 数据3
0xff) # 帧尾
uart.write(dateout + '\n')
print(dateout)
#将获取到的色块的坐标变为json json.dumps([max_blob.cx(),max_blob.cy()])
while(True):
Location = []
clock.tick()
img = sensor.snapshot()
red_blobs = img.find_blobs([red1],area_threshold=10,pixels_threshold=20)
if red_blobs:
#new_blobs = blob_sort(red_blobs) #对色块的大小进行排序
#send_message(new_blobs) #获取排序后色块的信息并依次发送至muc
max_blob = find_max(red_blobs)
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
send_message(max_blob)