【这是识别对象】
我现在的代码如下(不能一起且按顺序输出):import sensor, image, time
from pyb import UART
import json #载入库
thresholds = [(65, 70, -59, -47, 11, 35),
(27,33,33,44,-75,-64),
(34,41,49,60,1,22)] #1绿色 2蓝色 3*红色
sensor.reset()
sensor.set_pixformat(sensor.RGB565) #设置图像格式为 LAB 三通道图像
sensor.set_framesize(sensor.QVGA) #设置图像分辨率为 320240
sensor.skip_frames(time = 5000) #等待 2s 感光元件稳定后执行后续任务
sensor.set_auto_gain(False) #关闭自动增益
sensor.set_auto_whitebal(False) #关闭白平衡
clock = time.clock() #初始化时钟
uart = UART(3, 9600) #初始化串口 3,波特率为 9600
#寻找最大的目标像素块函数#
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
#寻找目标颜色块程序*#
while(True):
img = sensor.snapshot() #获取图像存至 img 变量
#sensor.set_vflip(True) #开启镜头反向
GREEN_blobs = img.find_blobs([thresholds[0]], pixels_threshold=15, area_threshold=15, merge=False)
#识别LVSEI
BLUE_blobs = img.find_blobs([thresholds[1]], pixels_threshold=15, area_threshold=15, merge=False)
#识别LANSEI
RED_blobs = img.find_blobs([thresholds[2]], pixels_threshold=15, area_threshold=15, merge=False)
#识别HONGSEI
print("干旱信息如下")
if GREEN_blobs:
max_blob=find_max(GREEN_blobs) #查找色块中最大的一个
img.draw_rectangle(max_blob.rect()) #用矩形框框出最大的色块
img.draw_cross(max_blob.cx(), max_blob.cy()) #十字标记最大色块中心
print("轻微干旱")
output_target="1"
print( output_target)
uart.write(output_target) #通过串口发送数据
elif BLUE_blobs:
max_blob=find_max(BLUE_blobs)
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
print("一般干旱")
output_target="2"
print( output_target)
uart.write(output_target)
elif RED_blobs:
max_blob=find_max(RED_blobs)
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
print("严重干旱")
output_target="3"
print( output_target)
uart.write(output_target)
else:
print('not found!')
output_target="0"
print( output_target)
uart.write(output_target)