logo

请问神经网络检测到了目标怎么计数

2023-07-18 18:54
M
_6pcn

我正在坐一个环形小车,每跑一圈都会经过一次停止线,我想在检测到第三次停止线的时候给单片机发送信号停止,但是我不会怎么每经过一次记圈数+1,因为目标是从远到近逐渐出现,如果累加的话一秒就会记很多次,中间也存在漏检,请问怎么解决这个问题

mport sensor, image, time, os, tf, uos, gc, lcd , cpufreq
from pyb import UART,LED

uart = UART(3, 115200, timeout_char=1000)
u_start=bytearray([0x2C,0x12])
u_over=bytearray([0x5B])
LED(2).on()

sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)     # Set frame size to QVGA (320x240)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=2000)          # Let the camera adjust.
lcd.init()
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:
    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()




while(True):
    flag=3
    clock.tick()
    cpufreq.set_frequency(480)
    img = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.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())

        predictions_list = list(zip(labels, obj.output()))
        #print(predictions_list[0][1])
        if(predictions_list[0][1]>=0.7):
            flag=2
        elif(predictions_list[3][1]>=0.95):
            flag=4

        else:
            flag=3
        #for i in range(len(predictions_list)):
            #print("%s = %f" % (predictions_list[i][0], predictions_list[i][1]))


    uart_buf = bytearray([flag])
    img.draw_string(10,10,str(flag),color=(0,0,255))
    uart.write(u_start)
    uart.write(uart_buf)
    uart.write(u_over)
    print(clock.fps(), "fps")
    lcd.display(img)
2023-07-18 20:09
I
网红小智智 星瞳科技

连续20次没有识别到,就算增加一圈。

2023-07-18 21:04
M
_6pcn

那请问这个的判断应该怎么写,能稍微给一下思路嘛

2023-07-19 20:13
I
网红小智智 星瞳科技
连续次数 = 0
圈数 = 0

while True:
    if 识别到了:
        连续次数 = 0
    else:
        连续次数 += 1
    if 连续次数 == 20:
        圈数 += 1
    print(圈数)