logo

代码运行时,图象十分卡顿,怎么处理?

2021-01-28 21:41
N
odwv

一个用于形状及颜色识别和物块分拣的程序,运行时画面很卡

# 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)
2021-01-29 19:28
I
网红小智智 星瞳科技

你的代码里那么多pyb.delay,当然会延迟很多。