-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (109 loc) · 2.65 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
log "github.com/sirupsen/logrus"
"github.com/enbis/gocv-fps-filter/utils"
"github.com/hybridgroup/mjpeg"
"github.com/spf13/viper"
"gocv.io/x/gocv"
)
var (
deviceID int
err error
webcam *gocv.VideoCapture
stream *mjpeg.Stream
)
func main() {
err := loadConfig()
if err != nil {
log.Error(err.Error())
}
startProcess()
}
func loadConfig() error {
viper.SetDefault("host", "0.0.0.0:8080")
viper.SetDefault("required_fps", 1)
viper.SetDefault("video_codec", "MJPG")
viper.SetDefault("device_id", "1")
viper.AddConfigPath("./")
viper.AddConfigPath("./config")
viper.SetConfigName("config")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
log.Printf("Using config file: %s", viper.ConfigFileUsed())
}
return nil
}
func startProcess() {
deviceID := viper.GetString("device_id")
host := viper.GetString("host")
// open webcam
webcam, err = gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening capture device: %v\n", deviceID)
return
}
defer webcam.Close()
// create the mjpeg stream
stream = mjpeg.NewStream()
stream.FrameInterval = 0
// start capturing
go mjpegCapture()
fmt.Println("Capturing. Point your browser to " + host)
// start http server
http.Handle("/", stream)
log.Fatal(http.ListenAndServe(host, nil))
}
func mjpegCapture() {
img := gocv.NewMat()
defer img.Close()
webcam.Set(gocv.VideoCaptureFOURCC, toFOURCC(viper.GetString("video_codec")))
requiredFps := viper.GetFloat64("required_fps")
fps := webcam.Get(gocv.VideoCaptureFPS)
counter := utils.NewCounter(0)
prop, err := processFps(fps, requiredFps)
if err != nil {
log.Error(err.Error())
}
iFps := 0
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
fmt.Printf("counter %d e iFPS %d \n", counter.GetCount(), iFps)
if counter.GetCount() == int(fps) {
counter.SetCounter(0)
iFps = 0
continue
} else if counter.GetCount() == iFps {
buf, _ := gocv.IMEncode(".jpg", img)
stream.UpdateJPEG(buf)
iFps += prop
} else {
counter.Increment()
continue
}
counter.Increment()
}
}
func processFps(running, required float64) (int, error) {
prop := int(running / required)
if prop < 1 {
return 0, errors.New("fps over than running")
}
return prop, nil
}
func toFOURCC(codec string) float64 {
c1 := []rune(string(codec[0]))[0]
c2 := []rune(string(codec[1]))[0]
c3 := []rune(string(codec[2]))[0]
c4 := []rune(string(codec[3]))[0]
return float64((c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24))
}