Models

scalacv.Models
object Models

A small registry and downloader for the model files scalacv's detectors need. It is the downloader: scalacv.vision.FaceDetect.downloadModel is a one-line alias for fetch(FaceDetect.modelSpec, into).

fetch downloads to a temp file beside the target and moves it into place only after it verifies, so an interrupted run never leaves a truncated model for the next load to trip over. It is idempotent: a target that already exists (and, if a hash or size is pinned, still matches) is returned without touching the network. URLs may be http(s):// or file://, so a model you already have on disk is just another source.

The detector model specs live next to their detectors (scalacv.vision.FaceDetect.modelSpec and scalacv.vision.FaceRecognizer.modelSpec); supply your own ModelSpec for anything else.

Attributes

Source
Models.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Models.type

Members list

Value members

Concrete methods

def fetch(spec: ModelSpec, into: Path): Either[CvError, Path]

Fetches spec into the directory into (created if absent), returning the verified file's path or a Left describing which stage failed — the directory, every URL tried, the size, or the checksum. Cancellation is not a mirror failure: InterruptedException propagates with the interrupt flag restored. The path remains exactly into.resolve(spec.fileName); this API adds no lock artifacts or digest directories. Publication requires atomic replacement and fails closed if unsupported, leaving the prior target unchanged rather than falling back to a potentially partial copy. For coordinated, content-addressed storage, opt in to fetchCached.

Fetches spec into the directory into (created if absent), returning the verified file's path or a Left describing which stage failed — the directory, every URL tried, the size, or the checksum. Cancellation is not a mirror failure: InterruptedException propagates with the interrupt flag restored. The path remains exactly into.resolve(spec.fileName); this API adds no lock artifacts or digest directories. Publication requires atomic replacement and fails closed if unsupported, leaving the prior target unchanged rather than falling back to a potentially partial copy. For coordinated, content-addressed storage, opt in to fetchCached.

spec.fileName must be a single path component — see isSafeFileName. A name that would resolve outside into (../x, an absolute path) or below it (sub/x) is a Left(CvError.LoadFailed) returned before the cache is consulted or the directory created, never a write somewhere else.

Attributes

Source
Models.scala
def fetchCached(spec: ModelSpec, cacheRoot: Path): Either[CvError, Path]

Fetches a pinned model at cacheRoot/sha256//. The root is the caller's namespace; unlike fetch, distinct revisions with the same filename coexist. Only an exact 64-hex-digit SHA-256 and a single non-special filename are accepted (no separators, NUL or drive prefixes); malformed tokens are rejected, never trimmed or path-normalized into valid ones.

Fetches a pinned model at cacheRoot/sha256//. The root is the caller's namespace; unlike fetch, distinct revisions with the same filename coexist. Only an exact 64-hex-digit SHA-256 and a single non-special filename are accepted (no separators, NUL or drive prefixes); malformed tokens are rejected, never trimmed or path-normalized into valid ones.

Every call verifies the bytes again, after acquiring JVM and process locks. Concurrent callers sharing a target coalesce a successful miss into one download; failures are not cached. Coordination uses 64 JVM lock stripes and at most 64 persistent lock files in .locks per root. Never remove these files or the cache root while callers may be active or waiting. Stripe collisions can serialize unrelated keys.

The root may itself be a symlink; layout components and entries beneath it may not. The caller must control the root and its ancestors: this is not a sandbox against hostile concurrent filesystem edits. Sharing requires a filesystem supporting process file locks and atomic replacement. As with fetch, unsupported atomic publication fails closed, with no non-atomic copy/delete fallback. Neither API promises fsync/crash durability or protects a returned path from subsequent external modification. InterruptedException propagates with the flag restored, including cancellation while waiting for a lock.

Attributes

Source
Models.scala