import sensor, image, time ,pyb
from pyb import LED
from pyb import UART
import struct
import json
red_led = LED(1)
green_led = LED(2)
thresholds = [(25, 95, 23, 92, 10, 79), # 一般情况下的红色阈值
(47, 99, -92, -18, 4, 84), # 一般情况下的绿色阈值(16, 40, -58, -18, 8, 50)
(0, 80, -78, -20, -57, 4), # 一般情况下的蓝色阈值(26, 71, -27, 1, -59, -20)(7, 32, -31, 26, -82, -12)
(0, 73, 94, 19, 1, 127)]
#red_threshold = (12, 100, 127, 28, -128, 127)
#thresholds=[ (47, 99, -92, -18, 4, 84)]
#blue_threshold = (12, 100, 127, 28, -128, 127)
#green_threshold = (12, 100, 127, 28, -128, 127)
Left_Right_error=0
uart = UART(3, 256000)
middle = 0
distance = 0
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
while(True):
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs(thresholds)
# blobs = img.find_blobs(thresholds, pixels_threshold=150, area_threshold=150, merge=True)
if blobs:
green_led.on()
max_blob = find_max(blobs)
middle_x = max_blob.cx()
middle_y = max_blob.cy()
Len=max_blob[2]
width=max_blob[3]
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
Lm = (max_blob[2]+max_blob[3])/2
distance = int(2668/Lm)
# print(distance)
uart.writechar(0xf1)
uart.writechar(middle_x>>8)
uart.writechar(middle_x)
uart.writechar(middle_y>>8)
uart.writechar(middle_y)
uart.writechar(Len>>8)
uart.writechar(Len)
uart.writechar(width>>8)
uart.writechar(width)
uart.writechar(0xf2)
print(middle_x,middle_y,max_blob[2],max_blob[3])
else:
green_led.off()
for c in img.find_circles(threshold = 2000, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
print(c)
![0_1715757268905_f3e623a24127ad9c0d0740abb23b110.png](https://fcdn.singtown.com/28b95848-8e1c-4611-b35b-085ca485da11.png)
F
fi2e
@fi2e
0
声望
2
楼层
428
资料浏览
0
粉丝
0
关注
fi2e 发布的帖子
-
现在是一识别到物体,这边就报错,应该是之前我把u盘main文件换成我的了,请问怎么解决?
-
我把存储器里的main换成我的了,就报错了,格式化了下还是报错
![0_1715752354864_a747deec-0378-4afd-9c41-55e62b7b38bd-image.png](https://fcdn.singtown.com/e818140f-f566-4dee-8ccd-faa700 9dcc3f.png)