Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 70 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,96 @@
# Swarm Robotics Localization

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

# FYP-localization-robots
Python tooling for ArUco-based localization, marker generation, and camera calibration.

Python repository for address the Localization problem of Swarm Robotics
## Project structure

### Requirements
- markers/ - Generate ArUco marker PNGs and a PDF.
- scripts/ - Main localization pipeline (OpenCV + MQTT) and supporting tools.
- scripts/board/ - Camera calibration workflow.
- scripts/previousScripts/ - Legacy demos and prototypes.
- docs/ - External documentation entry point.

Please install following pip packages if they aren't pre-installed
## Shared virtual environment

```
pip install numpy
This repo uses a shared root .venv and a single global requirements.txt.

pip install aruco
pip install paho-mqtt
pip3 install opencv-python
pip3 install opencv-contrib-python
pip3 install pyyaml
Windows PowerShell:

```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

or use following command
macOS/Linux:

```
pip install -r requirement.txt
```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

You need to copy and rename the following files in _scripts_ directory and fill the necessary configuration details before run the scripts.
## Configuration

- config-mapping_sample.yaml INTO config-mapping.yaml
- config-mqtt_sample.yaml INTO config-mqtt.yaml
If you run `./run.sh` (Unix-like shells), missing configs are auto-created from samples on first run.
If you run `python scripts/script.py` directly, copy the sample configs in scripts/ first:

### Run the scripts
- scripts/config-mapping_sample.yaml -> scripts/config-mapping.yaml
- scripts/config-mqtt_sample.yaml -> scripts/config-mqtt.yaml

You can try scripts on the ./scripts/ directory
## Entry points

### Build a executable file (not working so far)

Install PyInstaller from PyPI:
Markers:

```powershell
cd markers
python marker_generator.py
```
pip install pyinstaller

Localization pipeline:

```powershell
cd scripts
python script.py
```

Go to your program’s directory and run:
Calibration:

```powershell
cd scripts/board
python calibrate.py
```
pyinstaller --onefile script.py

Legacy demos:

```powershell
cd scripts/previousScripts
python script2.py
```

After, please make sure to copy the './board' folder into the directory which executes the exe file.
## Outputs

- markers/generated/ - PNG marker images
- markers/aruco_markers.pdf - Generated marker PDF
- scripts/board/calibration_data.txt - Saved calibration data

## Troubleshooting

- cv2.aruco missing: install opencv-contrib-python (not opencv-python only).
- MyPy import-untyped: reportlab has no type hints; ignore or add a stub package.
- Camera not opening: try a different camera index in scripts/script.py.
- MQTT connection errors: verify scripts/config-mqtt.yaml values and broker reachability.

## Notes

- run.sh is a convenience script for Unix-like shells. On Windows, use the commands above.
- Raspberry Pi camera script is documented in scripts/README.md.

### Read More
## Read more

