diff --git a/community_projects/NeoPixel/README.md b/community_projects/NeoPixel/README.md new file mode 100644 index 0000000..a2e174e --- /dev/null +++ b/community_projects/NeoPixel/README.md @@ -0,0 +1,51 @@ +# Hailo NeoPixel exampe +This exmaple is based on the detection pipeline. It detects a person and follows him with the leds. + +## Installation +Follow the installation flow in the main README file, and then continue folowing this README file. + +### Enable SPI +```bash +sudo raspi-config +``` + +- 3 Interface Options +- I4 SPI +- Yes +- reboot + +### Pins Connection +Based on https://github.com/vanshksingh/Pi5Neo +- Connect 5+ to 5V +- GND to GND +- Din to GPIO10 (SPI MOSI) + +### Navigate to the repository directory: +```bash +cd hailo-rpi5-examples +``` + +### Environment Configuration (Required for Each New Terminal Session) +Ensure your environment is set up correctly by sourcing the provided script. This script sets the required environment variables and activates the Hailo virtual environment. If the virtual environment does not exist, it will be created automatically. +```bash +source setup_env.sh +``` +### Navigate to the example directory: +```bash +cd community_projects/NeoPixel/ +``` +### Requirements Installation +Within the activated virtual environment, install the necessary Python packages: +```bash +pip install -r requirements.txt +``` + +### To Run the Simple Example: +```bash +python example.py +``` +### To Run the Real Example: +```bash +python follow_detection.py +``` +- To close the application, press `Ctrl+C`. diff --git a/community_projects/NeoPixel/example.py b/community_projects/NeoPixel/example.py new file mode 100644 index 0000000..87a08bb --- /dev/null +++ b/community_projects/NeoPixel/example.py @@ -0,0 +1,30 @@ +# Based on https://github.com/vanshksingh/Pi5Neo +# install using 'pip install pi5neo' +#Running Rainbow Wave (Rainbow colors move across the strip) +import time +from pi5neo import Pi5Neo + +def running_rainbow(neo, delay=0.05): + colors = [ + (255, 0, 0), # Red + (255, 127, 0), # Orange + (255, 255, 0), # Yellow + (0, 255, 0), # Green + (0, 0, 255), # Blue + (75, 0, 130), # Indigo + (148, 0, 211) # Violet + ] + + num_colors = len(colors) + while True: + for offset in range(neo.num_leds): + for i in range(neo.num_leds): + neo.set_led_color(i, *colors[(i + offset) % num_colors]) + neo.update_strip() + time.sleep(delay) + +# Initialize Pi5Neo with 10 LEDs +neo = Pi5Neo('/dev/spidev0.0', 10, 800) + +# Rainbow wave across the strip +running_rainbow(neo) \ No newline at end of file diff --git a/community_projects/NeoPixel/follow_detection.py b/community_projects/NeoPixel/follow_detection.py new file mode 100644 index 0000000..7936184 --- /dev/null +++ b/community_projects/NeoPixel/follow_detection.py @@ -0,0 +1,79 @@ +import gi +gi.require_version('Gst', '1.0') +from gi.repository import Gst, GLib +import os +import sys +import numpy as np +import cv2 +import hailo +sys.path.append('../../basic_pipelines') + +from hailo_rpi_common import ( + get_caps_from_pad, + get_numpy_from_buffer, + app_callback_class, +) +from detection_pipeline import GStreamerDetectionApp + +# Based on https://github.com/vanshksingh/Pi5Neo +# Pins connections: +# Connect 5+ to 5V +# GND to GND +# Din to GPIO10 (SPI MOSI) + +# install using 'pip install pi5neo' +from pi5neo import Pi5Neo + +# ----------------------------------------------------------------------------------------------- +# User-defined class to be used in the callback function +# ----------------------------------------------------------------------------------------------- +# Inheritance from the app_callback_class +class user_app_callback_class(app_callback_class): + def __init__(self): + super().__init__() + self.num_leds = 10 + self.neo = Pi5Neo('/dev/spidev0.0', self.num_leds, 800) + self.update_rate = 4 +# ----------------------------------------------------------------------------------------------- +# User-defined callback function +# ----------------------------------------------------------------------------------------------- + +# This is the callback function that will be called when data is available from the pipeline +def app_callback(pad, info, user_data): + # Using the user_data to count the number of frames + user_data.increment() + # run only every user_data.update_rate frames + if (user_data.get_count() % user_data.update_rate): + return Gst.PadProbeReturn.OK + # Get the GstBuffer from the probe info + buffer = info.get_buffer() + # Check if the buffer is valid + if buffer is None: + return Gst.PadProbeReturn.OK + + # Get the detections from the buffer + roi = hailo.get_roi_from_buffer(buffer) + detections = roi.get_objects_typed(hailo.HAILO_DETECTION) + + # Parse the detections + for detection in detections: + label = detection.get_label() + bbox = detection.get_bbox() + confidence = detection.get_confidence() + if label == "person": + # control leds according to person X location + x = (bbox.xmin() + bbox.xmax()) / 2 + # select led to light + ind = int(user_data.num_leds * x) + print(f'setting led {ind}') + user_data.neo.fill_strip(0, 0, 0) # clear all leds + user_data.neo.set_led_color(ind, 0, 0, 255) + user_data.neo.update_strip() + # exit after first detection + return Gst.PadProbeReturn.OK + +if __name__ == "__main__": + # Create an instance of the user app callback class + user_data = user_app_callback_class() + app = GStreamerDetectionApp(app_callback, user_data) + app.run() diff --git a/community_projects/NeoPixel/requirements.txt b/community_projects/NeoPixel/requirements.txt new file mode 100644 index 0000000..626dafe --- /dev/null +++ b/community_projects/NeoPixel/requirements.txt @@ -0,0 +1 @@ +pi5neo \ No newline at end of file