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:
@@ -126,10 +126,11 @@ func (m *faceSwapModel) FaceSwap(ctx context.Context, req imagegen.FaceSwapReque
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
raw, respType, err := m.p.doRaw(ctx, http.MethodPost, path, m.id, contentType, body, maxFaceSwapResponseBytes)
|
||||
raw, respHdr, err := m.p.doRawHeaders(ctx, http.MethodPost, path, m.id, contentType, body, maxFaceSwapResponseBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
respType := respHdr.Get("Content-Type")
|
||||
if len(raw) == 0 {
|
||||
return nil, &llm.APIError{Provider: m.p.name, Model: m.id, Message: "face swap response contained no image"}
|
||||
}
|
||||
@@ -151,7 +152,55 @@ func (m *faceSwapModel) FaceSwap(ctx context.Context, req imagegen.FaceSwapReque
|
||||
if hdr := mimeFromContentType(respType, "image/"); hdr != "" {
|
||||
mimeType = hdr
|
||||
}
|
||||
return &imagegen.Result{Images: []llm.ImagePart{{MIME: mimeType, Data: raw}}}, nil
|
||||
return &imagegen.Result{
|
||||
Images: []llm.ImagePart{{MIME: mimeType, Data: raw}},
|
||||
SwappedFaces: parseSwapReport(respHdr.Get("X-Swap-Report")),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// swapReport mirrors the shim's X-Swap-Report header.
|
||||
type swapReport struct {
|
||||
Image []int `json:"image"`
|
||||
Faces []struct {
|
||||
Index int `json:"index"`
|
||||
Size []int `json:"size"`
|
||||
Yaw *float64 `json:"yaw"`
|
||||
IdentitySimilarity *float64 `json:"identity_similarity"`
|
||||
} `json:"faces"`
|
||||
}
|
||||
|
||||
// parseSwapReport decodes the measured outcome. A missing or malformed header
|
||||
// yields nil rather than an error: an older shim does not send it, and a swap
|
||||
// that produced a good image must not fail because the diagnostics alongside
|
||||
// it were unreadable.
|
||||
func parseSwapReport(header string) []imagegen.SwappedFace {
|
||||
header = strings.TrimSpace(header)
|
||||
if header == "" {
|
||||
return nil
|
||||
}
|
||||
var rep swapReport
|
||||
if err := json.Unmarshal([]byte(header), &rep); err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]imagegen.SwappedFace, 0, len(rep.Faces))
|
||||
for _, f := range rep.Faces {
|
||||
sf := imagegen.SwappedFace{
|
||||
Index: f.Index,
|
||||
Yaw: f.Yaw,
|
||||
IdentitySimilarity: f.IdentitySimilarity,
|
||||
}
|
||||
if len(f.Size) == 2 {
|
||||
sf.Width, sf.Height = f.Size[0], f.Size[1]
|
||||
}
|
||||
if len(rep.Image) == 2 {
|
||||
sf.ImageWidth, sf.ImageHeight = rep.Image[0], rep.Image[1]
|
||||
}
|
||||
out = append(out, sf)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// imageFilename picks a multipart filename for an image part. The shim reads
|
||||
|
||||
Reference in New Issue
Block a user