- [ar-markers 0.5.0](https://pypi.org/project/ar-markers/)
- [Augmented Reality using ArUco Markers in OpenCV (C++ / Python)](https://www.learnopencv.com/augmented-reality-using-aruco-markers-in-opencv-c-python/)
- [OpenCV: Detection of ArUco Markers](https://docs.opencv.org/trunk/d5/dae/tutorial_aruco_detection.html)
- [OpenCV: Detection of ArUco Boards](https://docs.opencv.org/master/db/da9/tutorial_aruco_board_detection.html)
- [Calibrating the board](https://mecaruco2.readthedocs.io/en/latest/notebooks_rst/Aruco/sandbox/ludovic/aruco_calibration_rotation.html)
- [ar-markers](https://pypi.org/project/ar-markers/)
- [Augmented Reality using ArUco Markers in OpenCV](https://www.learnopencv.com/augmented-reality-using-aruco-markers-in-opencv-c-python/)
- [OpenCV ArUco detection](https://docs.opencv.org/master/d5/dae/tutorial_aruco_detection.html)
- [OpenCV ArUco board detection](https://docs.opencv.org/master/db/da9/tutorial_aruco_board_detection.html)
29 changes: 29 additions & 0 deletions markers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Markers

Generate ArUco marker PNGs and a PDF sheet.

## Setup

Use the shared root .venv and the global requirements.txt:

```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r ..\requirements.txt
```

## Run

```powershell
python marker_generator.py
```

## Outputs

- generated/ - Marker PNG files
- aruco_markers.pdf - PDF sheet

## Troubleshooting

- cv2.aruco missing: install opencv-contrib-python.
- reportlab import warnings: ignore MyPy import-untyped warnings or add stubs.
Binary file added markers/aruco_markers.pdf
Binary file not shown.
72 changes: 62 additions & 10 deletions markers/marker_generator.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,69 @@
# This is the updated version of the aruco marker generater.
# Using this you can generate pdf at once

import cv2 as cv
import numpy as np
import os
from reportlab.platypus import SimpleDocTemplate, Image, Table # type: ignore
from reportlab.lib import colors # type: ignore
from reportlab.lib.pagesizes import A4 # type: ignore
from reportlab.lib.units import mm # type: ignore

# Output PDF file
pdf_path = "aruco_markers.pdf"

# Load the predefined dictionary
dictionary = cv.aruco.Dictionary_get(cv.aruco.DICT_6X6_250)
# Create temporary folder for images
temp_dir = "./generated"
os.makedirs(temp_dir, exist_ok=True)

# Generate the marker
# Load predefined dictionary
dictionary = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_6X6_250)

markers = [int(x) for x in range(0, 10)]
# Marker settings
markers = list(range(0, 10))
image_size = 600

for m in markers:
markerImage = np.zeros((image_size, image_size), dtype=np.uint8)
# Generate marker images
image_paths = []
for marker_id in markers:
marker_image = cv.aruco.generateImageMarker(
dictionary,
marker_id,
image_size
)

path = os.path.join(temp_dir, f"marker{marker_id}.png")
cv.imwrite(path, marker_image)
image_paths.append(path)

print("Markers generated.")

# Create PDF
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
elements = []

# Prepare images in grid (2 columns)
table_data = []
row = []

for i, img_path in enumerate(image_paths):
img = Image(img_path, width=70*mm, height=70*mm)
row.append(img)

if len(row) == 2:
table_data.append(row)
row = []

# Add remaining images if odd count
if row:
table_data.append(row)

table = Table(table_data)
table.setStyle([
('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
('ALIGN', (0, 0), (-1, -1), 'CENTER')
])

elements.append(table)
doc.build(elements)

# drawMarkers(dict, marker_id, pixel_size, output_image, border_width)
markerImage = cv.aruco.drawMarker(dictionary, m, image_size, markerImage, 1);
cv.imwrite("./generated/marker" + str(m) + ".png", markerImage);
print(f"PDF created successfully: {pdf_path}")
Binary file removed markers/pdf/ar_markers_0-5.pdf
Binary file not shown.
Binary file removed markers/pdf/ar_markers_5-9.pdf
Binary file not shown.
11 changes: 5 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
opencv-python==4.3.0.36
numpy==1.22.0
ar-markers==0.5.0
paho-mqtt==1.5.0
opencv-contrib-python==4.5.1.48
pyyaml==5.4
numpy
opencv-contrib-python
paho-mqtt
pyyaml
reportlab
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh

cd ./scripts

python3 ./script.py
49 changes: 49 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Localization Scripts

Main OpenCV + MQTT localization pipeline and support scripts.

## Setup

Use the shared root .venv and the global requirements.txt:

```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r ..\requirements.txt
```

## Configuration

If you run `../run.sh`, missing configs are auto-created from samples on first run.
If you run `python script.py` directly, copy the sample configs before running:

- config-mapping_sample.yaml -> config-mapping.yaml
- config-mqtt_sample.yaml -> config-mqtt.yaml

## Run

Main localization pipeline:

```powershell
python script.py
```

Raspberry Pi camera demo (Raspberry Pi OS only):

```powershell
python pycamera.py
```

## Outputs

- No files are written by default. Results are shown in the OpenCV window and published to MQTT topics.

## OS notes

- pycamera.py requires the Raspberry Pi camera stack and the picamera package.

## Troubleshooting

- cv2.aruco missing: install opencv-contrib-python.
- Camera not opening: change camera index in script.py.
- MQTT errors: verify broker settings in config-mqtt.yaml.
32 changes: 32 additions & 0 deletions scripts/board/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Camera Calibration

Calibrate the camera from chessboard images and write calibration_data.txt.

## Setup

Use the shared root .venv and the global requirements.txt:

```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r ..\..\requirements.txt
```

## Inputs

- sample/\*.jpg - Chessboard images (7x6 corners) for calibration.

## Run

```powershell
python calibrate.py
```

## Outputs

- calibration_data.txt - Camera matrix and distortion coefficients.

## Troubleshooting

- No corners detected: ensure the chessboard has 7x6 inner corners and good lighting.
- cv2 missing: install opencv-contrib-python.
Loading