This project is a complete computer vision pipeline for detecting, tracking, and analyzing the behavior of crystals in video streams. It features a user-friendly graphical interface (GUI) built with Tkinter for easy operation.
The core of the system uses a YOLOv8 model for initial object detection, a DeepSort tracker for maintaining object identities across frames, and a custom-trained ResNet50 embedding network to robustly re-identify crystals even after rotation or temporary occlusion.
- Real-Time Detection & Tracking: Identifies and tracks multiple crystals simultaneously in a video file.
- Robust Re-Identification: Utilizes a custom PyTorch embedding model trained with cosine similarity loss to prevent ID switching when crystals flip or overlap.
- Dynamic Statistics: Calculates key metrics like unique crystal count and average velocity.
- Interactive GUI: A user-friendly interface built with Tkinter allows for easy video selection, real-time visualization, and control over the analysis.
- Visualization Tools: Renders bounding boxes and IDs on each frame for immediate visual feedback.
- Data Export: Optionally saves all processed frames and can compile them into a final output video.
- Python 3.10+
- PyTorch: For the custom embedding/re-identification model.
- YOLOv8 (Ultralytics): For high-performance object detection.
- DeepSORT: For real-time object tracking.
- OpenCV: For video and image processing.
- Tkinter: For the graphical user interface.
- UV: Fast Python package and project manager.
- Git LFS: For managing large model files.
This project contains large model files and uses UV for fast, reliable dependency management. Please follow these steps carefully to ensure the repository is set up correctly.
Before cloning, you must have Git LFS (Large File Storage) installed on your system.
- On macOS:
brew install git-lfs - On Windows/Linux: Download and install from the official website.
After installing, set it up by running this command once in your terminal:
git lfs installUV is a fast Python package manager that replaces pip and virtualenv.
On macOS/Linux:
curl -LsSf https://astral.sh/uv/install.sh | shOn Windows:
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"Alternative (using pip):
pip install uvAfter installation, restart your terminal or run:
source ~/.bashrc # or source ~/.zshrc on macOSgit clone <your-repository-url>
cd CrystalDectorGit LFS will automatically download the large model files (.pt and .pth files) during cloning.
This project requires Python 3.10 or higher. Check your Python version:
python3 --versionIf you need to install Python 3.10+:
On macOS (using Homebrew):
brew install [email protected]Using UV (recommended):
uv python install 3.11
uv python pin 3.11On Windows/Linux: Download from python.org
UV will automatically create a virtual environment and install all dependencies:
uv syncThis command will:
- Create a
.venvdirectory in your project - Install all required packages from
pyproject.toml - Generate a
uv.lockfile for reproducible builds
Ensure these files are in your project root directory:
YOLO_best.pt- YOLOv8 detection model (managed by Git LFS)cosine_epoch30.pth- Custom ResNet50 embedding model (managed by Git LFS)
If these files are missing after cloning, pull them manually:
git lfs pullThe easiest way to use the tracker is through the graphical interface:
uv run python tracker_gui.pyGUI Features:
- Click "Select Video File" to choose your input video
- Configure options:
- Save Annotated Frames: Save processed frames to
modified_images/folder - Draw Velocity Vectors: Visualize crystal movement
- Save Annotated Frames: Save processed frames to
- Click "Start Analysis" to begin processing
- Monitor progress in real-time
- View statistics after completion
- Optionally click "Create Video" to compile frames into an output video
For basic tracking without GUI:
uv run python tracker_deepsort.pyNote: Edit the working_directory variable in the script to match your project location.
To create a video from saved frames:
uv run python create_video.pyBefore running tracker_deepsort.py, update these variables in the script:
working_directory = "/path/to/your/CrystalDector" # Update this path
video_source = r"video_4.mp4" # Your input video
draw_bounding_boxes = True # Draw boxes around crystals
draw_velocity_vector = True # Draw velocity arrows
save_images = True # Save annotated framesCrystalDector/
├── tracker_gui.py # GUI application (recommended)
├── tracker_deepsort.py # Command-line tracking script
├── train_resnet50_cosine.py # Embedding model training script
├── create_video.py # Convert frames to video
├── pyproject.toml # Project dependencies (UV config)
├── uv.lock # Locked dependency versions
├── requirements.txt # Legacy requirements (for reference)
├── YOLO_best.pt # YOLOv8 detection model
├── cosine_epoch30.pth # ResNet50 embedding model
├── modified_images/ # Output folder for annotated frames
└── README.md # This file
uv add package-nameuv remove package-nameuv lock --upgradeuv lock --upgrade-package numpyuv pip listThe system generates:
-
Console Statistics:
- Number of unique crystals tracked
- Average velocity
- Standard deviation of velocity
-
Annotated Frames (if enabled):
- Saved to
modified_images/directory - Each frame shows bounding boxes and crystal IDs
- Optional velocity vectors
- Saved to
-
Output Video (optional):
- Compiled from annotated frames
- Named
{original_filename}_tracked.mp4
Make sure you're running scripts with uv run:
uv run python tracker_gui.pyOr activate the virtual environment:
source .venv/bin/activate # macOS/Linux
.venv\Scripts\activate # Windows
python tracker_gui.pyPull large files with Git LFS:
git lfs pullEnsure you're using Python 3.10+:
python3 --version
uv python install 3.11
uv python pin 3.11
uv syncThe system automatically uses Apple Metal (MPS) GPU acceleration when available. Check the console output to confirm:
Using device: Apple Metal (MPS GPU)
Remove the lock file and resync:
rm uv.lock
uv syncTo train your own embedding model on custom crystal images:
uv run python train_resnet50_cosine.py --data-root /path/to/crystal/images --epochs 30The script expects a folder of individual crystal images and will train a ResNet50 model with cosine similarity loss.
- Operating System: macOS, Linux, or Windows
- Python: 3.10 or higher
- RAM: 8GB minimum, 16GB recommended
- GPU: Optional but recommended (CUDA for NVIDIA, MPS for Apple Silicon)
- Storage: ~2GB for models and dependencies
- GPU Acceleration: The system automatically uses available GPU (CUDA/MPS)
- Frame Skipping: Adjust
FRAME_SKIPin scripts for faster processing - Video Resolution: Resize frames for faster processing (see commented code in
tracker_deepsort.py) - Batch Size: Modify detection batch size for memory optimization
[MIT, same as YOLO usage]
- YOLOv8 by Ultralytics
- DeepSORT tracking algorithm
- PyTorch and torchvision
- UV by Astral