Freeze after toggle sensor pixformat in the code

I’m using OpenMV on a project which requires the running code switches between color and black&white modes dynamically according to the message received by UART.

The situation:
By default, I set the sensor to GRAYSCALE and then set it to RGB565 according to the UART. Possibly, I need to set back to GRAYSCALE.

However, when I test it, the camera would freeze when I switch the sensor between GRAYSCALE and RGB565 for 1 or 2 times.
What could cause this problem? And how should this be fixed?
I guess it may be related to the UART interrupt because I try to use a counter variable to switch the mode and it didn’t freeze.

By the way, sometimes when I reconnect the Cam to the IDE, and run the code for the first time, the IDE would show: “MemoryError: memory allocation failed”…What is this problem?

Some key code:

def init(mode):
    if mode == TRACK_XY:
        # sensor.reset()
        sensor.set_pixformat(sensor.GRAYSCALE)
        sensor.set_framesize(sensor.QQVGA)
        sensor.skip_frames(300)
        sensor.set_auto_gain(True)
        sensor.set_auto_whitebal(False)
    elif mode == TRACK_COLOR:
        # sensor.reset()
        sensor.set_pixformat(sensor.RGB565)
        sensor.set_framesize(sensor.QQVGA)
        sensor.skip_frames(time=1500)
        sensor.set_auto_gain(False)
        sensor.set_auto_whitebal(False)
        if color_learn:
            threshold_learn(threshold_color_mid)
            print(threshold_color_mid)
    elif mode == TRACK_TAG:
        # sensor.reset()
        sensor.set_pixformat(sensor.GRAYSCALE)
        sensor.set_framesize(sensor.QQVGA)
        sensor.skip_frames(time=30)
        sensor.set_auto_gain(False)
        sensor.set_auto_whitebal(False)
    else:
        pass
    
while(True):
    # Init
    if init_flag == 0:
        init(cam_mode)
        init_flag = 1

    if cam_mode == SLEEP:
        pass
    elif cam_mode == TRACK_XY:
        img = sensor.snapshot()
        pass
    elif cam_mode == TRACK_COLOR:
        img = sensor.snapshot()
        pass
    elif cam_mode == TRACK_TAG:
        pass

   #read mode from uart
    cam_mode_old = cam_mode
    for i in range(available()):
        parse_byte(read_byte())
    # Init again
    if cam_mode != cam_mode_old:
        init_flag = 0

Hi, is there a reason you need to change modes? All the newer code I’m adding works in grayscale and rgb565. Anyway, when you switch modes some frames will be dropped. This isn’t a bug. It just happens.

By newer code… Anything that returns an object with methods as a result is newer code. So, all the tag/code stuff, line stuff, and color tracking stuff. Eventually everything will handle both grayscale and RGB. But in general, only a few methods don’t and they explicitly say this in the doc.

If you are getting a memory allocated filed error then you are running out of heap for something. Debug what method is throwing hat and wrap it in a try and except so you can just bail if the method fails.

Yeah, I need to track black line against the white background on one circumstance and after that, I need to track an object with color.

I have found that by decreasing the time of skipped frames (“sensor.skip_frames(10)” ) will make it work fine.
Change it from 10 to 100, and the camera would freeze after switched to RGB from GRAYSCALE.

Thanks anyway.