Find_blobs function is blocked

Hi everyone! As part of a project, I was developing a small script to reuse frames from prerecorded videos in order to test it quickly. The real project is to detect the different blobs on the street and classify them. To use the prerecorded video, I extracted the frames and then compared successive frames, meaning I found the blobs between frames 1-2, 2-3, 3-4, etc. However, when I use find_blobs on some random pairs of images (e.g., pair 562 - 563), the camera stops and disconnects. I tried deleting one of these pairs and it works fine, but that’s only a temporary solution and the pair changes if the video changes. I think it’s a problem specific to these images with the find_blobs function. Any idea why this doesn’t work?

Here’s a snippet of my code. Thanks.

m = mjpeg.Mjpeg("Result_modifie.mjpeg")  #Result file
print("File name: Result_modifie.mjpeg")

files = os.listdir("/frames_bmp")
bmps = [file for file in files if "bmp" in file]
print(bmps)

# Extra buffer
img_ref = sensor.alloc_extra_fb(640, 360, sensor.GRAYSCALE)

for i in range(1, len(bmps)):
    print(f"frames: {i-1} - {i}")
    clock.tick()

    img_ref.replace(image.Image("/frames_bmp/" + bmps[i-1], copy_to_fb=True))
    img = image.Image("/frames_bmp/" + bmps[i], copy_to_fb=True)

    #DynamicAdjust(img)
    #DynamicAdjust(img_ref)

    # Frames modifications PREPROCESSING
    img_diff = img_ref.difference(img)
    img_diff_bin=PreProcessing(img_diff)

    print("getting blobs") ###ALWAYS PRINTED

    blobs = img_diff_bin.find_blobs([(254,255)], merge=True, margin=12, area_threshold=blobAreaThreshold, pixels_threshold=50) # Apply Blob detection algorithm

    print("blobs: ", len(blobs)) ### Disconnected before print this


    #*****Extract and fill list of Valid Blobs for caracterization***
       ### Operations to classify and draw blobs
    m.add_frame(img_diff_bin)
m.close()

Sounds like a bug. Can you reduce the code to its simplest form and then post everything I will need to debug it?

Thanks,

Thanks for your response! Here is the code. I have used 600-1000 images. I did different validations after this loop in order to filtre blobs, but the main issue persists. I believe it could be a problem when it detects a large number of blobs between two images, but I monitored the free memory and it seems fine to me.

import sensor, image, time, mjpeg, os, gc, pyb, math

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.skip_frames(time=2000)

clock = time.clock()       
m = mjpeg.Mjpeg("Result.mjpeg")  #Result file
print("Nombre del archivo: Result.mjpeg")

files = os.listdir("/frames_bmp")
bmps = [file for file in files if "bmp" in file]
print(bmps)

# Extra buffer
img_ref = sensor.alloc_extra_fb(640, 360, sensor.GRAYSCALE)

for i in range(1, len(bmps)):
    print(f"frames: {i-1} - {i}")
    clock.tick()

    img_ref.replace(image.Image("/frames_bmp/" + bmps[i-1], copy_to_fb=True))
    img = image.Image("/frames_bmp/" + bmps[i], copy_to_fb=True)
    # Frames modifications
    img_diff = img_ref.difference(img)

    img_diff_bin = img_diff.binary([(10,240)])

    blobs = img_diff_bin.find_blobs([(254,255)], merge=True, margin=10, area_threshold=blobAreaThreshold, pixels_threshold=50) # Apply Blob detection algorithm

    print("blobs: ", blobs)


    #*****Extract and fill list of Valid Blobs for caracterization***
    for blob in blobs:
          if blob.area() > blobAreaDetection and blob.area() < maxBlobSizeThreshold and blob.density() > blobDensity  and blob.elongation() < blobElongationMax and blob.elongation() > blobElongationMin:
            colorDrawBlob = 255                                       				# Colored in White if it validate detection criterias
            img_diff_bin.draw_rectangle(blob.rect(), color=colorDrawBlob)            				# Draw Blob founds
            img_diff_bin.draw_cross(blob.cx(), blob.cy(), color=colorDrawBlob)
            img_diff_bin.draw_string(blob.cx(),blob.cy() + 5, str(blob.area()))

    print("memoire free: ",gc.mem_free())

    # Add frame to video
    m.add_frame(img_diff_bin)
    print("frames in video: ", m.count())
    print("Size video: ", m.size())
# Close video
m.close()
print("Closed file: ",m.is_closed())

Hi, you need to upload the test image file that causes the problems. When I say reproducible, I mean you should provide everything that guarantees I hit the issue.

The code you posted above doesn’t include the images that will cause the problem.