import sensor, image, time
from machine import UART
from pyb import UART
from pyb import Pin, Timer
def find_max_circle(circles):
max_size = 0
for circle in circles:
if circle.r()>max_size:
max_circle = circle
max_size = circle.r()
return max_circle
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 500)
sensor.set_auto_gain(False)
sensor.set_hmirror(True) #水平方向翻转
sensor.set_vflip(True) #垂直方向翻转
# 50kHz pin6 timer2 channel1
light = Timer(2, freq=50000).channel(1, Timer.PWM, pin=Pin("P6"))
light.pulse_width_percent(100) # 控制亮度 0~100
uart = UART(3, 115200)#P4-TX P5-RX
clock = time.clock()
rw=0
x=0
y=0
data=([0xFE,0xE0F,x,y,rw])
while(True):
clock.tick()
img = sensor.snapshot()
img.lens_corr(1.6)
for code in img.find_qrcodes():
img.draw_rectangle(code.rect(), color = (255, 0, 0))
rw=int(code.payload())
data=bytearray([0xFE,0xE0,x,y,rw])
uart.write(data)
print(rw,data)
circles = img.find_circles(x_stride=5, y_stride=1, threshold = 4000, x_margin = 20, y_margin = 20, r_margin =20,
r_min =6, r_max = 100, r_step = 2)
if circles:
max_circle = find_max_circle(circles) #找到最大的圆
area = (max_circle.x()-max_circle.r(), max_circle.y()-max_circle.r(), 2*max_circle.r(), 2*max_circle.r())
img.draw_rectangle(area, color = (250, 0, 0))
x=int(max_circle.x())
y=int(max_circle.y())
data=bytearray([0xFE,0xE0,x,y,rw])
uart.write(data)
print(x,y,data)
O
o2rl 发布的帖子
-
使用h7plus执行扫码、识别圆形两个任务,运行时帧率降低怎么解决?