This commit is contained in:
pq-yang
2026-03-16 06:45:38 +00:00
parent 12eeaa10fc
commit 49a4b36032
3 changed files with 59 additions and 83 deletions
+34 -33
View File
@@ -67,33 +67,31 @@
## 🔧 Installation ## 🔧 Installation
### From PyPI / Source
```bash
# install from repo
pip install git+https://github.com/pq-yang/MatAnyone2.git#egg=matanyone2
# or install optional extras
pip install git+https://github.com/pq-yang/MatAnyone2.git#egg=matanyone2[gui] # Gradio demo + PySide6
pip install git+https://github.com/pq-yang/MatAnyone2.git#egg=matanyone2[dev] # development / evaluation tools
pip install git+https://github.com/pq-yang/MatAnyone2.git#egg=matanyone2[all] # everything
```
### Conda ### Conda
```bash 1. Clone Repo
conda create -n matanyone2 python=3.10 -y ```bash
conda activate matanyone2 git clone https://github.com/pq-yang/MatAnyone2
pip install git+https://github.com/pq-yang/MatAnyone2.git#egg=matanyone2[all] cd MatAnyone2
``` ```
### uv (recommended) 2. Create Conda Environment and Install Dependencies
```bash
# create new conda env
conda create -n matanyone2 python=3.10 -y
conda activate matanyone2
# install python dependencies
pip install -e .
# [optional] install python dependencies for gradio demo
pip3 install -r hugging_face/requirements.txt
```
### uv
You may also install via [uv](https://docs.astral.sh/uv/):
```bash ```bash
# create a new project and add matanyone2 # create a new project and add matanyone2
uv init my-matting-project && cd my-matting-project uv init my-matting-project && cd my-matting-project
uv add matanyone2@git+https://github.com/pq-yang/MatAnyone2.git uv add matanyone2@git+https://github.com/pq-yang/MatAnyone2.git
# or with optional extras
uv add matanyone2[gui]@git+https://github.com/pq-yang/MatAnyone2.git
uv add matanyone2[all]@git+https://github.com/pq-yang/MatAnyone2.git
``` ```
## 🔥 Inference ## 🔥 Inference
@@ -122,23 +120,29 @@ Run the following command to try it out:
```shell ```shell
# intput format: video folder # intput format: video folder
matanyone2 -i inputs/video/test-sample1 -m inputs/mask/test-sample1.png python inference_matanyone2.py -i inputs/video/test-sample1 -m inputs/mask/test-sample1.png
# intput format: mp4 # intput format: mp4
matanyone2 -i inputs/video/test-sample2.mp4 -m inputs/mask/test-sample2.png python inference_matanyone2.py -i inputs/video/test-sample2.mp4 -m inputs/mask/test-sample2.png
# or via python
python inference_matanyone2.py -i inputs/video/test-sample1 -m inputs/mask/test-sample1.png
``` ```
The results will be saved in the `results` folder, including the foreground output video and the alpha output video. - The results will be saved in the `results` folder, including the foreground output video and the alpha output video.
- If you want to save the results as per-frame images, you can set `--save-image`.
- If you want to set a limit for the maximum input resolution, you can set `--max-size`, and the video will be downsampled if min(w, h) exceeds. By default, we don't set the limit.
### Python API (recommended 🔥) ### uv
If you install via uv, you may try the following command:
```shell
matanyone2 -i inputs/video/test-sample1 -m inputs/mask/test-sample1.png
```
- Run `matanyone2 --help` for a full list of options.
### Python API 🤗
You can load the model directly from Hugging Face using `from_pretrained` and run inference programmatically: You can load the model directly from Hugging Face using `from_pretrained` and run inference programmatically:
```python ```python
from matanyone2 import MatAnyone2, InferenceCore from matanyone2 import MatAnyone2, InferenceCore
model = MatAnyone2.from_pretrained("not-lain/matanyone2") model = MatAnyone2.from_pretrained("PeiqingYang/MatAnyone2")
processor = InferenceCore(model, device="cuda:0") processor = InferenceCore(model, device="cuda:0")
processor.process_video( processor.process_video(
input_path="inputs/video/test-sample2.mp4", input_path="inputs/video/test-sample2.mp4",
@@ -146,9 +150,6 @@ processor.process_video(
output_path="results", output_path="results",
) )
``` ```
- If you want to save the results as per-frame images, you can set `--save-image`.
- If you want to set a limit for the maximum input resolution, you can set `--max-size`, and the video will be downsampled if min(w, h) exceeds. By default, we don't set the limit.
- Run `matanyone2 --help` for a full list of options.
## 🎪 Interactive Demo ## 🎪 Interactive Demo
To get rid of the preparation for first-frame segmentation mask, we prepare a gradio demo on [hugging face](https://huggingface.co/spaces/PeiqingYang/MatAnyone2) and could also **launch locally**. Just drop your video/image, assign the target masks with a few clicks, and get the the matting results! To get rid of the preparation for first-frame segmentation mask, we prepare a gradio demo on [hugging face](https://huggingface.co/spaces/PeiqingYang/MatAnyone2) and could also **launch locally**. Just drop your video/image, assign the target masks with a few clicks, and get the the matting results!
@@ -158,7 +159,7 @@ To get rid of the preparation for first-frame segmentation mask, we prepare a gr
```shell ```shell
cd hugging_face cd hugging_face
# install GUI dependencies (if not already installed via pip install -e ".[gui]") # install GUI dependencies
pip3 install -r requirements.txt # FFmpeg required pip3 install -r requirements.txt # FFmpeg required
# launch the demo # launch the demo
+3 -12
View File
@@ -4,6 +4,7 @@ import random
import numpy as np import numpy as np
import torch import torch
import torchvision
IMAGE_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG') IMAGE_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG')
VIDEO_EXTENSIONS = ('.mp4', '.mov', '.avi', '.MP4', '.MOV', '.AVI') VIDEO_EXTENSIONS = ('.mp4', '.mov', '.avi', '.MP4', '.MOV', '.AVI')
@@ -11,18 +12,8 @@ VIDEO_EXTENSIONS = ('.mp4', '.mov', '.avi', '.MP4', '.MOV', '.AVI')
def read_frame_from_videos(frame_root): def read_frame_from_videos(frame_root):
if frame_root.endswith(VIDEO_EXTENSIONS): # Video file path if frame_root.endswith(VIDEO_EXTENSIONS): # Video file path
video_name = os.path.basename(frame_root)[:-4] video_name = os.path.basename(frame_root)[:-4]
cap = cv2.VideoCapture(frame_root) frames, _, info = torchvision.io.read_video(filename=frame_root, pts_unit='sec', output_format='TCHW') # RGB
fps = cap.get(cv2.CAP_PROP_FPS) fps = info['video_fps']
if fps <= 0:
fps = 24
frames = []
while True:
ret, frame = cap.read()
if not ret:
break
frames.append(frame[..., [2, 1, 0]]) # BGR to RGB, HWC
cap.release()
frames = torch.Tensor(np.array(frames)).permute(0, 3, 1, 2).contiguous() # TCHW
else: else:
video_name = os.path.basename(frame_root) video_name = os.path.basename(frame_root)
frames = [] frames = []
+22 -38
View File
@@ -22,54 +22,35 @@ classifiers = [
"Operating System :: OS Independent", "Operating System :: OS Independent",
] ]
dependencies = [ dependencies = [
'cython',
'gitpython >= 3.1',
'thinplate@git+https://github.com/cheind/py-thin-plate-spline',
'hickle >= 5.0',
'tensorboard >= 2.11',
'numpy >= 1.21', 'numpy >= 1.21',
'Pillow >= 9.5', 'Pillow >= 9.5',
'opencv-python >= 4.8', 'opencv-python >= 4.8',
'scipy >= 1.7', 'scipy >= 1.7',
'pycocotools >= 2.0.7',
'tqdm >= 4.66.1', 'tqdm >= 4.66.1',
'gradio >= 3.34',
'gdown >= 4.7.1',
'einops >= 0.6', 'einops >= 0.6',
'hydra-core >= 1.3.2', 'hydra-core >= 1.3.2',
'requests', 'PySide6 >= 6.2.0',
'imageio >= 2.25.0', 'charset-normalizer >= 3.1.0',
'imageio[ffmpeg]', 'netifaces >= 0.11.0',
'huggingface_hub >= 0.25.0', 'cchardet >= 2.1.7',
'safetensors',
'kornia',
'easydict', 'easydict',
'torch', 'av >= 0.5.2',
'torchvision', 'requests',
'typer >= 0.9.0',
"av>=16.1.0",
]
[project.optional-dependencies]
dev = [
'cython',
'gitpython >= 3.1',
'thinplate@git+https://github.com/cheind/py-thin-plate-spline',
'hickle >= 5.0',
'tensorboard >= 2.11',
'pycocotools >= 2.0.7',
'gdown >= 4.7.1',
'xlsxwriter',
]
gui = [
'gradio >= 6.9.0',
'PySide6 >= 6.2.0',
'pyqtdarktheme', 'pyqtdarktheme',
] 'imageio == 2.25.0',
all = [ 'imageio[ffmpeg]',
'cython', 'huggingface_hub == 0.36.2',
'gitpython >= 3.1', 'safetensors',
'thinplate@git+https://github.com/cheind/py-thin-plate-spline',
'hickle >= 5.0',
'tensorboard >= 2.11',
'pycocotools >= 2.0.7',
'gdown >= 4.7.1',
'xlsxwriter', 'xlsxwriter',
'gradio >= 6.9.0', 'kornia',
'PySide6 >= 6.2.0',
'pyqtdarktheme',
] ]
[project.scripts] [project.scripts]
@@ -104,3 +85,6 @@ torchvision = { index = "pytorch-cu128" }
[project.urls] [project.urls]
"Homepage" = "https://github.com/pq-yang/MatAnyone2" "Homepage" = "https://github.com/pq-yang/MatAnyone2"
"Bug Tracker" = "https://github.com/pq-yang/MatAnyone2/issues" "Bug Tracker" = "https://github.com/pq-yang/MatAnyone2/issues"
[tool.setuptools]
package-dir = {"" = "."}