OpenMV物体分类和颜色识别两种模式通过串口发送,与STM32通信。

import sensor, image, time, math,os, tf
from pyb import UART
from pyb import Pin, Timer
# 50kHz pin6 timer2 channel1

uart = UART(3, 9600)
buff=0
while(True):
    while( uart.any() ):
        temp = uart.readline().decode()
        buff=temp
        if temp=='1':  #第一条指令的初始化(只执行一次)*******************颜色追踪初始化***************
            threshold_index = 0
            thresholds = [(41, 82, -18, 11, -20, 11)]
            area=(54,21,156,201)
            sensor.reset()
            sensor.set_pixformat(sensor.RGB565)
            sensor.set_framesize(sensor.QVGA)
            sensor.skip_frames(time = 2000)
            sensor.set_auto_gain(True)
            sensor.set_auto_whitebal(True)
            clock = time.clock()
            #打开灯
            light = Timer(2, freq=50000).channel(1, Timer.PWM, pin=Pin("P6"))
            light.pulse_width_percent(100) # 控制亮度 0~100
            print("1初始化完成")
        elif temp=='2':   #第二条指令的初始化(只执行一次)*******************图像分类初始化***************
            sensor.reset()
            sensor.set_pixformat(sensor.RGB565)
            sensor.set_framesize(sensor.QVGA)
            sensor.set_windowing((240, 240))       # Set 240x240 window.
            sensor.skip_frames(time=2000)
            net = "trained.tflite"
            labels = [line.rstrip('\n') for line in open("labels.txt")]
            clock = time.clock()
            #打开灯
            light = Timer(2, freq=50000).channel(1, Timer.PWM, pin=Pin("P6"))
            light.pulse_width_percent(100) # 控制亮度 0~100
            print("2初始化完成")



    if buff == '1' : #第一条指令的主循环(循环执行)*******************颜色追踪主循环***************
        clock.tick()
        img = sensor.snapshot()
        for blob in img.find_blobs([thresholds[threshold_index]],roi=area,invert=True,pixels_threshold=200, area_threshold=200, merge=True):
            # These values depend on the blob not being circular - otherwise they will be shaky.
            #if blob.compactness()>0.5 :
                img.draw_edges(blob.min_corners(), color=(255,0,0))
                img.draw_line(blob.major_axis_line(), color=(0,255,0))
                img.draw_line(blob.minor_axis_line(), color=(0,0,255))
                uart.write("\r\n%d %d" % (blob.cx(),blob.cy()))
                #print(blob.cx(),blob.cy())
                #print(blob.compactness())
                img.draw_string(240, 0, "FPS:%.2f"%(clock.fps()))
                img.draw_string(0,0,"%d %d" % (blob.cx(),blob.cy()))
    elif buff == '2' : #第二条指令的主循环(循环执行)*******************图像分类主循环***************
        clock.tick()
        img = sensor.snapshot()
        # default settings just do one detection... change them to search the image...
        for obj in tf.classify(net, 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]))
                #uart.write("\r\n%s = %d" % (predictions_list[i][0], predictions_list[i][1] *100))
            #打印的图像上:

            item = [0,0,0,0,0]
            img.draw_string(170, 0, "FPS:%.2f"%(clock.fps()))
            for j in range(len(predictions_list)):
                img.draw_string(0, j*8, "%s = %d %%" % (predictions_list[j][0], predictions_list[j][1]*100) )

                item[j]= predictions_list[j][1]*100

            #print("%s" % item)


            if item[0] < 90 and item[1] < 90 and item[2] < 90 and item[3] < 90 and item[4] < 90:
                uart.write("%s" % 0)
            else:
                if item[0] > 90:
                    uart.write("%s" % 1)
                elif item[1]>90:
                    uart.write("%s" % 8)
                elif item[2]>90:
                    uart.write("%s" % 9)
                elif item[3]>90:
                    uart.write("%s" % 6)
                elif item[4]>90:
                    uart.write("%s" % 5)
                else:
                    uart.write("%s" % 0)

OpenMV物体分类和颜色识别两种模式通过串口发送,与STM32通信。_第1张图片

你可能感兴趣的:(OpenMV,嵌入式,STM32)