Template matching with framesize QVGA

I’m using the template matching example in IDE as reference for my application of H7 receiving command from arduino via uart. I had the sensor framesize set at QVGA and using search_DS for template. It works for some period of time until i disconnect the H7 (in the IDE) and reconnect trying to run again. IDE pop up “OSError: ROI does not overlap on the image” and the fps is showing strange value (~0.3fps). Done screening the codes and wiring not fruitful until i realize that, changing the framesize back to QQVGA and run, then change it back to QVGA and run again will actually get the H7 back at work again. I know this is not a fix and would like to seek advise on this. Like i said, my code is based on the template matching example so i didn’t change much of the code except the framesize and template name.

I don’t quite understand the issue, can you post the code, template and whatever I need to reproduce this issue ?

Here you go. I wanted to post the *.pgm files but i don’t know how to attach.
As for the uart command from arduino, it’s just a 2-bytes ASCII coming through uart to H7.
I hope you can reproduce this issue as my code seems unstable, sometimes the snapshot() will take very long time to capture which result in drop of fps to ~0.35fps. I’m using H7 with 16GB sd card.

import sensor, image, time, pyb
from pyb import UART, LED
from image import SEARCH_DS

# from pyb import LED
red_led   = LED(1)
green_led = LED(2)
blue_led  = LED(3)

# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QVGA)
# Set as QVGA return OSError: ROI does not overlap with image. Or sometimes fps drop until ~0.35fps.
# Change it to QQVGA then start, it will run without issue, fps > 50fps.
# After that, change it back to QVGA and start again, it can run without any issue, fps can hit > 50fps.


sensor.set_pixformat(sensor.GRAYSCALE)
templates1 = ["BLT_55mmQ_Straight.pgm", "BLT_55mmQ_SkewR.pgm", "BLT_55mmQ_SkewL.pgm"]

clock = time.clock()
uart = UART(3, 115200, timeout_char=1000)
uart.init(115200, bits=8, parity=None, stop=1)
print("Waiting for Arduino...")

#-----------------------------------------------------------------------
def TM_BLE():
    result1 = 0
    count1 = 0

    # Run template matching
    while (count1 < 5):
        print(count1)
        clock.tick()
        img = sensor.snapshot()
        print(clock.fps())

        for t in templates1:
            template1 = image.Image(t)
            r = img.find_template(template1, 0.7, roi=(30,30,100,100), step=4, search=SEARCH_DS)
            if r:
                img.draw_rectangle(r)
                print(t)
                result1 = 1

        count1 = count1 + 1

    # Return result to arduino
    uart.writechar(result1)
    if (result1 == 1):
        green_led.on()
        time.sleep(1000)
        green_led.off()
    else:
        red_led.on()
        time.sleep(1000)
        red_led.off()

#-----------------------------------------------------------------------
line =""
while(True):

    # Wait for command from arduino
    if uart.any():

        buf = uart.read()
        if (buf != '\r'):
            line += str(buf)
        else:
            break

        print(line)

        cmd_1 = line[2]
        cmd_2 = line[3]

        # Check of "01" from arduino
        if cmd_1 == '0':
            if cmd_2 == '1':
                TM_BLE()
        line = ""

Please attach the templates in a zip file.

Done.
pgm for forum.zip (1.26 KB)

K, you’re going to need to figure out which part is wrong and give us just that. I can’t actually run your script since it waits on an Arduino and needs to be pointed at something.

In general, I’m getting somewhat tired of users posting all of their code and then asking us to fix it. We can debug things if you give us a specific ask. Just saying it’s not working and posting everything is not suitable. We need a specific ask.

Alright, i have removed the arduino & uart part in the code. Like i have explained in the beginning, it was running fine with QVGA initially. When i disconnect the H7 and reconnect and run again, IDE prompt OSError. Try and see if you can reproduce same issue. If yes, could you please set it to QQVGA and run then switch it back to QVGA to verify? If you managed to reproduce this, i hope there will be some reasons that you can think of.

import sensor, image, time, pyb
from pyb import UART, LED
from image import SEARCH_DS

# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QVGA)
# Set as QVGA return OSError: ROI does not overlap with image. Or sometimes fps drop until ~0.35fps.
# Change it to QQVGA then start, it will run without issue, fps > 50fps.
# After that, change it back to QVGA and start again, it can run without any issue, fps can hit > 50fps.

sensor.set_pixformat(sensor.GRAYSCALE)
templates1 = ["BLT_55mmQ_Straight.pgm", "BLT_55mmQ_SkewR.pgm", "BLT_55mmQ_SkewL.pgm"]
clock = time.clock()

#-----------------------------------------------------------------------
def TM_BLE():
    result1 = 0
    count1 = 0

    # Run template matching
    while (count1 < 5):
        print(count1)
        clock.tick()
        img = sensor.snapshot()
        print(clock.fps())

        for t in templates1:
            template1 = image.Image(t)
            r = img.find_template(template1, 0.7, roi=(30,30,100,100), step=4, search=SEARCH_DS)
            if r:
                img.draw_rectangle(r)
                print(t)
        count1 = count1 + 1

#-----------------------------------------------------------------------

while(True):
    TM_BLE()
    time.sleep(1000)

Thanks, I should be able to now.

Hi, yes I get the same error by just stopping/running the script again. I’m not sure why you’re not calling sensor.reset() (we have this explicitly in every example) if you just just add sensor.reset() before using the sensor it works as expected.

EDIT: I’ll make sure the sensor state is reset on soft-resets to avoid issues like this in the future.

Thanks for the help. I have tested your suggestion and it works as expected. I’m sorry for this silly mistake that i miss out the sensor.reset(). Thanks buddy, have a nice day.