-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
88 lines (78 loc) · 1.83 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
80
81
82
83
84
85
86
87
88
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os/exec"
"github.com/machinebox/sdk-go/facebox"
)
const boundary = "informs"
var (
fbox *facebox.Client
)
func main() {
http.HandleFunc("/cam", cam)
http.HandleFunc("/camFacebox", camFacebox)
fbox = facebox.New("http://localhost:8080")
log.Fatal(http.ListenAndServe(":8081", nil))
}
func cam(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary="+boundary)
cmd := exec.CommandContext(r.Context(), "./capture.py")
cmd.Stdout = w
err := cmd.Run()
if err != nil {
log.Println("[ERROR] capturing webcam", err)
}
}
func camFacebox(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary="+boundary)
cmd := exec.CommandContext(r.Context(), "./capture.py")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Println("[ERROR] Getting the stdout pipe")
return
}
cmd.Start()
mr := multipart.NewReader(stdout, boundary)
for {
p, err := mr.NextPart()
if err == io.EOF {
log.Println("[DEBUG] EOF")
break
}
if err != nil {
log.Println("[ERROR] reading next part", err)
return
}
jp, err := ioutil.ReadAll(p)
if err != nil {
log.Println("[ERROR] reading from bytes ", err)
continue
}
jpReader := bytes.NewReader(jp)
faces, err := fbox.Check(jpReader)
if err != nil {
log.Println("[ERROR] calling facebox", err)
continue
}
for _, face := range faces {
if face.Matched {
fmt.Println("I know you ", face.Name)
} else {
fmt.Println("I DO NOT know you ")
}
}
// just MJPEG
w.Write([]byte("Content-Type: image/jpeg\r\n"))
w.Write([]byte("Content-Length: " + string(len(jp)) + "\r\n\r\n"))
w.Write(jp)
w.Write([]byte("\r\n"))
w.Write([]byte("--informs\r\n"))
}
cmd.Wait()
}