大概改了一下代码:
import sensor, image, time
from pyb import UART,LED
import pyb
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # 灰度更快(160x120 max on OpenMV-M7)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
p = pyb.Pin("P9",pyb.Pin.OUT_PP)
#p.high()
#p.low()
LED(1).on()
LED(2).on()
LED(3).on()
while(True):
clock.tick()
img = sensor.snapshot()
rects=img.find_rects(threshold = 15000)
if rects:
p.value(1)
else:
p.value(0)
for r in rects:
img.draw_rectangle(r.rect(), color = (255, 0, 0))
cx = r.x() + (r.w() // 2)
cy = r.y() + (r.h() // 2)
img.draw_line((80,60,cx,cy), color=(127))
img.draw_cross(cx ,cy)
img.draw_cross(80 ,60)
for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
print(cx,cy)
print("FPS %f" % clock.fps())