-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcam.go
67 lines (57 loc) · 1.44 KB
/
cam.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
// Copyright 2011 <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ingore
package main
import (
"flag"
"fmt"
"log"
"github.com/chai2010/opencv"
)
var (
flagCamNum = flag.Int("cam-num", 1, "set camera num")
)
func main() {
flag.Parse()
cameras := make([]*opencv.Capture, *flagCamNum)
for i := 0; i < len(cameras); i++ {
cameras[i] = opencv.NewCameraCapture(i)
if cameras[i] == nil {
log.Fatalf("can not open camera %d", i)
}
defer cameras[i].Release()
}
writers := make([]*opencv.VideoWriter, *flagCamNum)
for i := 0; i < len(writers); i++ {
m := cameras[i].QueryFrame()
writers[i] = opencv.NewVideoWriter(
fmt.Sprintf("cam%d_output.avi", i),
opencv.FOURCC('M', 'J', 'P', 'G'), 30,
m.GetWidth(), m.GetHeight(),
1,
)
if writers[i] == nil {
log.Fatalf("can not create writer %d", i)
}
defer writers[i].Release()
}
windows := make([]*opencv.Window, *flagCamNum)
for i := 0; i < len(windows); i++ {
windows[i] = opencv.NewWindow(fmt.Sprintf("Camera: %d", i))
defer windows[i].Destroy()
}
for i := 0; ; i++ {
for idx := 0; idx < *flagCamNum; idx++ {
if m := cameras[idx].QueryFrame(); m != nil {
fmt.Printf("camera(%d): Frame %d\n", idx, i)
windows[idx].ShowImage(m)
writers[idx].WriteFrame(m)
}
}
if key := opencv.WaitKey(30); key == 27 {
break
}
}
fmt.Println("Done")
}