# Detecting multiple moving objects using Arduino Nicla Vision

**URL:** <https://forums.openmv.io/t/detecting-multiple-moving-objects-using-arduino-nicla-vision/8195>\
**Category:** OpenMV Boards\
**Created:** [April 6, 2023, 10:26am UTC](https://forums.openmv.io/t/detecting-multiple-moving-objects-using-arduino-nicla-vision/8195 "2023-04-06T10:26:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeongmingg](https://avatars.discourse-cdn.com/v4/letter/y/96bed5/32.png) [@yeongmingg](https://forums.openmv.io/u/yeongmingg)\
**Post date:** [April 6, 2023, 10:26am UTC](https://forums.openmv.io/t/detecting-multiple-moving-objects-using-arduino-nicla-vision/8195/1 "2023-04-06T10:26:34Z")

</div>

This code is written to detect the moving object in certain color range. I am planning to detect multiple objects instead of one object. Which part can I modify to achieve this? I tried to loop through all the blobs detected and process one by one but the result come out show that still only one blob detected.

```auto
#The code can detect one moving object
#When the object stays stationary, no rectangle will be drawn
#When the object is moving toward the camera, a rectangle will be drawn on detected object.
#The speed and distance of detected object will be shown above the rectangle.
#When the object is not moving toward the camera, the object will be ignored.

import sensor, image, time
from machine import I2C
from vl53l1x import VL53L1X

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

min_blob_size = 50 # Minimum size of blob to detect
fps = 60 # Frame per second
blob_areas = [] # List to store the previous blob areas
distances = [] # List to store the previous distances

# Define the physical size of a pixel in meters
pixel_size_m = 0.0001 # replace with the actual value for your camera

# Define the maximum and minimum distances to detect objects
max_distance = 10.0 # in meters
min_distance = 0.1 # in meters

# Define the distance sensor
tof = VL53L1X(I2C(2))

prev_distance_m = None
prev_time = None

while True:
    img = sensor.snapshot()
    blobs = img.find_blobs([(0,50),(175, 225)], area_threshold=min_blob_size)

    for blob in blobs:
        # Compute the distance of the blob from the camera
        distance_cm = tof.read() / 10 # Convert from mm to cm
        distance_m = distance_cm / 100 # Convert from cm to m
        distances.append(distance_m)

        # Compute the displacement of the object
        if prev_distance_m is not None and prev_time is not None:
            displacement_m = prev_distance_m - distance_m
            time_elapsed = time.ticks_diff(time.ticks_ms(), prev_time) / 1000 # Convert to seconds
            if time_elapsed > 0:
                speed_mps = displacement_m / time_elapsed
            else:
                speed_mps = 0.0

            # Show the speed above the blob if the speed is above 0.1 m/s
            if speed_mps > 0.1:
                img.draw_string(blob.x(), blob.y() - 20, "Speed: %.2f m/s" % speed_mps)

                # Determine the color for the distance range
                if distance_m < min_distance:
                    color = (255, 0, 0) # red
                    warning = True
                else:
                    color = (0, 255, 0) # green
                    warning = False

                # Draw a rectangle around the blob with the appropriate color if the speed is above 0.1 m/s
                img.draw_rectangle(blob.rect(), color=color)

                # Show the distance above the blob if the speed is above 0.1 m/s
                if warning:
                    img.draw_string(blob.x(), blob.y() - 40, "WARNING: Too close!")
                img.draw_string(blob.x(), blob.y() - 60, "Distance: %.2f m" % distance_m)

        # Store the current distance and time for the next iteration
        prev_distance_m = distance_m
        prev_time = time.ticks_ms()

        # Add the current blob area to the list
        blob_areas.append(blob.area())

        # Remove the oldest blob area and distance if the list is too long
        if len(blob_areas) > 10:
            blob_areas.pop(0)
        if len(distances) > 10:
            distances.pop(0)

    # Wait for the next frame
    time.sleep(1.0 / fps)

```

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 6, 2023, 5:40pm UTC](https://forums.openmv.io/t/detecting-multiple-moving-objects-using-arduino-nicla-vision/8195/2 "2023-04-06T17:40:06Z")

</div>

Hi, please don’t post all your code and ask us to debug it in full for you. Please ask a specific question about a particular thing in your code that you are having trouble understanding.
