Documentation
¶
Overview ¶
Package withoutbg removes image backgrounds with the withoutBG open-weights ONNX model (DepthAnythingV2 + ConvNeXt matting), driven through pure-onnx (purego, no CGO).
The model expects a letterboxed RGB tensor at a fixed canvas size and returns an alpha matte of the same canvas. The authoritative contract ships next to the model file as a sidecar JSON (canvas size, tensor names, shapes, SHA256); this package reads it, validates the ONNX graph against it, and uses it for the preprocessing defaults.
Typical use:
r, err := withoutbg.New(withoutbg.Config{ModelPath: "withoutbg-open-weights.onnx"})
if err != nil {
log.Fatal(err)
}
defer r.Close()
cutout, err := r.Remove(img) // *image.RGBA, original size, alpha = matte
Index ¶
- Constants
- func DecodeImage(rd io.Reader) (image.Image, string, error)
- func SidecarPath(modelPath string) string
- func WritePNG(w io.Writer, img image.Image) error
- type Config
- type Remover
- func (r *Remover) Alpha(img image.Image) (*image.Gray, error)
- func (r *Remover) AlphaReader(rd io.Reader) (*image.Gray, error)
- func (r *Remover) Close() error
- func (r *Remover) Cutout(rd io.Reader, w io.Writer) error
- func (r *Remover) Remove(img image.Image) (*image.NRGBA, error)
- func (r *Remover) RemoveReader(rd io.Reader) (*image.NRGBA, error)
- func (r *Remover) Sidecar() *Sidecar
- type Sidecar
Constants ¶
const ( // DefaultCanvasSize is used when neither the sidecar nor Config provide one. DefaultCanvasSize = 448 // DefaultInputName and DefaultOutputName match the published export. DefaultInputName = "rgb" DefaultOutputName = "alpha" )
Variables ¶
This section is empty.
Functions ¶
func DecodeImage ¶ added in v0.2.0
DecodeImage reads an image from rd. PNG and JPEG are supported out of the box; other formats work if the caller has registered them with image.
func SidecarPath ¶
SidecarPath returns the conventional sidecar path for a model file ("model.onnx" -> "model.onnx.json"). The published export uses exactly this naming, so the sidecar is found automatically.
Types ¶
type Config ¶
type Config struct {
// ModelPath is the path to withoutbg-open-weights.onnx (required).
ModelPath string
// SidecarPath overrides the sidecar location. Empty means
// ModelPath + ".json", which is how the published export ships.
SidecarPath string
// CanvasSize is the square letterbox canvas fed to the model. Empty/zero
// means "take it from the sidecar".
CanvasSize int
// InputName / OutputName override the ONNX tensor names. Empty means
// "take them from the sidecar", falling back to the graph itself.
InputName string
OutputName string
// VerifyModelSHA256 checks the model file against the sidecar's sha256
// before loading. It reads the whole file (455 MB for the published
// export), so it is off by default.
VerifyModelSHA256 bool
// LibraryPath optionally points to a prebuilt libonnxruntime.so. It is
// used only when the runtime is not yet initialized; otherwise the
// ONNXRUNTIME_LIB_PATH environment variable or the pure-onnx bootstrap
// download applies.
LibraryPath string
// contains filtered or unexported fields
}
Config controls model loading and preprocessing. Zero values fall back to the values from the sidecar (and then to the package defaults).
type Remover ¶
type Remover struct {
// contains filtered or unexported fields
}
Remover runs the withoutBG matting model.
func New ¶
New loads the sidecar, validates the ONNX graph against it and prepares an inference session.
The sidecar is required: it carries the canvas size and tensor names, and the model repository documents it as the authoritative contract. Loading a model without it would mean guessing the preprocessing.
func (*Remover) Alpha ¶
Alpha runs the model and returns the matte at the original image size. White (255) means "keep", black (0) means "background".
func (*Remover) AlphaReader ¶ added in v0.2.0
AlphaReader decodes an image from rd and returns the matte at its original size.
func (*Remover) Close ¶
Close releases the session. The ONNX Runtime environment is destroyed when the last Remover is closed.
func (*Remover) Cutout ¶ added in v0.2.0
Cutout is the whole stream pipeline in one call: decode an image from rd, remove its background, and write the PNG cutout to w. Use RemoveReader or AlphaReader when you want the images instead of encoded bytes.
func (*Remover) Remove ¶
Remove returns the input image with the matte attached as its alpha channel, ready to be saved as a PNG cutout. The result is an *image.NRGBA, so its RGB values are the original colours and A is the matte.
func (*Remover) RemoveReader ¶ added in v0.2.0
RemoveReader decodes an image from rd and returns the cutout.
type Sidecar ¶
type Sidecar struct {
CanvasSize int `json:"canvas_size"`
MattingInputSize int `json:"matting_input_size"`
OpsetVersion int `json:"opset_version"`
Precision string `json:"precision"`
Variant string `json:"variant"`
DepthVariant string `json:"depth_variant"`
ConvNeXtSize string `json:"convnext_size"`
ModelVersion string `json:"model_version"`
InputName string `json:"input_name"`
OutputName string `json:"output_name"`
InputDtype string `json:"input_dtype"`
InputShape []int `json:"input_shape"`
OutputShape []int `json:"output_shape"`
SizeMB float64 `json:"size_mb"`
SHA256 string `json:"sha256"`
// MAE and MaxAbs describe how closely the ONNX export reproduces the
// reference implementation on the exporter's validation set.
MAE float64 `json:"mae"`
MaxAbs float64 `json:"max_abs"`
}
Sidecar is the metadata file that ships next to the ONNX export (<model>.onnx.json). It is the authoritative source for the canvas size, tensor names and shapes, precision and the model hash; the README of the model repository says to read it first, so this package does the same and validates the ONNX graph against it.
func LoadSidecar ¶
LoadSidecar reads and validates a sidecar file. A missing sidecar is an error: without it the canvas size and tensor names would be guesses.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
withoutbg
command
Command withoutbg removes the background from an image with the withoutBG open-weights ONNX model.
|
Command withoutbg removes the background from an image with the withoutBG open-weights ONNX model. |
|
internal
|
|
|
onnxmeta
Package onnxmeta reads the model contract (graph input/output names and shapes) directly from an ONNX protobuf file.
|
Package onnxmeta reads the model contract (graph input/output names and shapes) directly from an ONNX protobuf file. |