import sensor, image, time
from pyb import UART
import json
red_threshold = ((85, 43, 60, 96, -65, 38))
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
uart = UART(1, 9600)
def find_max(blobs):
max_pixels=0
for blob in blobs:
if blob[4] > max_pixels:
max_blob=blob
max_pixels = blob[4]
return max_blob
while(True):
clock.tick()
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([red_threshold])
if blobs:
max_blob=find_max(blobs)
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
pcx=max_blob.cx()
pcy=max_blob.cy()
cxy={"cx":pcx,"cy":pcy}
uart.write(json.dumps(cxy) + '\r\n')
print('you send:',cxy)
比如上面这样的代码,检测色块中心坐标并通过串口发出来,比如我现在想要500ms发一次,应该如何设置?我试过加延时函数500ms,但是发现摄像头的图像变得特别卡,好像不行