Help me, I'm a beginner and I have a bug that I can't handle

I tried various programs and I got the error : MemoryError: Out of fast frame buffer stack memory

when the program starts, it runs for a couple of seconds, and then this

Post the code?

But, don’t just provide the whole program. Please reduce it down to just the lines of code necessary to cause the error.

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

BLUE_THRESHOLD = (0, 50, -128, 127, -128, 127)  
RED_THRESHOLD = (0, 70, 30, 127, -128, 127)     

blue_count = 0  
red_count = 0   

while(True):
    img = sensor.snapshot()   
    rects = img.find_rects(threshold = 10000)  

    for r in rects:
        # Определяем цвет прямоугольника
        stats = img.get_statistics(roi=r)
        if stats.l_mode() == image.BLUE:  
            img.draw_rectangle(r, color = (0, 0, 255))  
            blue_count += 1
        elif stats.l_mode() == image.RED:  
            img.draw_rectangle(r, color = (255, 0, 0))  
            red_count += 1

    print("Синие квадраты:", blue_count)
    print("Красные квадраты:", red_count)

==================================
according to the idea, the program should look for blue and red squares and determine their number.

I have a task in which a drone flies according to the program and flies over an area with squares of two colors and the camera must determine the number of squares of different colors and give the data to the drone.

If you explain to me in detail what is wrong with me, I will be very grateful.

the error occurs at this point: rects = img.find_rects(threshold = 10000)

Hi, yes,

That function can run out of memory at higher resolutions. Please use it at QQVGA. find_rects() uses the AprilTag library quad detector, which is a very heavy-weight algorithm that uses malloc.

As such, it can exhaust RAM.

Given you want to detect squares, you don’t need find_rects(). Insteaduse find_blobs() with the color thresholds you are looking for and then use the minimum_rect() feature which will return the rotated rectangle that fits around a blob. You can then compare the number of pixels found in the blob and the size of the rectangle to determine if it’s a square that’s filled.

This will not run out of RAM and be much faster and run at a higher resolution.

1 Like

Thanks for the help, I’ll try to figure it out.