logo

串口数据传输速度太慢怎么提高速度呢?

2021-05-12 14:29
U
_2yu5
# Edge Impulse - OpenMV Image Classification Example

import sensor, image, time, os, tf
from pyb import UART
import json


sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=2000)          # Let the camera adjust.

net = "trained.tflite"#调用神经网络模型
labels = [line.rstrip('\n') for line in open("labels.txt")]
color_threshold = [(0, 100, 19, 127, -9, 127)]#红色色块提取
uart = UART(3, 115200)#串口通信

def zfl(s, width):
    return '{:0>{w}}'.format(s, w=width)

def find_max(blobs):
    max_pixels=0
    for blob in blobs:
        if blob[4] > max_pixels:
            max_blob=blob
            max_pixels = blob[4]
    return max_blob

clock = time.clock()
while(True):
    clock.tick()

    img = sensor.snapshot()
    blobs = img.find_blobs(color_threshold,pixels_threshold=100, area_threshold=10)
    #色块提取,确定roi区域
    if blobs:
        max_blob = find_max(blobs)
        x = max_blob.x()
        y = max_blob.y()
        w = max_blob.w()
        h = max_blob.h()
        thresholds = (x,y,w,h)
        #获取图像xy伸缩率,float形式
        x_change = 240.0/w
        y_change = 240.0/h
        #中心位置转为4位字符串形式
        X = zfl(str(max_blob.cx()),4)
        Y = zfl(str(max_blob.cy()),4)
        #图像roi区域复制,便于之后操作
        img = img.crop(roi=thresholds,x_scale=x_change,y_scale=y_change,copy_to_fb=True)
        #神经网络判别并输出,置信度70%
        for obj in tf.classify(net, img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
 #           print("----------" )
            predictions_list = list(zip(labels, obj.output()))
            for i in range(len(predictions_list)):
                if (predictions_list[i][1] > 0.6):#相似度达到60%以上认为是
                    output_str = 'AB'+X+Y
                    print("%s"%(predictions_list[i][0]))

                    uart.write(output_str)
                    print(output_str)
                    print(clock.fps())
d4aaf395-93b1-4905-b082-aa7fbacc35dd.png
2021-05-12 19:13
I
网红小智智 星瞳科技

不是传输速度慢,是神经网络需要的计算量大,计算起来慢。这个没法解决的。