-
Notifications
You must be signed in to change notification settings - Fork 869
/
main.go
51 lines (42 loc) · 909 Bytes
/
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
// What it does:
//
// This example uses the VideoCapture class to capture a frame from a connected webcam,
// then save it to an image file on disk.
//
// How to run:
//
// saveimage [camera ID] [image file]
//
// go run ./cmd/saveimage/main.go 0 filename.jpg
//
package main
import (
"fmt"
"os"
"gocv.io/x/gocv"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("How to run:\n\tsaveimage [camera ID] [image file]")
return
}
deviceID := os.Args[1]
saveFile := os.Args[2]
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
img := gocv.NewMat()
defer img.Close()
if ok := webcam.Read(&img); !ok {
fmt.Printf("cannot read device %v\n", deviceID)
return
}
if img.Empty() {
fmt.Printf("no image on device %v\n", deviceID)
return
}
gocv.IMWrite(saveFile, img)
}