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.
#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)