$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-integrate-pytorch-visionCreates MATLAB interfaces to Python image processing and computer vision models from GitHub repositories or pip-installable packages using MPyReq. Use when asked to interface MATLAB with a Python CV/image model (segmentation, depth estimation, object detection, image generation,
| 1 | # MPyReq MATLAB Interface Builder |
| 2 | |
| 3 | Build a MATLAB interface to a Python/PyTorch model repository using the MPyReq framework. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - User asks to interface MATLAB with a Python image processing or computer vision model (segmentation, depth estimation, object detection, image generation, super-resolution, pose estimation, optical flow, salient object detection, etc.) |
| 8 | - User provides a GitHub repository URL for a vision/image model and wants to call it from MATLAB |
| 9 | - User asks to "create an MPyReq wrapper" or "MPyReq demo" for an image/CV model |
| 10 | - User wants to run a pip-installable vision model library (e.g., Cellpose, SAM2, Depth-Pro, BiRefNet, StarDist) from MATLAB |
| 11 | |
| 12 | ## When Not to Use |
| 13 | |
| 14 | - General-purpose Python-MATLAB interfacing (no vision/image model involved) |
| 15 | - Non-vision models: NLP, audio, tabular, reinforcement learning, time-series |
| 16 | - Model deployment, containerization, or inference servers |
| 17 | - Pure MATLAB image processing workflows with no Python dependency |
| 18 | - Creating Python code (this skill creates MATLAB code that calls Python) |
| 19 | |
| 20 | ## Prerequisites: MPyReq on the MATLAB Path |
| 21 | |
| 22 | Before generating any demo script, verify that MPyReq is available. Run `which MPyReq` via the MATLAB MCP server (if available) or ask the user to confirm. |
| 23 | |
| 24 | ### If MPyReq is NOT on the MATLAB path: |
| 25 | |
| 26 | 1. **Download MPyReq** from the MATLAB File Exchange: |
| 27 | https://mathworks.com/matlabcentral/fileexchange/182230-matlab-based-python-requirements-manager |
| 28 | 2. **Install it** — either: |
| 29 | - Open the downloaded `.mltbx` file in MATLAB (double-click), which installs it as a MATLAB Add-On automatically, or |
| 30 | - Extract the files and add the folder containing `MPyReq.m` to the MATLAB path: |
| 31 | ```matlab |
| 32 | addpath("/path/to/mpyreq"); |
| 33 | savepath; % persist across sessions |
| 34 | ``` |
| 35 | 3. **Verify** by running `which MPyReq` in MATLAB — it should return the path to `MPyReq.m`. |
| 36 | |
| 37 | Do not proceed with demo generation until MPyReq is confirmed on the path. |
| 38 | |
| 39 | ## Input |
| 40 | |
| 41 | Ask the user for: |
| 42 | 1. **GitHub repository URL** — the Python model repository to interface with |
| 43 | 2. **What the model does** (optional) — to help identify the right inference example |
| 44 | |
| 45 | ## Step 1: Analyze the Repository |
| 46 | |
| 47 | Fetch and analyze the GitHub repository to determine: |
| 48 | |
| 49 | - **Python version requirement** — check `setup.py`, `setup.cfg`, `pyproject.toml`, or README for the required Python version. Default to `"3.12"` if not specified. Use `"3.11"` if the project needs older compatibility. |
| 50 | - **Installation method** — determine how the project is installed: |
| 51 | - If it uses `torch.hub.load()`: only need `torch` and `torchvision` as pip packages (model downloads automatically) |
| 52 | - If it's a pip-installable package: use `MPyReq.pipPackage()` |
| 53 | - If it's a non-packaged git repo: use `MPyReq.gitrepo()` + `MPyReq.requirementTextFile()` if a `requirements.txt` exists |
| 54 | - If it needs `pip install git+<url>`: use `MPyReq.pipPackage("git+<url>", Name="<ProjectName>")` |
| 55 | - **Additional dependencies** — any extra pip packages needed (e.g., `torch`, `torchvision`, etc.) |
| 56 | - **Model weights** — determine how weights are loaded: |
| 57 | - `torch.hub.load()` — weights download automatically, no `MPyReq.weights()` needed |
| 58 | - Direct URL download — use `MPyReq.weights()` with the checkpoint URL |
| 59 | - HuggingFace `.from_pretrained()` — weights download automatically via the library |
| 60 | - **Inference example** — locate the primary inference/prediction code in the README or example scripts |
| 61 | - **Preprocessing requirements** — check if the model requires specific input normalization (e.g., ImageNet mean/std), resizing, or center cropping |
| 62 | |
| 63 | ## Step 2: Generate the MPyReq Setup Script |
| 64 | |
| 65 | Create a MATLAB `.m` file that sets up the Python environment. Follow these patterns from the demo files: |
| 66 | |
| 67 | ### MANDATORY: Installation folder setup |
| 68 | Every generated script MUST begin with `MPyReq.setInstallFolder()`. This tells MPyReq where to download Python, packages, and model weights. Without this, MPyReq will show a GUI dialog which blocks non-interactive execution. Also include `MPyReq.autoAcceptDownloadPrompts(true)` to avoid interactive confirmation prompts. |
| 69 | |
| 70 | ```matlab |
| 71 | % Set installation folder (SSD recommended, ~15+ GB free space) |
| 72 | % Change this path to a suitable location on your machine |
| 73 | MPyReq.set |