This project implements a system for detecting a plus sign and a red dot in live video using Raspberry Pi and computer vision techniques, and integrates a capacitive sensing circuit for laser harmonisation feedback. The repository contains code for dataset generation, model training, object detection, red-dot localization, and Raspberry Pi GPIO interfacing.
-
- Synthetic Dataset Creation
- Background Augmentation
-
- YOLOv8 Plus-Sign Detector
- PixelLib Instance Segmentation (optional)
-
Object Detection & Red Dot Localization
- YOLOv8 Inference
- Red Dot Detection (HSV Mask)
- Distance & Direction Calculations
-
- Picamera2 Configuration
- GPIO Capacitive Sensor (harmonisation)
The Laser Harmonisation Project combines computer vision and hardware interfacing to detect alignment of a red dot relative to a plus sign marker. A YOLOv8 model detects the plus sign in real-time video, while a simple HSV-based mask localizes the red dot. The system computes Euclidean distance and directional angle between the marker and the dot. An accompanying capacitive sensing circuit on a Raspberry Pi provides feedback on harmonisation status.
- Python 3.7+
- OpenCV
- NumPy
- Ultralytics YOLOv8 (
ultralyticspackage) - Picamera2 (for Raspberry Pi OS)
- RPi.GPIO (for Raspberry Pi GPIO)
- PixelLib & Mask R-CNN (optional for instance segmentation)
Install dependencies:
pip install opencv-python numpy ultralytics picamera2 RPi.GPIO pixellibThe script in dataset_generation/plus_sign_generator.py produces num_signs synthetic images of plus signs on a Cartesian plane and generates corresponding XML annotations (Pascal VOC format).
-
Parameters:
plane_size: Canvas size (pixels)num_signs: Number of images to generatearm_length: Half-length of plus sign armsline_width: Plus sign thickness
The script in dataset_generation/background_overlay.py overlays a transparent plus sign template onto a variety of background images at random scales and positions, saving results in data/output.
Place your annotated dataset in YOLO format under yolo_dataset/ and update the data.yaml file. Train using:
yolo task=detect mode=train model=yolov8n.pt data=data.yaml epochs=300 imgsz=640The resulting .pt model will be used for inference in detect_plus_red.py.
Use pixellib_train.py to train a Mask R-CNN model on a custom dataset:
from pixellib.custom_train import instance_custom_training
# Example
dataset_path = "path/to/dataset"
num_classes = 2 # plus sign + background
model_name = "my_trained_model"
train_pixellib_model(dataset_path, num_classes, model_name)Process video using:
detect_objects_in_video("input.mp4", "my_trained_model/mask_rcnn_model.h5", "output.mp4")Code file: detect_plus_red.py
-
Load YOLO Model
model = YOLO("/path/to/red_plus.pt")
-
Frame Capture via Picamera2
-
Red Dot Detection using HSV thresholding in
find_red_dot() -
Process YOLO Detections:
-
Draw bounding box around plus sign
-
Compute center coordinate
-
If red dot found, draw circle and compute:
- Euclidean distance (
calculate_distance) - Direction angle (
calculate_direction)
- Euclidean distance (
-
-
Display original frame and zoomed crop with annotations
In detect_plus_red.py, configure the camera:
picam2 = Picamera2()
picam2.video_configuration.controls.FrameRate = 30
picam2.configure(picam2.create_preview_configuration(main={"format": 'RGB888', "size": (1280, 720)}))
picam2.start()Script: rc_sensor.py
- Measures charge time on pin 7
- Returns
"harmonisation is done"when count ≤ 50
def rc_time(pin):
# discharge then measure
...
return "harmonisation is done" if count<=50 else "In process"├── dataset_generation/
│ ├── plus_sign_generator.py
│ └── background_overlay.py
├── yolo_dataset/
│ ├── images/
│ ├── labels/
│ └── data.yaml
├── detect_plus_red.py
├── pixellib_train.py
├── rc_sensor.py
├── requirements.txt
└── README.md
-
Generate or prepare dataset
-
Train YOLO model or PixelLib model
-
Copy trained model to Raspberry Pi path
-
Run detection:
python3 detect_plus_red.py
-
Monitor output windows; press
qto quit -
Run capacitive sensor script:
python3 rc_sensor.py
- No red dot detected: Adjust HSV ranges in
find_red_dot() - Weak YOLO performance: Increase dataset size or epochs
- GPIO errors: Ensure correct pin numbering mode and wiring