Background Matting v2

BACKGROUND_MATTING_V2

Cuts a person out of video precisely, hair included, when you also give a picture of the empty background from the same still camera; returns the matte or the person over a flat colour.

Video Editingvideo-mattingbackground-mattingalpha-mattehigh-resolutionmitFine-tunable
Background Matting v2: Video Editing model on Kainguru

About Background Matting v2

Capabilities

  • Precise edges — hair, fingers and thin straps, refined at the video's full resolution
  • Real foreground colours — the person's own colours come out of the edges, not the old background's
  • Matte or composite — the matte as a grey video, or the person over a flat colour to key out or cover
  • Fine-tunable — train it on your own clips, their mattes and the empty scene

Good to know

  • It needs a picture of the scene without the person, taken from the same place, and a camera that stays put: handheld footage will not work (use a matting model that needs no background instead).
  • Videos are MP4, MOV, WebM or MKV, at most 256 MB, 300 frames and 2,073,600 pixels a frame (1920 x 1080); the background picture has the video's shape and is scaled to its size. H.264 holds no transparency, so a result is the matte (white = person) or the person over a flat colour; it has no sound.
  • The result is an SDR video at a constant frame rate: a variable-frame-rate video is re-timed evenly at its average rate, and HDR (HLG or PQ) colours are not tone-mapped, so they look flatter than in an HDR player.
  • Each frame is matted on its own; Reduce flicker (on by default) smooths a pixel that jumps for one frame while the frames around it agree.
  • Fine-tuning takes one ZIP (at most 4 GB, 128 MB a clip, no password) with 2 to 100 clips and a manifest.jsonl holding one {"video": "clips/a.mp4", "matte": "mattes/a.mp4", "background": "plates/a.png"} line per clip. A matte is a grey video of the clip's size and length (white = person); the background is a picture of the empty scene at the clip's size. Clips are at least 512 pixels a side.
  • Training works on random 512 x 512 crops of the first 48 frames of each clip and stops after the steps you ask for or after 9 hours, keeping what it has learned.

Attribution

Background Matting V2 by Shanchuan Lin, Andrey Ryabtsev, Soumyadip Sengupta, Brian Curless, Steve Seitz and Ira Kemelmacher-Shlizerman, released under the MIT License.

Background Matting v2 inputs and output

NameTypeRequiredDefaultDetails
videofileYes—The video of people: MP4, MOV, WebM or MKV, at most 256 MB, 300 frames and 2,073,600 pixels a frame. The result has no sound.
1–2048 characters · video/mp4, quicktime, webm, x-matroska
backgroundfileYes—A PNG, JPEG or WebP picture of the scene without the person, from the same still camera, at most 50 MB; it is scaled to the video's size.
1–2048 characters · image/png, jpeg, webp
smoothbooleanNotrueSmooths a matte pixel that jumps for one frame while the frames around it agree.
outputtextNocompositecomposite: the people over a flat backdrop colour; matte: the alpha matte as a grey video, white = person.
Allowed: composite, matte
backdroptextNogreenThe flat colour behind the people in a composite.
Allowed: green, blue, white, black, gray

Output. Returns MP4 video: The people over a flat colour, or their alpha matte as a grey video: an H.264 MP4 without sound, the input's size and frame rate.

Run Background Matting v2 with the API

Send your API key in the X-API-Key header. The run is asynchronous: submit it, then poll it until it finishes. Create an API key.

cURL
curl -X POST "https://api.kainguru.com/api/v1/executions?mlFlowId=BACKGROUND_MATTING_V2" \
  -H "X-API-Key: kg_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
  "input": {
    "background": "https://example.com/input.png",
    "video": "https://example.com/input.png",
    "backdrop": "green",
    "output": "composite",
    "smooth": true
  },
  "outputFormat": "json"
}'

# Poll until the execution reaches a terminal state
curl "https://api.kainguru.com/api/v1/executions/{executionId}" \
  -H "X-API-Key: kg_your_api_key"
Python
from kainguru import KainguruClient

with KainguruClient(api_key="kg_your_api_key", base_url="https://api.kainguru.com/api") as client:
    submitted = client.executions.execute(
        "BACKGROUND_MATTING_V2",
        {
          "background": "https://example.com/input.png",
          "video": "https://example.com/input.png",
          "backdrop": "green",
          "output": "composite",
          "smooth": True
        },
        output_format="json",
    )
    result = client.executions.await_completion(submitted.id)
    print(result.execution.output)

Fine-tune Background Matting v2

You can fine-tune Background Matting v2 on your own data and run the tuned model through the same API. Dataset: One ZIP (at most 4 GB, 128 MB a clip, no password) with 2 to 100 clips, a grey matte video for each, and manifest.jsonl with one line per clip: {"video", "matte", "background"} (the empty scene). More: Good to know, in the README.

The steps are in the docs.

Background Matting v2 pricing

Price per frame: 0.002185 credits ($0.00002185). The price is shown again before every run; all prices are on the pricing page.

Licence and source

MODNet Video Matting: Video Editing model on Kainguru
Video Editing

MODNet Video Matting

Cuts people out of video with a soft alpha matte that keeps hair and fine edges, with no green screen and no…

Fine-tunable
DeOldify Video: Video Editing model on Kainguru
Video Editing

DeOldify Video

Colourises black-and-white video with DeOldify's video model: plausible colour for every frame, laid over the original…

Fine-tunable
MuseTalk 1.5: Video Editing model on Kainguru
Video Editing

MuseTalk 1.5

Makes a face in a video say new words. Send a clip of one talking face and an audio file; the mouth and jaw move to…

Fine-tunable
RIFE 4.26: Video Editing model on Kainguru
Video Editing

RIFE 4.26

Raises a video's frame rate 2, 3, 4 or 8 times by predicting the frames in between, fast enough for long clips (RIFE…

Fine-tunable
STTN Subtitle Remover: Video Editing model on Kainguru
Video Editing

STTN Subtitle Remover

Removes burned-in subtitles and captions from a video: finds the text on every frame and fills it in from the rest of…

Fine-tunable
Face Anonymizer Video: Video Editing model on Kainguru
Video Editing

Face Anonymizer Video

Finds every face in a short video and covers it on each frame with a blur, pixelation or a solid box.