Example Code for MicroPython-Pedestrian Detection

This example demonstrates how to use the ESP32-P4 to detect pedestrians in an image. The ESP32-P4 will mark the detected pedestrians and save the annotated image as a new file.

Hardware Preparation

Software Preparation

Usage steps

  1. Upload the pedestrian.jpg file to the ESP32-P4.
  2. Run the script to view the position information of the detected pedestrians.
  3. Click the Stop/Restart Backend Process button to view the marked image on the MicroPython device.

pedestrian.jpg

from espdl import HumanDetector
from jpeg import Decoder, Encoder


decoder = Decoder()
encoder = Encoder(width=640, height=480, pixel_format="RGB888")
human_detector = HumanDetector(width=640, height=480)

# Capture and process the image
img = open("pedestrian.jpg", "rb").read()  # Capture the original image (usually in JPEG format)
framebuffer = decoder.decode(img)  # Convert to RGB888 format
# Convert memoryview to bytearray for modification
framebuffer = bytearray(framebuffer)
# Run pedestrian detection
results = human_detector.run(framebuffer)

# Draw bounding box
def draw_rectangle(buffer, width, height, x, y, w, h, color=(255, 0, 0)):
    """
    Draw a rectangular bounding box on an RGB888 format image buffer
    :param buffer: Image buffer
    :param width: Image width
    :param height: Image height
    :param x: X-coordinate of the top-left corner of the bounding box
    :param y: Y-coordinate of the top-left corner of the bounding box
    :param w: Width of the bounding box
    :param h: Height of the bounding box
    :param color: Bounding box color (in RGB format)
    """
    # Helper function: Set color for a single pixel
    def set_pixel(buffer, width, x, y, color):
        offset = (y * width + x) * 3
        buffer[offset] = color[0]  # R (Red channel)
        buffer[offset + 1] = color[1]  # G (Green channel)
        buffer[offset + 2] = color[2]  # B (Blue channel)

    # Draw top edge of the bounding box
    for i in range(x, x + w):
        if 0 <= i < width and 0 <= y < height:
            set_pixel(buffer, width, i, y, color)

    # Draw bottom edge of the bounding box
    for i in range(x, x + w):
        if 0 <= i < width and 0 <= y + h < height:
            set_pixel(buffer, width, i, y + h, color)

    # Draw left edge of the bounding box
    for j in range(y, y + h):
        if 0 <= j < height and 0 <= x < width:
            set_pixel(buffer, width, x, j, color)

    # Draw right edge of the bounding box
    for j in range(y, y + h):
        if 0 <= j < height and 0 <= x + w < width:
            set_pixel(buffer, width, x + w, j, color)

# Draw bounding boxes for detected pedestrians on the image
for face in results:
    print(face)
    x1, y1, x2, y2 = face['box']
    draw_rectangle(framebuffer, 640, 480, x1, y1, x2 - x1, y2 - y1, color=(255, 0, 0))  # Use red bounding box

# Re-encode the image with bounding boxes to JPEG format and save
marked_img = encoder.encode(framebuffer)
with open("pedestrian_marked.jpg", "wb") as f:
    f.write(marked_img)

Result

Result

Marked Result

Marked Result

Was this article helpful?

TOP