logo

有大佬帮忙看看openmv怎么和matlab通信吗?

2023-02-21 10:44
H
zhq6

Openmv

import sensor, image, time, os, tf, uos, gc, lcd, pyb
from pyb import Pin,Timer,LED
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.
sensor.set_auto_whitebal(False) # turn this off.

apple=(15, 54, 75, 10, -57, 55)
orange=(55, 98, -128, 127, 23, 127)
#(55, 98, -128, 127, 23, 127),(13, 55, -11, 24, 9, 97)
#strawberry=()

net = None
labels = None

key1=Pin('P1',Pin.IN,Pin.PULL_UP)       #启动
key2=Pin('P2',Pin.IN,Pin.PULL_UP)       #停止
LED=Pin('P9',Pin.OUT_PP,Pin.PULL_NONE)

StartFlag=0 #开始标志

try:
    # load the model, alloc the model file on the heap if we have at least 64K free after loading
    net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
except Exception as e:
    print(e)
    raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')

try:
    labels = [line.rstrip('\n') for line in open("labels.txt")]
except Exception as e:
    raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')




clock = time.clock()
uart=UART(3,115200)
uart.init(115200, bits=8, parity=None,timeout=1000, read_buf_len=128)  #8位数据位,无校验位,1位停止位、
#uart = pyb.USB_VCP()



def sending_data(size):
    global uart;
    
    #data = ustruct.pack("<bbhhh",      #格式为俩个字符俩个短整型(2字节)
                   #0x2C,                      #帧头1
                   #0x12,                      #帧头2
                   #int(size), # up sample by 4   #数据1)
    data=str(size)                 
    uart.write(data);   #必须要传入一个字节数组


while(True):
    clock.tick()
###############################启动停止识别程序##############################################
    img = sensor.snapshot()

    if key1.value()==0:   # 按钮启动
        LED.value(1)      #LED灯亮
        while key1.value()==0:
              key1.value()
        StartFlag=1        #开始识别

    if key2.value()==0:   # 按钮启动
        LED.value(1)      #LED灯亮
        while key2.value()==0:
              key2.value()
        StartFlag=0        #停止识别
    LED.value(0)
############################################################################################

#################################神经网络分类并画框读出尺寸################################################
    # default settings just do one detection... change them to search the image...

    if StartFlag==0:
        for obj in net.classify(img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
            #print("**********\nPredictions at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())
            #img.draw_rectangle(obj.rect())
            # This combines the labels and confidence values into a list of tuples
            predictions_list = list(zip(labels, obj.output()))
            for i in range(len(predictions_list)):
                print("%s = %f" % (predictions_list[i][0], predictions_list[i][1]))
                if predictions_list[i][0] == 'apple':
                    apple_blobs=img.find_blobs([apple])
                    if len(apple_blobs) == 1:
                        b=apple_blobs[0]
                        img.draw_rectangle(b[0:4],color=(255,0,0)) # rect
                        img.draw_cross(b[5], b[6]) # cx, cy
                        length=b[2]-b[0]
                        width=b[3]-b[1]
                        #size=str(length if length>width else width)
                        #print(size)
                        #data_out=json.dumps(size)
                        #uart.write(data_out+'\n')
                        size=length if length>width else width
                        print(size)
                        sending_data(size)




                if predictions_list[i][0] == 'orange':
                    orange_blobs=img.find_blobs([orange])
                    if len(orange_blobs) == 1:
                            b=orange_blobs[0]
                            img.draw_rectangle(b[0:4],color=(250, 140, 53)) # rect
                            img.draw_cross(b[5], b[6]) # cx, cy
                            length=b[2]-b[0]
                            width=b[3]-b[1]
                            #size=str(length if length>width else width)
                            #print(size)
                            #data_out=json.dumps(size)
                            #uart.write(data_out+'\n')
                            size=length if length>width else width
                            print(size)
                            sending_data(size)


                #################################窗口显示水果种类#############################
                if predictions_list[i][1]>0.75:
                    LED.value(1)    #LED灯亮
                    img.draw_string(30,10,predictions_list[i][0],color=(255,0,0),scale=2,x_spacing=1,mono_space=False,string_rotation=0,)
                #############################################################################
    else:
         img.draw_string(30,0,"Stop",color=(255,0,0), scale=2,x_spacing=1,mono_space=False,string_rotation=0)

    print(clock.fps(), "fps")
H
zhq6

@zhq6

matlab端口
% 配置串口
clear all
delete(instrfindall)
instrreset()

s = serialport('COM10',115200);
configureTerminator(s,"LF")
fprintf('Serial port is open and configured.\n');
%device.Terminator
while true
    data = readline(s);
    disp(data);
end
% Close the serial port when done
fclose(s);