logo

img后面这个roi参数,是0,0的时候才能正常识别,是其他的时候就识别不了,要怎么才能实现不同区域分开识别呢?

2021-12-16 23:58
5
burh
# Edge Impulse - OpenMV Image Classification Example

import sensor, image, time, os, tf, lcd

sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE)    # 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.
lcd.init()

net = "trained.tflite"
labels = [line.rstrip('\n') for line in open("labels.txt")]
clock = time.clock()

while(True):
    clock.tick()

    img = sensor.snapshot()
    img.draw_rectangle([0,0,240,120])
    img.draw_rectangle([0,120,240,240])

    for obj in tf.classify(net, img ,[0,0,240,120],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())
        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]))
    for obj in tf.classify(net, img ,[0,120,240,240],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())
        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]))

    print(clock.fps(), "fps")
2021-12-17 18:16
I
网红小智智 星瞳科技

错误原因:[0,120,240,240],超出了图像的范围。

解决办法:

这个参数是(x,y,w,h)

所以左右两个的ROI是(0,0,120,240),(120,0,120,240)

上下两个ROI是(0,0,240,120),(0,120,240,120)

另外你应该把

    img.draw_rectangle([0,0,240,120])
    img.draw_rectangle([0,120,240,240])

删掉,会影响图片。

2021-12-17 18:42
5
burh

@kidswong999 那如果要识别四个区域,这个应该怎么划分呢?不用划区域的线吗

2021-12-20 21:05
I
网红小智智 星瞳科技

如果是上下左右4个区域。就是:

(0,0,120,120),(120,0,120,120),

(0,120,120,120),(120,120,120,120)

这很难算吗?

2021-12-20 21:16
5
burh

@kidswong999 我的问题不是很这个哈,主要是哪个roi参数?为啥我换成这些区域就识别不了呢?

2021-12-20 21:31
5
burh

@kidswong999

import sensor, image, time, os, tf, lcd
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((128, 128))
sensor.skip_frames(time=2000)
lcd.init()

net = "trained.tflite"
labels = [line.rstrip('\n') for line in open("labels.txt")]
uart = UART(3, 19200)
clock = time.clock()
while(True):
    clock.tick()
    img = sensor.snapshot()
    lcd.display(img)
    for obj in tf.classify(net, img,[0,0,128,68],min_scale=1, 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())
        print("1")
        img.draw_rectangle(obj.rect())
        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[0][1] > 0.99:
              uart.write("0")
              print(0)
            if predictions_list[1][1] > 0.97:
              uart.write("1")
              print(1)

就是for obj in tf.classify(net, img,[0,0,128,68],min_scale=1, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):中img后面那个roi参数是0,0的时候才能正常识别,换成其他的时候就不能识别

2021-12-20 21:36
I
网红小智智 星瞳科技

实测可以运行的代码

# Edge Impulse - OpenMV Image Classification Example

import sensor, image, time, os, tf

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.

net = "trained.tflite"
labels = [line.rstrip('\n') for line in open("labels.txt")]

clock = time.clock()
while(True):
    clock.tick()

    img = sensor.snapshot()

    # default settings just do one detection... change them to search the image...
    for obj in tf.classify(net, img, (0,0,240,120), 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]))

    # default settings just do one detection... change them to search the image...
    for obj in tf.classify(net, img, (0,120,240,120), 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]))


    print(clock.fps(), "fps")
2021-12-21 16:05
5
burh
f3cfac09-0f0a-4f44-aebd-cd2c3b0045dd.png

确实是可以分块的,但是他好像就识别不了了,请问是什么原因呢

2021-12-21 17:29
5
burh

@kidswong999 神经网络模型是可以分区域识别的吗?