import sensor, image, time,json
from pyb import Pin
from pyb import UART
yeloow_threshold = (33, 63, -128, 89, 62, 7)#纸杯的阈值
orange_threshold = (10, 94, -21, 70, 127, 22)#橘子皮
bottle_threshold = (40, 88, -22, 50, 127, -3)#水瓶
battery_threshold = (32, 100, -5, 45, 122, 2 )#电池
spitball_threshold = (40, 90, -5, 65, 42, -128)#纸团
sensor.reset() # Initialize the camera sensor.
sensor.set_hmirror(True)
sensor.set_vflip(True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
sensor.set_auto_gain(False)
#sensor.set_hmirror(True)
clock = time.clock()
uart = UART(3, 460800)#串口波特率需要和mcuisp一致 这里设为460800
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 #寻找最大色块并返回最大色块的坐标
def use(find_max,blobs):
return find_max(blobs)
while(True):
img = sensor.snapshot()#采集图像
find_max(blobs)
print(use(find_max,blobs))
blob = max_blob
if blob == img.find_blobs([yellow_thresholds]):
blob=find_max(blobs)
img.draw_rectangle(max_blob.rect())#框选最大色块
img.draw_cross(max_blob.cx(), max_blob.cy())#在最大色块中心画十字
pcx = max_blob.cx()#定义pcx为最大色块中心的横坐标
output_str=json.dumps(max_blob.cx()) #把pcx用json字符串的形式发送给isp
uart.write(output_str + '\r\n')
print(pcx)#打印输出pcx
else:
print('not found!')
4
4dwv 发布的帖子
-
程序运行一下就不能运行了