feat(faceswap): carry the measured outcome, not just the image
A face swap always returns an image and always looks like success. Whether the likeness actually transferred is a different question, and until now nothing in the response answered it — so a caller wanting to know went and asked a vision model instead. That is wrong in precisely the cases that matter: shown a jogger in a Georgetown cap holding McDonald's cups, a VLM answers "Bill Clinton" whoever's face is on him. In the run that prompted this it reported failure on six consecutive CORRECT swaps (measured afterwards at 0.79-0.84 cosine), and the caller burned 21 minutes chasing a problem that did not exist. Result.SwappedFaces now carries, per replaced face: pixel size, the target image's dimensions, head yaw, and cosine similarity between the source face and the face actually present in the output. Yaw and FractionOfImage are the two that explain the complaint. The swap in question replaced a 138px face in a 1010px-wide photo — 14% of the width, correct and invisible at a glance — and elsewhere a face turned -82 degrees, where the features carrying identity are edge-on and any swap reads as a generic person. Same code on a 168px face in a 385px picture (44%, yaw 2) is unmistakable. None of that was inferable from a bounding box. Typed on Result rather than stuffed into Raw: a caller has to act on this, and a value reachable only by type-asserting an `any` is one nobody finds in time. doRawHeaders is doRaw with the whole header instead of only Content-Type; doRaw delegates to it, so the other 25 call sites are untouched and there is still one place where the status check and the size cap live. A missing or malformed header yields nil, not an error — an older shim sends no header, and a swap that produced a good image must not fail because the diagnostics beside it were unreadable. Covered for absent/garbage/wrong-type, and the parse is break-checked.
This commit is contained in:
@@ -64,6 +64,57 @@ func (f DetectedFace) Size() (w, h int) {
|
||||
return f.Box[2] - f.Box[0], f.Box[3] - f.Box[1]
|
||||
}
|
||||
|
||||
// SwappedFace is the MEASURED outcome for one face the provider replaced.
|
||||
//
|
||||
// It exists because "the call returned an image" and "the likeness
|
||||
// transferred" are different claims that look identical from outside, and a
|
||||
// caller that cannot tell them apart will go looking for another way to
|
||||
// check. The one it reaches for — asking a vision model who the result looks
|
||||
// like — is wrong in exactly the cases that matter: a VLM shown a jogger in a
|
||||
// Georgetown cap holding McDonald's cups answers "Bill Clinton" whoever's
|
||||
// face is on him, so it reports failure on a correct swap.
|
||||
type SwappedFace struct {
|
||||
// Index is the face's position in the provider's left-to-right ordering.
|
||||
Index int
|
||||
|
||||
// Width, Height are the replaced face's pixel size in the TARGET.
|
||||
// Meaningful only against ImageWidth/ImageHeight: a 138px face is large
|
||||
// in a 400px picture and nearly invisible in a 2000px one, and it is the
|
||||
// ratio, not the absolute size, that decides whether a person notices.
|
||||
Width, Height int
|
||||
|
||||
// ImageWidth, ImageHeight are the target image's dimensions, repeated on
|
||||
// every entry so a single face is self-describing without the caller
|
||||
// holding onto the rest of the response.
|
||||
ImageWidth, ImageHeight int
|
||||
|
||||
// Yaw is how far the head is turned from camera, in degrees, or nil when
|
||||
// the provider does not report pose. The best single predictor of whether
|
||||
// a swap will READ as the source person: past roughly ±45° the features
|
||||
// carrying identity are edge-on and the result looks like a generic
|
||||
// person rather than a specific one.
|
||||
Yaw *float64
|
||||
|
||||
// IdentitySimilarity is cosine similarity between the source face and the
|
||||
// face actually present in the result, 0-1, or nil when the provider
|
||||
// could not measure it. Above ~0.5 the identity transferred; a LOW value
|
||||
// is the only evidence that a swap genuinely failed.
|
||||
IdentitySimilarity *float64
|
||||
}
|
||||
|
||||
// FractionOfImage is the swapped face's width as a share of the image's, 0-1.
|
||||
// The number that predicts whether a person will SEE the change: the swap
|
||||
// that prompted all this replaced a 138px face in a 1010px-wide photo — 14%,
|
||||
// correct by every measure and invisible at a glance — while the same code on
|
||||
// a 168px face in a 385px picture (44%) is unmistakable. Returns 0 when the
|
||||
// dimensions are unknown.
|
||||
func (f SwappedFace) FractionOfImage() float64 {
|
||||
if f.ImageWidth <= 0 || f.Width <= 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(f.Width) / float64(f.ImageWidth)
|
||||
}
|
||||
|
||||
// FaceSwapper is the optional face-transfer surface. Separate interface so
|
||||
// existing providers keep compiling; callers type-assert.
|
||||
type FaceSwapper interface {
|
||||
|
||||
@@ -68,6 +68,15 @@ type Result struct {
|
||||
// Images are the generated images, in the order the backend returned them.
|
||||
Images []Image
|
||||
|
||||
// SwappedFaces is the measured outcome of a FaceSwap, one entry per face
|
||||
// replaced. Empty for every other operation, and empty from a provider
|
||||
// that does not measure. TYPED rather than tucked into Raw: a caller has
|
||||
// to act on this — it is the only way to distinguish a swap that
|
||||
// transferred the likeness from one that returned an image and nothing
|
||||
// more — and a value reachable solely by type-asserting an `any` is one
|
||||
// nobody discovers in time to use.
|
||||
SwappedFaces []SwappedFace
|
||||
|
||||
// Raw is the provider-native response object, an escape hatch for
|
||||
// provider-specific fields. May be nil; never required for normal use.
|
||||
Raw any
|
||||
|
||||
Reference in New Issue
Block a user