logo

串口发送数据的问题?

2024-09-05 21:06
C
d14g

:worried_face:我想用串口发送一个变量,但显示错误:object with buffer protocol required。这里不知道为什么不能发照片截图。是因为我的变量没有用json定义吗?我看教程-串口通信上里面的串口发送的output_str是赋值了json定义的数据

import sensor, image, time, os, tf

from machine import UART
import json

uart = UART(3, 9600)

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.

labels = [line.rstrip() for line in open("labels.txt")]
class_num = len(labels)#取出标签数量

clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()#trained.tflite地址的模型,对比img,输出相似值
    for obj in tf.classify("trained.tflite", img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
        img.draw_rectangle(obj.rect())#对obj坐标画框
        output = obj.output()
        for i in range(class_num):
            print("%s = %f" % (labels[i], output[i]))#标签 = 置信度

    print(clock.fps(), "fps")
    
    max_index = output.index(max(output))#索引位置,即max_value在数组中的位置
    
    print("最大数的位置是:\n", max_index + 1)
    
    if max_index:
        uart.write(max_index + 1)


        最后一行这个index有问题max
2024-09-05 21:19
C
d14g

我大学生做项目,识别不同物体,然后用串口发送不同的一个数,让主板去搞其他操作,我物体识别的代码就是上面的,用index提取output这个数组的最大数的位置,然后串口发送该位置

2024-09-11 19:22
I
网红小智智 星瞳科技
uart.writechar(max_index + 1)

注意,这个只能发送0~255