Perception β Decision β Actuation, all in a single closed loop.
- Overview
- How It Works
- Screenshots
- Features
- Architecture
- Project Structure
- Getting Started
- Communication Protocol
- Tech Stack
- Roadmap
- License
Vision Drone Sorter is a closed-loop robotics simulation that fuses computer vision with an autonomous aerial agent. A virtual drone in Unity surveys a scene scattered with colored cubes, a Python + OpenCV pipeline identifies each cube by color, and the drone then autonomously picks up every cube and delivers it to its matching drop-off zone β no human in the loop.
It is a compact, end-to-end demonstration of the sense β perceive β decide β act cycle that underpins real robotics:
Unity Camera βββΊ Screenshot βββΊ OpenCV Detection βββΊ TCP Command βββΊ Drone Picks & Sorts
(sense) (capture) (perceive) (decide) (act)
Unity owns the world β physics, the drone, and the environment. Python + OpenCV act as the brain that perceives the scene and issues commands across a lightweight TCP socket.
| # | Stage | Component | What happens |
|---|---|---|---|
| 1 | Capture | Takephoto.cs |
A dedicated Unity camera renders the live scene to a PNG screenshot. |
| 2 | Perceive | main.py (OpenCV) |
The image is converted to HSV; red / green / blue masks isolate cubes, contours are extracted, and each cube is counted & classified. |
| 3 | Decide | main.py (socket) |
For every detected cube, a color command is streamed to Unity over TCP port 65431. |
| 4 | Act | DroneMovement.cs |
Unity receives the command and runs a coroutine that flies the drone to the cube, grips it, and drops it in the matching zone. |
| 5 | Grip | pickdrop.cs |
Parents the cube to the drone's gripper on pickup and releases it on drop. |
The vision pipeline annotates every detected cube with a color-matched bounding box:
The annotated detection output is shown under How It Works.
- π₯ Vision-based perception β captures the live scene and detects red, green & blue cubes via HSV color segmentation.
- π§ Counting & classification β tallies how many cubes of each color exist and tags every detection.
- π Real-time TCP bridge β Python and Unity stay in sync over a minimal socket protocol on
localhost. - π Autonomous pick-and-place β the drone lifts, navigates, grips, transports, and drops each cube at its color-matched zone.
- β»οΈ Sequential task queue β commands are queued and executed safely on Unity's main thread, one cube at a time.
- πΈ In-engine screenshot capture β a purpose-built camera renders the scene straight into the vision pipeline.
βββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β UNITY β β PYTHON + OpenCV β
β β β β
β ββββββββββββββββββββββββββ β screenshot β ββββββββββββββββββββββββββ β
β β Takephoto.cs (cam) ββββΌββββββββββββββββΊβ β HSV mask + contours β β
β ββββββββββββββββββββββββββ β (.png) β βββββββββββββ¬βββββββββββββ β
β β β β colors[] β
β ββββββββββββββββββββββββββ β TCP :65431 β βββββββββββββΌβββββββββββββ β
β β DroneMovement.cs ββββΌβββββββββββββββββΌβββ socket client β β
β β (flight + queue) β β "red/green/ β β move_drone(color) β β
β βββββββββββββ¬βββββββββββββ β blue" β ββββββββββββββββββββββββββ β
β β β β β
β βββββββββββββΌβββββββββββββ β ββββββββββββββββββββββββββββββββ
β β pickdrop.cs (grip) β β
β ββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββ
vision-drone-sorter/
βββ Assets/
β βββ Scenes/
β β βββ CVSystem.unity # Main simulation scene
β βββ Scripts/
β β βββ DroneMovement.cs # TCP listener + autonomous flight & pick-and-place
β β βββ Takephoto.cs # In-engine camera screenshot capture
β β βββ pickdrop.cs # Gripper attach / release logic
β βββ Materials/ Β· Textures/ # Scene look & feel
β βββ ... # Models & supporting assets
βββ ProjectSettings/ # Unity project configuration
βββ Packages/ # Unity package manifest
βββ main.py # Python OpenCV detection + drone controller
βββ requirements.txt # Python dependencies
βββ detected_cards_colored.png # Sample detection output
βββ demo.mp4 # Demo recording
βββ README.md
- Unity 2020.3 LTS (e.g.
2020.3.17f1) β install via Unity Hub - Python 3.8+
git clone https://github.com/tathagata48/vision-drone-sorter.git
cd vision-drone-sorterpython -m venv venv
# Windows
venv\Scripts\activate
# macOS / Linux
source venv/bin/activate
pip install -r requirements.txt- Open Unity Hub β Add β select this project folder.
- Open the scene
Assets/Scenes/CVSystem.unity. - Press βΆ Play. The drone spins up a TCP server on port
65431and waits for commands.
With Unity in Play mode, capture a screenshot of the scene, then run:
python main.pyNote β update
image_pathinmain.pyto point at your captured screenshot. The script detects each cube, prints per-color counts, and streams sorting commands to the running Unity simulation.
A minimal text protocol over TCP keeps the two worlds in lockstep:
| Field | Value |
|---|---|
| Host | localhost |
| Port | 65431 |
| Payload | One of red, green, blue (ASCII) |
| Flow control | Unity ignores new commands while the drone is isBusy, guaranteeing one clean pick-and-place at a time |
| Layer | Technology |
|---|---|
| Simulation | Unity 2020.3 LTS β 3D world, physics, coroutine-based motion |
| Scripting | C# β networking, flight control, gripper logic |
| Orchestration | Python 3 |
| Vision | OpenCV β HSV color segmentation & contour detection |
| Math | NumPy β array / mask operations |
| Transport | TCP sockets β inter-process bridge between Python and Unity |
- Replace fixed delays with an ACK-based handshake for faster, deterministic sorting
- Live camera feed instead of a single screenshot per run
- Support for additional colors and arbitrary drop-zone mapping
- Swap HSV thresholding for a lightweight ML detector
- On-screen HUD showing detection counts and drone state
Distributed under the MIT License. See LICENSE for full details.




