Files
pigo/examples/python/pigo.go
2019-02-05 07:43:24 +02:00

103 lines
2.4 KiB
Go

package main
import "C"
import (
"fmt"
"io/ioutil"
"log"
"runtime"
"unsafe"
"github.com/esimov/pigo/core"
)
var (
cascade []byte
err error
p *pigo.Pigo
classifier *pigo.Pigo
)
func main() {}
//export FindFaces
func FindFaces(pixels []uint8) uintptr {
pointCh := make(chan uintptr)
dets := clusterDetection(pixels, 480, 640)
result := make([][]int, len(dets))
for i := 0; i < len(dets); i++ {
if dets[i].Q >= 5.0 {
result[i] = append(result[i], dets[i].Row, dets[i].Col, dets[i].Scale)
}
}
if len(result) > 0 {
// Since we cannot transfer a 2d array trough an array pointer
// we have to transform it into 1d array.
go func() {
det := make([]int, 0, len(result))
for _, v := range result {
det = append(det, v...)
}
fmt.Println(det)
// Include as a first slice element the number of detected faces.
// We need to transfer this value in order to define the Python array buffer length.
det = append([]int{len(result), 0, 0}, det...)
// Convert the slice into an array pointer.
s := *(*[]int)(unsafe.Pointer(&det))
p := uintptr(unsafe.Pointer(&s[0]))
// Ensure `det` is not freed by GC.
runtime.KeepAlive(det)
// return the pointer address
pointCh <- p
}()
return <-pointCh
}
return 0
}
// clusterDetection runs Pigo face detector core methods
// and returns a cluster with the detected faces coordinates.
func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection {
cParams := pigo.CascadeParams{
MinSize: 100,
MaxSize: 1000,
ShiftFactor: 0.15,
ScaleFactor: 1.1,
ImageParams: pigo.ImageParams{
Pixels: pixels,
Rows: rows,
Cols: cols,
Dim: cols,
},
}
if len(cascade) == 0 {
cascade, err = ioutil.ReadFile("../../data/facefinder")
if err != nil {
log.Fatalf("Error reading the cascade file: %v", err)
}
// 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(cascade)
if err != nil {
log.Fatalf("Error reading the cascade file: %s", err)
}
}
// Run the classifier over the obtained leaf nodes and return the detection results.
// The result contains quadruplets representing the row, column, scale and detection score.
dets := classifier.RunCascade(cParams, 0.0)
// Calculate the intersection over union (IoU) of two clusters.
dets = classifier.ClusterDetections(dets, 0)
return dets
}