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,
|
|
|
|
iouThreshold: 0.2,
|
2021-05-26 11:55:10 +02:00
|
|
|
scoreThreshold: 9.0,
|
2021-05-25 18:01:21 +02:00
|
|
|
perturb: 63,
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fs.FileExists(fileName) {
|
2021-05-26 14:41:59 +02:00
|
|
|
return faces, fmt.Errorf("faces: file '%s' not found", txt.Quote(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-05-26 14:41:59 +02:00
|
|
|
return faces, fmt.Errorf("faces: %v (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-05-25 18:01:21 +02:00
|
|
|
defer file.Close()
|
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-03 01:17:12 +02:00
|
|
|
maxSize = cols - 8
|
2021-09-03 00:57:59 +02:00
|
|
|
} else {
|
2021-09-03 01:17:12 +02:00
|
|
|
maxSize = rows - 8
|
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,
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:57:41 +02:00
|
|
|
if rows > 800 || cols > 800 {
|
2021-09-06 05:13:53 +02:00
|
|
|
d.scoreThreshold += 9.0
|
2021-06-01 18:57:41 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
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-03 01:17:12 +02:00
|
|
|
log.Debugf("faces: image size %dx%d, face size min %d, max %d", cols, rows, params.MinSize, params.MaxSize)
|
|
|
|
|
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-06-01 17:39:03 +02:00
|
|
|
var maxQ float32
|
|
|
|
|
|
|
|
// Sort by quality.
|
|
|
|
sort.Slice(det, func(i, j int) bool {
|
|
|
|
return det[i].Q > det[j].Q
|
|
|
|
})
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
for _, face := range det {
|
2021-09-01 20:46:15 +02:00
|
|
|
var eyesCoords []Area
|
|
|
|
var landmarkCoords []Area
|
2021-06-01 17:39:03 +02:00
|
|
|
var puploc *pigo.Puploc
|
|
|
|
|
2021-09-06 05:13:53 +02:00
|
|
|
if face.Q < d.scoreThreshold {
|
2021-05-25 18:01:21 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-06-01 17:39:03 +02:00
|
|
|
if maxQ < face.Q {
|
|
|
|
maxQ = face.Q
|
|
|
|
} else if maxQ >= 20 && face.Q < 15 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
|
|
|
results = append(results, Face{
|
|
|
|
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-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
|
|
|
}
|