Repeated image saves crashes program

I’m trying to save an image when a blob is detected. It works for 1 to 5 saves, but then crashes the program and I have to reconnect. Guessing it is a memory issue, but I’m unclear on how to resolve it. Here is the code in question:

while(True):
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs([thresholds[threshold_index]])
    if len(blobs) > 0:
        for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())
        sensor.snapshot().save("test.bmp") # or "example.bmp" (or others)
        pin1.value(1)
        pyb.delay(2000) # Time to save file
    else:
        pin1.value(0)

    print(clock.fps())

Please post your code in code tags. Also, are you using an SD card. The internal flash memory is not meant for saving images to.

Yes, there is an 8GB SD card loaded.

It saves fine when I run the ‘snapshot’ example program 15 plus times in a row, but not with my code where it is done in a loop.

Please post the full script, what is pin1 and threshold_index. I’m running your script now (but commented some lines, pin1 and threshold_index) and seems to be running fine.

Note: you’re running find_blobs twice in a row, not sure why. You can just loop through the blobs, and if you want to run else statement on empty loop I think Python syntax allows for… else. Also there’s no need to delay, save() will return after the file is written and flushed.

Sometimes it runs to up to 20 seconds, but it always disconnects on me. If I comment out the save line it runs fine. Code as follows:

# Single Color RGB565 Blob Tracking Example
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.

import sensor, image, time, os, pyb
from pyb import Pin

pin1 = Pin('P1', Pin.OUT_PP, Pin.PULL_NONE)

threshold_index = 1 # 0 for red, 1 for green, 2 for blue

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.

while(True):
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs([thresholds[threshold_index]])
    if len(blobs) > 0:
        for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=100, area_threshold=100, merge=True):
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())
        sensor.snapshot().save("test.bmp") # or "example.bmp" (or others)
        pin1.value(1)
    else:
        pin1.value(0)

    print(clock.fps())

You’re using the new IDE version right? So, I made a change the IDE that causes it to disconnect from a cam if that camera doesn’t respond in 40 Ms to fix another bug. This generated the bug you are seeing. When you write a file to disk the camera may not respond to IDE requests for over 40 ms. This will be fixed in the next version of the IDE. In the mean time I can generate a fixed version of the IDE for you. I’ll post that in a bit.

Tldr, I put a bug in the IDE trying to fix another bug. I can generate the IDE again with the reverted change for you.

Note, the program is not crashing, the IDE is disconnecting. Normally the timeout is 1 second for the IDE to wait for a command response.

Here’s a link to a version of the IDE I just built without this issue.

http://upload.openmv.io/openmv-ide-windows-1.7.0/openmv-ide-windows-1.7.0.exe

Yep, that was it. Thanks for the quick fix. You guys are great!