/
facelock.go
60 lines (52 loc) · 1.23 KB
/
facelock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
59
60
package main
import (
"fmt"
"os/exec"
"path"
"runtime"
"time"
"github.com/lazywei/go-opencv/opencv"
)
func main() {
cap := opencv.NewCameraCapture(0)
if cap == nil {
panic("can not open camera")
}
defer cap.Release()
_, currentfile, _, _ := runtime.Caller(0)
cascade := opencv.LoadHaarClassifierCascade(path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml"))
timeout := time.Second * 10
lastFace := time.Now()
for {
if cap.GrabFrame() {
img := cap.RetrieveFrame(1)
if img == nil {
continue
}
hasFaces, _ := ProcessImage(img, cascade)
if hasFaces {
lastFace = time.Now()
} else {
delta := time.Since(lastFace)
fmt.Println("no faces for %ts", delta)
if delta > timeout {
fmt.Println("lock!")
lock()
time.Sleep(time.Second * 60)
}
}
}
time.Sleep(time.Second)
}
}
func ProcessImage(img *opencv.IplImage, cascade *opencv.HaarCascade) (bool, error) {
faces := cascade.DetectObjects(img)
hasFaces := len(faces) > 0
return hasFaces, nil
}
func lock() {
cmd := "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine"
out, err := exec.Command(cmd).Output()
fmt.Println(out)
fmt.Println(err)
}