logo

需要一个缓冲区协议的对象,这个怎么解决

2023-08-23 18:10
P
csm6
image
# Edge Impulse - OpenMV Image Classification Example

import sensor, image, time, os, tf, uos, gc,pyb,machine
from pyb import UART
uart = UART(3,115200)


green = (10,100,-50,15,-15,50)
red = (0,100,-30,50,-30,30)
blue = (10,100,-20,40,-10,40)
orange = (10,80,-10,30,-20,35)
brown = (0,80,-10,20,-20,30)
yellow = (50,80,-10,10,-20,60)
white = (10,100,-20,30,-30,20)
gray = (20,80,-10,15,-10,25)


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)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()

name = 'c'

while(True):
    clock.tick()


    def rubbish():
        net = None
        labels = None

        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:

            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) + ')')

        image = sensor.snapshot()

        scores = net.classify(image)

        # default settings just do one detection... change them to search the image...
        for obj in net.classify(image, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):

            image.draw_rectangle(obj.rect())
            # This combines the labels and confidence values into a list of tuples
            predictions_list = list(zip(labels, obj.output()))
            name = predictions_list[0][0]
            for i in range(len(predictions_list)):

                max = 0.8
                if predictions_list[i][1] > max:
                    max = predictions_list[i][1]
                    name = predictions_list[i][0]

            if name == 'KHSLJ':
                name = 'k'
            elif name == 'YHLJ':
                name = 'y'
            elif name == 'CYLJ':
                name = 'c'
            elif name == 'QTLJ':
                name = 'q'

        return name
    img = sensor.snapshot()
    blobs1 = img.find_blobs([green])
    blobs2 = img.find_blobs([red])
    blobs3 = img.find_blobs([blue])
    blobs4 = img.find_blobs([orange])
    blobs5 = img.find_blobs([brown])
    blobs6 = img.find_blobs([yellow])
    blobs7 = img.find_blobs([white])
    blobs8 = img.find_blobs([gray])

    if blobs1:
        rubbish()
        uart.write(name)

    if blobs2:
        rubbish()
        uart.write(name)

    if blobs3:
        rubbish()
        uart.write(name)
    if blobs4:
        rubbish()
        uart.write(name)

    if blobs5:
        rubbish()
        uart.write(name)

    if blobs6:
        rubbish()
        uart.write(name)

    if blobs7:
        rubbish()
        uart.write(name)

    if blobs8:
        rubbish()
        uart.write(name)
2023-08-24 02:22
I
网红小智智 星瞳科技

你把name打印出来看看是啥。

2023-08-24 12:11
P
csm6
19cb6cc0-a22d-4128-be93-8d148a480586.png
import sensor, image, time, os, tf, uos, gc,pyb,machine
from pyb import UART
uart = UART(3,115200)


green = (10,100,-50,15,-15,50)
red = (0,100,-30,50,-30,30)
blue = (10,100,-20,40,-10,40)
orange = (10,80,-10,30,-20,35)
brown = (0,80,-10,20,-20,30)
yellow = (50,80,-10,10,-20,60)
white = (10,100,-20,30,-30,20)
gray = (20,80,-10,15,-10,25)


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)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()



while(True):
    clock.tick()


    def rubbish():
        net = None
        labels = None

        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:

            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) + ')')

        image = sensor.snapshot()

        scores = net.classify(image)

        # default settings just do one detection... change them to search the image...
        for obj in net.classify(image, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):

            image.draw_rectangle(obj.rect())
            # This combines the labels and confidence values into a list of tuples
            predictions_list = list(zip(labels, obj.output()))
            name = predictions_list[0][0]
            for i in range(len(predictions_list)):

                max = 0.8
                if predictions_list[i][1] > max:
                    max = predictions_list[i][1]
                    name = predictions_list[i][0]

            if name == 'KHSLJ':
                name = 'k'
            elif name == 'YHLJ':
                name = 'y'
            elif name == 'CYLJ':
                name = 'c'
            elif name == 'QTLJ':
                name = 'q'

        return name
        
    name = rubbish()

    img = sensor.snapshot()
    blobs1 = img.find_blobs([green])
    blobs2 = img.find_blobs([red])
    blobs3 = img.find_blobs([blue])
    blobs4 = img.find_blobs([orange])
    blobs5 = img.find_blobs([brown])
    blobs6 = img.find_blobs([yellow])
    blobs7 = img.find_blobs([white])
    blobs8 = img.find_blobs([gray])

    if blobs1:
        rubbish()
        uart.write(name)
        print(name)

    if blobs2:
        rubbish()
        uart.write(name)

    if blobs3:
        rubbish()
        uart.write(name)
    if blobs4:
        rubbish()
        uart.write(name)

    if blobs5:
        rubbish()
        uart.write(name)

    if blobs6:
        rubbish()
        uart.write(name)

    if blobs7:
        rubbish()
        uart.write(name)

    if blobs8:
        rubbish()
        uart.write(name)
P
csm6

前面打印name 一直是'c',我就把代码改成这样了,又提示了这个错误

2023-08-25 01:02
I
网红小智智 星瞳科技

tf.load不要在死循环里调用,只需要在一开始调用一次。