Skip to content

About

Autonomous drone that sees colored cubes and sorts them - Unity simulation driven by real-time OpenCV computer vision. Closed-loop perception to actuation over TCP.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Β 
Β 

Latest commit

Β 

History

7 Commits

Folders and files

Repository files navigation


🚁 Vision Drone Sorter

An autonomous drone that sees colored cubes and sorts them

A Unity simulation driven by a real-time OpenCV computer-vision brain


Unity Python OpenCV C# License: MIT

Perception β†’ Decision β†’ Actuation, all in a single closed loop.



🎬 Demo

Vision Drone Sorter β€” autonomous pick-and-place demo

β–Ά Watch the full video with audio Β Β·Β  preview above is a condensed, silent loop


πŸ“Œ Table of Contents


πŸ“– Overview

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.


πŸ–ΌοΈ How It Works

# 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.

🎯 Detection result

The vision pipeline annotates every detected cube with a color-matched bounding box:

OpenCV detection result with colored bounding boxes

πŸ“Έ Screenshots

Unity scene with scattered colored cubes and the drone awaiting capture
Scene overview β€” colored cubes scattered across the field, drone idle, awaiting a capture.
OpenCV detection script running with terminal output
Vision pipeline β€” main.py running HSV segmentation and streaming results to Unity.
Drone carrying a gripped cube across the scene
Autonomous transport β€” the drone grips a cube and flies it toward its matching drop-off zone.
Scene near completion with most cubes already sorted
Near completion β€” most cubes already delivered; the field clears as the sort finishes.

The annotated detection output is shown under How It Works.


✨ Features

  • πŸŽ₯ 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.

πŸ— Architecture

        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚           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)   β”‚  β”‚
        β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ—‚οΈ Project Structure

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

πŸš€ Getting Started

Prerequisites

  • Unity 2020.3 LTS (e.g. 2020.3.17f1) β€” install via Unity Hub
  • Python 3.8+

1 Β· Clone the repository

git clone https://github.com/tathagata48/vision-drone-sorter.git
cd vision-drone-sorter

2 Β· Set up the Python environment

python -m venv venv

# Windows
venv\Scripts\activate
# macOS / Linux
source venv/bin/activate

pip install -r requirements.txt

3 Β· Open the Unity project

  1. Open Unity Hub β†’ Add β†’ select this project folder.
  2. Open the scene Assets/Scenes/CVSystem.unity.
  3. Press β–Ά Play. The drone spins up a TCP server on port 65431 and waits for commands.

4 Β· Run the vision pipeline

With Unity in Play mode, capture a screenshot of the scene, then run:

python main.py

Note β€” update image_path in main.py to point at your captured screenshot. The script detects each cube, prints per-color counts, and streams sorting commands to the running Unity simulation.


🧩 Communication Protocol

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

πŸ› οΈ Tech Stack

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

πŸ—ΊοΈ Roadmap

  • 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

πŸ“œ License

Distributed under the MIT License. See LICENSE for full details.


Built with Unity, OpenCV, and a lot of hovering. βš™οΈπŸš

About

Autonomous drone that sees colored cubes and sorts them - Unity simulation driven by real-time OpenCV computer vision. Closed-loop perception to actuation over TCP.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages