2021-05-25 14:30:24 +02:00
|
|
|
package face
|
|
|
|
|
|
|
|
import (
|
2021-05-25 18:01:21 +02:00
|
|
|
_ "embed"
|
2021-05-25 14:30:24 +02:00
|
|
|
"fmt"
|
|
|
|
_ "image/jpeg"
|
|
|
|
"io"
|
|
|
|
"os"
|
2021-05-25 18:01:21 +02:00
|
|
|
"path/filepath"
|
|
|
|
"runtime/debug"
|
2021-06-01 17:39:03 +02:00
|
|
|
"sort"
|
2021-05-31 15:40:52 +02:00
|
|
|
|
|
|
|
pigo "github.com/esimov/pigo/core"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2021-05-25 14:30:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed cascade/facefinder
|
|
|
|
var cascadeFile []byte
|
|
|
|
|
|
|
|
//go:embed cascade/puploc
|
|
|
|
var puplocFile []byte
|
|
|
|
|
|
|
|
var (
|
|
|
|
classifier *pigo.Pigo
|
|
|
|
plc *pigo.PuplocCascade
|
|
|
|
flpcs map[string][]*FlpCascade
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
p := pigo.NewPigo()
|
|
|
|
// Unpack the binary file. This will return the number of cascade trees,
|
|
|
|
// the tree depth, the threshold and the prediction from tree's leaf nodes.
|
|
|
|
classifier, err = p.Unpack(cascadeFile)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-05-26 14:41:59 +02:00
|
|
|
log.Errorf("faces: %s", err)
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pl := pigo.NewPuplocCascade()
|
|
|
|
plc, err = pl.UnpackCascade(puplocFile)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-05-26 14:41:59 +02:00
|
|
|
log.Errorf("faces: %s", err)
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flpcs, err = ReadCascadeDir(pl, "cascade/lps")
|
|
|
|
|
|
|
|
if err != nil {
|
2021-05-26 14:41:59 +02:00
|
|
|
log.Errorf("faces: %s", err)
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
eyeCascades = []string{"lp46", "lp44", "lp42", "lp38", "lp312"}
|
|
|
|
mouthCascades = []string{"lp93", "lp84", "lp82", "lp81"}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Detector struct contains Pigo face detector general settings.
|
|
|
|
type Detector struct {
|
2021-05-25 18:01:21 +02:00
|
|
|
minSize int
|
|
|
|
angle float64
|
|
|
|
shiftFactor float64
|
|
|
|
scaleFactor float64
|
|
|
|
iouThreshold float64
|
|
|
|
scoreThreshold float32
|
|
|
|
perturb int
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
// Detect runs the detection algorithm over the provided source image.
|
2021-09-03 00:57:59 +02:00
|
|
|
func Detect(fileName string, findLandmarks bool, minSize int) (faces Faces, err error) {
|
2021-05-25 18:01:21 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2021-05-26 14:41:59 +02:00
|
|
|
log.Errorf("faces: %s (panic)\nstack: %s", r, debug.Stack())
|
2021-05-25 18:01:21 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-09-03 00:57:59 +02:00
|
|
|
if minSize < 20 {
|
|
|
|
minSize = 20
|
|
|
|
}
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
d := &Detector{
|
2021-09-03 00:57:59 +02:00
|
|
|
minSize: minSize,
|
2021-05-25 18:01:21 +02:00
|
|
|
angle: 0.0,
|
|
|
|
shiftFactor: 0.1,
|
|
|
|
scaleFactor: 1.1,
|
2021-10-07 09:32:17 +02:00
|
|
|
iouThreshold: float64(OverlapThresholdFloor) / 100,
|
2021-09-23 13:16:05 +02:00
|
|
|
scoreThreshold: float32(ScoreThreshold),
|
2021-05-25 18:01:21 +02:00
|
|
|
perturb: 63,
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
2021-12-14 17:07:38 +01:00
|
|
|
return faces, fmt.Errorf("faces: file '%s' not found", txt.LogParam(filepath.Base(fileName)))
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
det, params, err := d.Detect(fileName)
|
2021-05-25 14:30:24 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-12-14 17:07:38 +01:00
|
|
|
return faces, fmt.Errorf("faces: %s (detect faces)", err)
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
if det == nil {
|
2021-05-26 14:41:59 +02:00
|
|
|
return faces, fmt.Errorf("faces: no result")
|
2021-05-25 18:01:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
faces, err = d.Faces(det, params, findLandmarks)
|
2021-05-25 14:30:24 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-05-26 14:41:59 +02:00
|
|
|
return faces, fmt.Errorf("faces: %s", err)
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
return faces, nil
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect runs the detection algorithm over the provided source image.
|
2021-09-06 05:13:53 +02:00
|
|
|
func (d *Detector) Detect(fileName string) (faces []pigo.Detection, params pigo.CascadeParams, err error) {
|
2021-05-25 14:30:24 +02:00
|
|
|
var srcFile io.Reader
|
|
|
|
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return faces, params, err
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:51:48 +02:00
|
|
|
defer func(file *os.File) {
|
|
|
|
err = file.Close()
|
|
|
|
}(file)
|
2021-05-25 14:30:24 +02:00
|
|
|
|
|
|
|
srcFile = file
|
|
|
|
|
|
|
|
src, err := pigo.DecodeImage(srcFile)
|
2021-09-03 00:57:59 +02:00
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return faces, params, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pixels := pigo.RgbToGrayscale(src)
|
|
|
|
cols, rows := src.Bounds().Max.X, src.Bounds().Max.Y
|
|
|
|
|
2021-09-03 00:57:59 +02:00
|
|
|
var maxSize int
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
if cols < 20 || rows < 20 || cols < d.minSize || rows < d.minSize {
|
2021-09-03 01:17:12 +02:00
|
|
|
return faces, params, fmt.Errorf("image size %dx%d is too small", cols, rows)
|
2021-09-03 00:57:59 +02:00
|
|
|
} else if cols < rows {
|
2021-09-22 19:33:41 +02:00
|
|
|
maxSize = cols - 4
|
2021-09-03 00:57:59 +02:00
|
|
|
} else {
|
2021-09-22 19:33:41 +02:00
|
|
|
maxSize = rows - 4
|
2021-09-03 00:57:59 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
imageParams := &pigo.ImageParams{
|
|
|
|
Pixels: pixels,
|
|
|
|
Rows: rows,
|
|
|
|
Cols: cols,
|
|
|
|
Dim: cols,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = pigo.CascadeParams{
|
2021-09-06 05:13:53 +02:00
|
|
|
MinSize: d.minSize,
|
2021-09-03 00:57:59 +02:00
|
|
|
MaxSize: maxSize,
|
2021-09-06 05:13:53 +02:00
|
|
|
ShiftFactor: d.shiftFactor,
|
|
|
|
ScaleFactor: d.scaleFactor,
|
2021-05-25 14:30:24 +02:00
|
|
|
ImageParams: *imageParams,
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
log.Tracef("faces: image size %dx%d, face size min %d, max %d", cols, rows, params.MinSize, params.MaxSize)
|
2021-09-03 01:17:12 +02:00
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
// Run the classifier over the obtained leaf nodes and return the Face results.
|
|
|
|
// The result contains quadruplets representing the row, column, scale and Face score.
|
2021-09-06 05:13:53 +02:00
|
|
|
faces = classifier.RunCascade(params, d.angle)
|
2021-05-25 14:30:24 +02:00
|
|
|
|
|
|
|
// Calculate the intersection over union (IoU) of two clusters.
|
2021-09-06 05:13:53 +02:00
|
|
|
faces = classifier.ClusterDetections(faces, d.iouThreshold)
|
2021-05-25 14:30:24 +02:00
|
|
|
|
|
|
|
return faces, params, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Faces adds landmark coordinates to detected faces and returns the results.
|
2021-09-06 05:13:53 +02:00
|
|
|
func (d *Detector) Faces(det []pigo.Detection, params pigo.CascadeParams, findLandmarks bool) (results Faces, err error) {
|
2021-09-21 09:51:48 +02:00
|
|
|
// Sort results by size.
|
2021-06-01 17:39:03 +02:00
|
|
|
sort.Slice(det, func(i, j int) bool {
|
2021-09-20 22:19:54 +02:00
|
|
|
return det[i].Scale > det[j].Scale
|
2021-06-01 17:39:03 +02:00
|
|
|
})
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
for _, face := range det {
|
2021-09-21 09:51:48 +02:00
|
|
|
// Skip result if quality is too low.
|
2021-09-23 13:16:05 +02:00
|
|
|
if face.Q < QualityThreshold(face.Scale) {
|
2021-06-01 17:39:03 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:19:54 +02:00
|
|
|
var eyesCoords []Area
|
|
|
|
var landmarkCoords []Area
|
|
|
|
var puploc *pigo.Puploc
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
faceCoord := NewArea(
|
2021-05-25 18:01:21 +02:00
|
|
|
"face",
|
2021-06-01 17:39:03 +02:00
|
|
|
face.Row,
|
|
|
|
face.Col,
|
2021-05-25 18:01:21 +02:00
|
|
|
face.Scale,
|
|
|
|
)
|
|
|
|
|
2021-09-21 09:51:48 +02:00
|
|
|
// Detect additional face landmarks?
|
2021-09-02 23:47:37 +02:00
|
|
|
if face.Scale > 50 && findLandmarks {
|
2021-05-25 18:01:21 +02:00
|
|
|
// Find left eye.
|
|
|
|
puploc = &pigo.Puploc{
|
|
|
|
Row: face.Row - int(0.075*float32(face.Scale)),
|
|
|
|
Col: face.Col - int(0.175*float32(face.Scale)),
|
|
|
|
Scale: float32(face.Scale) * 0.25,
|
2021-09-06 05:13:53 +02:00
|
|
|
Perturbs: d.perturb,
|
2021-05-25 18:01:21 +02:00
|
|
|
}
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
leftEye := plc.RunDetector(*puploc, params.ImageParams, d.angle, false)
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
if leftEye.Row > 0 && leftEye.Col > 0 {
|
2021-09-01 20:46:15 +02:00
|
|
|
eyesCoords = append(eyesCoords, NewArea(
|
2021-05-25 18:01:21 +02:00
|
|
|
"eye_l",
|
|
|
|
leftEye.Row,
|
|
|
|
leftEye.Col,
|
|
|
|
int(leftEye.Scale),
|
|
|
|
))
|
|
|
|
}
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
// Find right eye.
|
|
|
|
puploc = &pigo.Puploc{
|
|
|
|
Row: face.Row - int(0.075*float32(face.Scale)),
|
|
|
|
Col: face.Col + int(0.185*float32(face.Scale)),
|
|
|
|
Scale: float32(face.Scale) * 0.25,
|
2021-09-06 05:13:53 +02:00
|
|
|
Perturbs: d.perturb,
|
2021-05-25 18:01:21 +02:00
|
|
|
}
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
rightEye := plc.RunDetector(*puploc, params.ImageParams, d.angle, false)
|
2021-05-25 18:01:21 +02:00
|
|
|
|
|
|
|
if rightEye.Row > 0 && rightEye.Col > 0 {
|
2021-09-01 20:46:15 +02:00
|
|
|
eyesCoords = append(eyesCoords, NewArea(
|
2021-05-25 18:01:21 +02:00
|
|
|
"eye_r",
|
|
|
|
rightEye.Row,
|
|
|
|
rightEye.Col,
|
|
|
|
int(rightEye.Scale),
|
|
|
|
))
|
|
|
|
}
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
if leftEye != nil && rightEye != nil {
|
2021-05-25 14:30:24 +02:00
|
|
|
for _, eye := range eyeCascades {
|
|
|
|
for _, flpc := range flpcs[eye] {
|
2021-05-25 18:01:21 +02:00
|
|
|
if flpc == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
flp := flpc.GetLandmarkPoint(leftEye, rightEye, params.ImageParams, d.perturb, false)
|
2021-05-25 14:30:24 +02:00
|
|
|
if flp.Row > 0 && flp.Col > 0 {
|
2021-09-01 20:46:15 +02:00
|
|
|
landmarkCoords = append(landmarkCoords, NewArea(
|
2021-05-25 14:30:24 +02:00
|
|
|
eye,
|
|
|
|
flp.Row,
|
|
|
|
flp.Col,
|
|
|
|
int(flp.Scale),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
flp = flpc.GetLandmarkPoint(leftEye, rightEye, params.ImageParams, d.perturb, true)
|
2021-05-25 14:30:24 +02:00
|
|
|
if flp.Row > 0 && flp.Col > 0 {
|
2021-09-01 20:46:15 +02:00
|
|
|
landmarkCoords = append(landmarkCoords, NewArea(
|
2021-05-25 14:30:24 +02:00
|
|
|
eye+"_v",
|
|
|
|
flp.Row,
|
|
|
|
flp.Col,
|
|
|
|
int(flp.Scale),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 18:01:21 +02:00
|
|
|
}
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
// Find mouth.
|
|
|
|
for _, mouth := range mouthCascades {
|
|
|
|
for _, flpc := range flpcs[mouth] {
|
|
|
|
if flpc == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
flp := flpc.GetLandmarkPoint(leftEye, rightEye, params.ImageParams, d.perturb, false)
|
2021-05-25 18:01:21 +02:00
|
|
|
if flp.Row > 0 && flp.Col > 0 {
|
2021-09-01 20:46:15 +02:00
|
|
|
landmarkCoords = append(landmarkCoords, NewArea(
|
2021-05-25 18:01:21 +02:00
|
|
|
"mouth_"+mouth,
|
|
|
|
flp.Row,
|
|
|
|
flp.Col,
|
|
|
|
int(flp.Scale),
|
|
|
|
))
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-25 18:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flpc := flpcs["lp84"][0]
|
|
|
|
|
|
|
|
if flpc != nil {
|
2021-09-06 05:13:53 +02:00
|
|
|
flp := flpc.GetLandmarkPoint(leftEye, rightEye, params.ImageParams, d.perturb, true)
|
2021-05-25 14:30:24 +02:00
|
|
|
if flp.Row > 0 && flp.Col > 0 {
|
2021-09-01 20:46:15 +02:00
|
|
|
landmarkCoords = append(landmarkCoords, NewArea(
|
2021-05-25 14:30:24 +02:00
|
|
|
"lp84",
|
|
|
|
flp.Row,
|
|
|
|
flp.Col,
|
|
|
|
int(flp.Scale),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 18:01:21 +02:00
|
|
|
|
2021-09-21 09:51:48 +02:00
|
|
|
// Create face.
|
2021-09-20 22:19:54 +02:00
|
|
|
f := Face{
|
2021-05-25 18:01:21 +02:00
|
|
|
Rows: params.ImageParams.Rows,
|
|
|
|
Cols: params.ImageParams.Cols,
|
|
|
|
Score: int(face.Q),
|
2021-09-02 23:47:37 +02:00
|
|
|
Area: faceCoord,
|
2021-05-25 18:01:21 +02:00
|
|
|
Eyes: eyesCoords,
|
|
|
|
Landmarks: landmarkCoords,
|
2021-09-20 22:19:54 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 09:51:48 +02:00
|
|
|
// Does the face significantly overlap with previous results?
|
2021-09-20 22:19:54 +02:00
|
|
|
if results.Contains(f) {
|
2021-09-21 09:51:48 +02:00
|
|
|
// Ignore face.
|
2021-09-20 22:19:54 +02:00
|
|
|
} else {
|
2021-09-21 09:51:48 +02:00
|
|
|
// Append face.
|
2021-09-20 22:19:54 +02:00
|
|
|
results.Append(f)
|
|
|
|
}
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
return results, nil
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|