加上roi的话,openmv会和电脑断开连接
img = sensor.snapshot().lens_corr(1.8)
for r in img.find_rects(threshold = 10000):
加上roi的话,openmv会和电脑断开连接
img = sensor.snapshot().lens_corr(1.8)
for r in img.find_rects(threshold = 10000):
一个用于形状及颜色识别和物块分拣的程序,运行时画面很卡
# Untitled - By: 41312 - 周四 1月 21 2021
import pyb
import sensor, image, time,pyb
from pyb import Servo
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
L = 2500
M = 1500
R = 500
s1 = Servo(1)#上料
s2 = Servo(2)#左右分拣
s3 = Servo(3)#向下分拣
theresholds = (0, 100, -74, -21, -124, 127)#颜色阈值
s1.pulse_width(L)#Initialize
def shangliao() : #上料
s1.pulse_width(M)
pyb.delay(500)
s1.pulse_width(L)
pyb.delay(500)
def xiafenjian(): #控制圆盘
s3.pulse_width(M)
pyb.delay(500)
s3.pulse_width(60)
def zuofenjian(): #左分拣
s2.pulse_width(L)
pyb.delay(500)
s2.pulse_width(M)
def youfenjian(): #右分拣
s2.pulse_width(R)
pyb.delay(500)
s2.pulse_width(M)
def ShiBieColor(): #识别颜色
pass
def circlefind() : #识别圆形
#lens_corr(1.8)畸变矫正
n=0
m=0
img = sensor.snapshot().lens_corr(1.8)
for c in img.find_circles(threshold = 3200, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(),c.y()-c.r(),2*c.r(),2*c.r())
statistics = img.get_statistics(roi=area)
print(statistics)
#(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the circle is red
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
n+=1
else:
m+=1
if n>=3 :
return 1
if m>=3 :
return 0
def rectfind() : #识别矩形
n=0
c=0
img = sensor.snapshot().lens_corr(1.8)
for r in img.find_rects(threshold = 25000):
area = (r.rect())
statistics = img.get_statistics(roi=area)
print(statistics)
if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the retangle is red
img.draw_rectangle(r.rect(),color =(0,0,0))
n+=1
else :
c+=1
if n>=3 :
return 1
if c>=3 :
return 0
def shibie() :
c = circlefind()
r = rectfind()
if c==1 :
return 1
elif r==1 :
return 2
else:
return 3
while(True):
clock.tick()
shangliao()
pyb.delay(200)
Value = shibie()
if(Value == 1) :
xiafenjian()
elif(Value == 2):
zuofenjian()
else:
youfenjian
pyb.delay(1500)
这个视频里也没讲怎么设置脉宽啊,使用s1.pluse_width()最小也不能转到零度是怎么回事。s1.pluse_width()能使舵机转170度左右但是s1.angle()不能是为什么。还有教程里用的time.sleep不能用是怎么回事
import time
import pyb
from pyb import Servo
s1 = Servo(1)
while(1):
s1.angle(-180)
pyb.delay(2000)
s1.angle(180)
pyb.delay(2000)