Permalink
Cannot retrieve contributors at this time
58 lines (48 sloc)
971 Bytes
// What it does: | |
// | |
// This example uses the VideoCapture class to capture frames from a connected webcam, | |
// and displays the video in a Window class. | |
// | |
// How to run: | |
// | |
// go run ./cmd/capwindow/main.go | |
// | |
// +build example | |
package main | |
import ( | |
"fmt" | |
"os" | |
"gocv.io/x/gocv" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("How to run:\n\tcapwindow [camera ID]") | |
return | |
} | |
// parse args | |
deviceID := os.Args[1] | |
webcam, err := gocv.OpenVideoCapture(deviceID) | |
if err != nil { | |
fmt.Printf("Error opening video capture device: %v\n", deviceID) | |
return | |
} | |
defer webcam.Close() | |
window := gocv.NewWindow("Capture Window") | |
defer window.Close() | |
img := gocv.NewMat() | |
defer img.Close() | |
fmt.Printf("Start reading device: %v\n", deviceID) | |
for { | |
if ok := webcam.Read(&img); !ok { | |
fmt.Printf("Device closed: %v\n", deviceID) | |
return | |
} | |
if img.Empty() { | |
continue | |
} | |
window.IMShow(img) | |
if window.WaitKey(1) == 27 { | |
break | |
} | |
} | |
} |