-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (69 loc) · 1.56 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bytes"
"flag"
"fmt"
"image"
"image/color"
"os"
"github.com/marianina8/expression/azure"
"gocv.io/x/gocv"
)
func check(msg string, e error) {
if e != nil {
panic(fmt.Errorf("%s: %s", msg, e.Error()))
}
}
func main() {
video := flag.String("video", "", "video for emotion analysis")
flag.Parse()
if *video == "" {
flag.Usage()
return
}
key := os.Getenv("emotion_key")
host := os.Getenv("emotion_host")
azure, err := azure.NewClient(host, key)
check("new azure client", err)
displayVideo(azure, *video)
}
func displayVideo(client *azure.Client, filename string) {
stream, err := gocv.VideoCaptureFile(filename)
if err != nil {
check("capture video file", err)
}
defer stream.Close()
// open display window
window := gocv.NewWindow("Detect")
defer window.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
framenum := 0
black := color.RGBA{0, 0, 0, 0}
dominantEmotion := ""
for {
if ok := stream.Read(&img); !ok {
return
}
if img.Empty() {
continue
}
framenum++
b, err := gocv.IMEncode(".jpg", img)
check("gocv image encode", err)
// check frame ever 120 frames / 1 sec
if framenum%30 == 0 {
emotionData := client.FaceAnalysis(bytes.NewReader(b))
if (emotionData.FaceRectangle != azure.FaceRectangle{}) {
dominantEmotion = emotionData.Dominant()
}
}
gocv.PutText(&img, dominantEmotion, image.Pt(40, 200), gocv.FontHersheyPlain, 3, black, 2)
// show the image in the window, and wait 1 millisecond
window.IMShow(img)
if window.WaitKey(1) >= 0 {
break
}
}
}