openmv的帧率在运行过程中逐渐下降的原因

openmv在运行过程中帧率逐渐下降,原本应该46帧逐渐下降到25帧左右。附上代码:

# Single Color Code Tracking Example
#
# This example shows off single color code tracking using the OpenMV Cam.
#
# A color code is a blob composed of two or more colors. The example below will
# only track colored objects which have both the colors below in them.

import sensor, image, time, math , pyb, struct ,gc
from pyb import UART
import ulab as np
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [(0, 34, -28, 5, 7, 30) # generic_red_thresholds -> index is 0 so code == (1 << 0)
              ] # generic_green_thresholds -> index is 1 so code == (1 << 1)
# Codes are or'ed together when "merge=True" for "find_blobs".
#(0, 13, -30, 5, -2, 16)
def get_speed(tl,tn,xl,xn,yl,yn):
    time_diff=tn-tl
    speed=[0,0]
    speed[0]=(xn-xl)*1000/time_diff
    speed[1]=(yn-yl)*1000/time_diff
    return speed

def get_acc(tl,tn,xsl,xsn,ysl,ysn):
    time_diff=tn-tl
    acc=[0,0]
    acc[0]=(xsn-xsl)*100/time_diff
    acc[1]=(ysn-ysl)*100/time_diff
    return acc

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
#设置图像大小
sensor.set_windowing((240,240))

sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

time_now=0;x_now=0;y_now=0;x_speed_now=0;y_speed_now=0;
time_last=0;x_last=0;y_last=0;x_speed_last=0;y_speed_last=0;
speed_now=[0,0]

# 数据传输协议
# 0x01 为坐标传输
# 0x02 为速度传输
# 0x03 为加速度传输
uart = UART(3, 115200)
zt=0x55
def tx_data(data,id):
    if(id==0x01 or id==0x04):
        tx=struct.pack("bbbii",zt,zt,id,data[0],data[1])
    else:
        tx=struct.pack("bbbff",zt,zt,id,data[0],data[1])
    uart.write(tx)
    return tx

roi0=(0,0,240,240)
while(True):
    clock.tick()
    time_last=time_now
    time_now=pyb.millis()
    img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)
    img.draw_rectangle(roi0, color=(0,255,0))
    x_last=x_now;y_last=y_now;
    x_speed_last=speed_now[0];y_speed_last=speed_now[1];
    blobs= img.find_blobs(thresholds,roi=roi0, pixels_threshold=10, area_threshold=10, merge=True)
    if(len(blobs)!=0):
        # 选择最大的像素块作为对象
        max_index0 = blobs.index(max(blobs, key=lambda x: x[4]))
        img.draw_cross(blobs[max_index0].cx(),blobs[max_index0].cy(), size=5, color=(255,0,0))
        img.draw_rectangle((blobs[max_index0][0],blobs[max_index0][1],blobs[max_index0][2],blobs[max_index0][3]), color=(0,255,0))
        #x y坐标赋值
        x_now=240-blobs[max_index0].cx();y_now=240-blobs[max_index0].cy();
        #计算速度
        speed_now=get_speed(time_last,time_now,x_last,x_now,y_last,y_now)
        #计算加速度
        #acc_now=get_acc(time_last,time_now,x_speed_last,x_speed_now,y_speed_last,y_speed_now)
        print("x=",x_now,"speed=",speed_now)
        tx_data([x_now,y_now],0x01)
        tx_data(speed_now,0x02)
        #tx_data(acc_now,0x03)
    else:
        print("error")
        tx_data((1,1),0x04)
    print(clock.fps())

代码的作用是板球的识别。
原本以为是因为没有及时释放变量,使得内存满了导致的卡顿。
但是经过排查发现是因为串口波特率设置问题,将串口波特率从9600改成115200即可恢复46的帧率。
openmv的帧率在运行过程中逐渐下降的原因_第1张图片
(运行效果图)

你可能感兴趣的:(openmv,python)