API リファレンス¶
presets¶
face_fit.presets
¶
Output composition presets.
The numbers follow the photo requirements: - Image size 640 (H) x 480 (W), 4:3. - Face (crown-to-chin) covers about 70-80% of the image height. - White background.
face_ratio defaults to 0.75, the middle of the 0.70-0.80 range.
top_margin matches the headroom ratio in the requirement diagram (~9%).
eye_line is a reference value for validation/debug (vertical placement is
driven by top_margin).
Spec
dataclass
¶
Output composition specification.
Attributes:
| Name | Type | Description |
|---|---|---|
out_w |
int
|
Output width in pixels. |
out_h |
int
|
Output height in pixels. |
face_ratio |
float
|
Fraction of the height the face (crown-to-chin) should occupy. |
top_margin |
float
|
Fraction of headroom above the crown. Drives vertical placement. |
eye_line |
float
|
Reference vertical position of the eyes (fraction from top). For validation. |
bg |
tuple[int, int, int]
|
Background color (RGB) used to fill any margins. |
Source code in src/face_fit/presets.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
get_preset(name)
¶
Return a preset by name. Raises KeyError for an unknown name.
Source code in src/face_fit/presets.py
48 49 50 51 52 53 | |
core¶
face_fit.core
¶
Shared high-level pipeline used by the CLI commands.
Wraps the load -> detect -> fit -> save flow and small helpers (output naming,
input expansion, opening files) so that the fit and batch commands stay
thin. Reuses :mod:face_fit.landmarks, :mod:face_fit.compose,
:mod:face_fit.render and :mod:face_fit.presets.
FitReport
dataclass
¶
Outcome of fitting one image.
Source code in src/face_fit/core.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
to_dict()
¶
Return a JSON-serializable summary.
Source code in src/face_fit/core.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
build_spec(preset, *, width=None, height=None, face_ratio=None, top_margin=None)
¶
Resolve a preset and apply optional overrides.
Source code in src/face_fit/core.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
default_output(in_path)
¶
Derive a default output path next to the input (<stem>_fitted.jpg).
Source code in src/face_fit/core.py
85 86 87 | |
fit_file(in_path, out_path, spec, *, render_scale=2, quality=95, debug=False, detector=None)
¶
Run the full pipeline on one file and write the JPEG output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_path
|
Path
|
Source image path. |
required |
out_path
|
Path
|
Destination JPEG path. |
required |
spec
|
Spec
|
Target composition. |
required |
render_scale
|
int
|
Internal supersampling factor. |
2
|
quality
|
int
|
JPEG quality. |
95
|
debug
|
bool
|
Also write a |
False
|
detector
|
FaceDetector | None
|
Reusable detector (recommended for batch); falls back to the
shared default detector when |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
FitReport
|
class: |
Source code in src/face_fit/core.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
iter_image_files(inputs)
¶
Expand a list of files, directories and glob patterns into image paths.
Directories are scanned non-recursively. Results are de-duplicated and sorted.
Source code in src/face_fit/core.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
open_file(path)
¶
Open a file with the OS default application (Windows/macOS/Linux).
Source code in src/face_fit/core.py
173 174 175 176 177 178 179 180 181 | |
compose¶
face_fit.compose
¶
Composition math (pure functions).
Given the face reference points (crown, chin, both eyes) and a target
composition :class:~face_fit.presets.Spec, compute a similarity transform
(roll correction + uniform scale + translation).
Coordinates are image pixels (x right, y down). The only dependency is numpy and nothing touches the image itself, which keeps this easy to unit-test.
Fit
dataclass
¶
Result of the similarity-transform computation.
Attributes:
| Name | Type | Description |
|---|---|---|
forward |
ndarray
|
2x3 affine matrix mapping source -> output. |
inverse_coeffs |
tuple[float, float, float, float, float, float]
|
Coefficients for PIL |
scale |
float
|
Applied uniform scale. |
roll_deg |
float
|
Corrected roll angle, in degrees. |
eye_line_actual |
float
|
Eye line in the output, as a fraction of the height. |
chin_line_actual |
float
|
Chin line in the output, as a fraction of the height. |
info_points |
dict[str, Point]
|
Reference points mapped to output coordinates, for debugging. |
Source code in src/face_fit/compose.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
compute_fit(*, crown, chin, eye_left, eye_right, spec)
¶
Compute the similarity transform from reference points and a spec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crown
|
Point
|
Top of the head (source px). |
required |
chin
|
Point
|
Chin tip (source px). |
required |
eye_left
|
Point
|
Center of the left-in-image eye (smaller x). |
required |
eye_right
|
Point
|
Center of the right-in-image eye. |
required |
spec
|
Spec
|
Target composition. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
Fit
|
class: |
Constraints
- Level the line between the eyes (roll correction).
- Scale so crown-to-chin equals
spec.face_ratio * out_h. - Place the crown at
y = top_margin * out_h(vertical). - Place the eye midpoint at
x = out_w / 2(horizontal).
Source code in src/face_fit/compose.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
landmarks¶
face_fit.landmarks
¶
Face landmark detection + crown estimation via white-background segmentation.
MediaPipe FaceLandmarker (Tasks API, 478 landmarks) provides the eyes, chin, face width and center. The crown (top of the head), which is not a landmark, is recovered by extracting the subject silhouette from the white background. Image I/O uses PIL (Unicode-path safe); array work uses numpy/OpenCV.
Use :class:FaceDetector to load the model once and process many images (e.g.
in batch mode); :func:detect_face is a convenience wrapper over a shared
default detector.
FaceDetector
¶
Reusable face detector that loads the MediaPipe model once.
Create one instance and call :meth:detect for many images (the heavy model
load happens only once). Usable as a context manager.
Source code in src/face_fit/landmarks.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
__enter__()
¶
Enter the context, returning this detector.
Source code in src/face_fit/landmarks.py
193 194 195 | |
__exit__(*exc)
¶
Exit the context, releasing the landmarker.
Source code in src/face_fit/landmarks.py
197 198 199 | |
__init__()
¶
Create a detector; the model loads lazily on first use.
Source code in src/face_fit/landmarks.py
161 162 163 | |
close()
¶
Release the underlying MediaPipe landmarker.
Source code in src/face_fit/landmarks.py
187 188 189 190 191 | |
detect(rgb)
¶
Extract face geometry from an RGB array. Raises RuntimeError if no face.
Source code in src/face_fit/landmarks.py
180 181 182 183 184 185 | |
FaceGeometry
dataclass
¶
Detected face geometry (all values in source px).
Source code in src/face_fit/landmarks.py
45 46 47 48 49 50 51 52 53 54 | |
detect_face(rgb)
¶
Detect face geometry using a shared default :class:FaceDetector.
Source code in src/face_fit/landmarks.py
207 208 209 | |
load_rgb(path)
¶
Load an image as a uint8 RGB array (applies EXIF orientation; Unicode-safe).
Source code in src/face_fit/landmarks.py
57 58 59 60 61 62 | |
render¶
face_fit.render
¶
Apply the similarity transform, fill the white background, downscale, and save (Pillow).
draw_debug(out_img, spec, fit)
¶
Return a debug image with composition guides (crown/chin/center/eye lines) and points.
Source code in src/face_fit/render.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
fit_to_image(rgb, geom, spec, render_scale=2)
¶
Produce the output image from the geometry and spec.
The affine transform is applied onto a render_scale-times larger canvas
at full resolution, then downscaled with LANCZOS to reduce aliasing. Margins
and missing areas are filled with the background color. No retouching
(color/skin) is performed.
Returns:
| Type | Description |
|---|---|
tuple[Image, Fit]
|
|
Source code in src/face_fit/render.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
save_jpeg(img, path, quality=95)
¶
Save as JPEG (Unicode-safe; chroma subsampling disabled for quality).
Source code in src/face_fit/render.py
82 83 84 85 86 | |
save_png(img, path)
¶
Save as PNG (for debug images; Unicode-safe).
Source code in src/face_fit/render.py
89 90 91 92 93 | |
model¶
face_fit.model
¶
Fetch and cache the MediaPipe FaceLandmarker model (.task).
ensure_model()
¶
Return the model path, downloading and caching it if missing.
Source code in src/face_fit/model.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |