This project demonstrates how to use the SAM2 (Segment Anything Model 2) for masking hands in videos. It provides both interactive and script-based implementations for generating masked videos.
- Environment Setup
- SAM2 Repository Setup
- Checkpoint Setup
- Running the Masking Function
- Output Files
- NOTE
- Python 3.10 or above is required.
- Create a Python Virtual Environment:
python -m venv myenv
- Activate the Environment:
source myenv/bin/activate - Install Dependencies:
pip install -r requirements.txt
- Clone the SAM2 Repository & navigate into it:
git clone https://github.com/facebookresearch/sam2.git && cd sam2
- Install the SAM2 Package:
pip install -e .
- Navigate to the checkpoints folder:
cd checkpoints - Download the required model checkpoints:
- Download script:
./download_ckpts.sh
- Alternatively, download the checkpoints manually from the SAM2 GitHub page and place them in the checkpoints folder.
- Download script:
- Move the
trackHands.ipynbnotebook to your working directory. - Open the notebook and modify the following variables if needed:
video_path: Path to the input video file.output_video_path: Path to save the masked video.
- Execute the notebook cells to generate the masked video.
- Move the
trackHands.pyscript to your working directory. - Modify the following variables in the script if needed:
video_path: Path to the input video file.output_video_path: Path to save the masked video.
- Run the script:
python trackHands.py
Use the provided test.mp4 file or replace it with your own video file.
The following output files will be generated:
- output_video_Large_model.mp4: Masked video using the SAM2 large model.
- output_video_small_model.mp4: Masked video using the SAM2 small model.
MPS Systems: The script uses torch.mps.empty_cache() to avoid errors on MPS-based systems. If you're using CUDA or CPU, comment out or modify this line accordingly.