From a47fcad929cce59a2b24fd13f27f233610579d45 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 1 Oct 2017 12:55:09 +0200 Subject: [PATCH] Refactor to use combined create/open functions for VideoCapture and VideoWriter to simplify usage Signed-off-by: deadprogram --- README.md | 11 +++--- examples/capture.go | 10 +++--- examples/capwindow.go | 9 +++-- examples/faceblur.go | 11 +++--- examples/facedetect.go | 16 ++++----- examples/pvl/faceblur.go | 11 +++--- examples/pvl/smiledetect.go | 11 +++--- examples/saveimage.go | 10 +++--- examples/savevideo.go | 20 ++++++----- pvl/README.md | 11 +++--- videoio.go | 67 +++++++++++++++++++------------------ 11 files changed, 91 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index f880587f..6ec1e604 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,12 @@ func main() { deviceID := 0 // open webcam - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) return - } + } + defer webcam.Close() // open display window window := opencv3.NewWindow("Face Detect") diff --git a/examples/capture.go b/examples/capture.go index 6c829007..211e0438 100644 --- a/examples/capture.go +++ b/examples/capture.go @@ -18,12 +18,12 @@ import ( func main() { deviceID := 0 - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(int(deviceID)); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) + return } + defer webcam.Close() // streaming, capture from webcam buf := opencv3.NewMat() diff --git a/examples/capwindow.go b/examples/capwindow.go index 944e5aa8..f084eb73 100644 --- a/examples/capwindow.go +++ b/examples/capwindow.go @@ -19,13 +19,12 @@ import ( func main() { deviceID := 0 - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(int(deviceID)); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) return } + defer webcam.Close() window := opencv3.NewWindow("Capture") defer window.Close() diff --git a/examples/faceblur.go b/examples/faceblur.go index 507d04e5..a20c9c4e 100644 --- a/examples/faceblur.go +++ b/examples/faceblur.go @@ -33,13 +33,12 @@ func main() { xmlFile := os.Args[2] // open webcam - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) return - } + } + defer webcam.Close() // open display window window := opencv3.NewWindow("Face Detect") diff --git a/examples/facedetect.go b/examples/facedetect.go index 5b928c1f..80fd8f2b 100644 --- a/examples/facedetect.go +++ b/examples/facedetect.go @@ -33,13 +33,12 @@ func main() { xmlFile := os.Args[2] // open webcam - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) return - } + } + defer webcam.Close() // open display window window := opencv3.NewWindow("Face Detect") @@ -78,10 +77,7 @@ func main() { opencv3.Rectangle(img, r, blue) size := opencv3.GetTextSize("Human", opencv3.FontHersheyPlain, 1.2, 2) - pt := image.Point{ - X: r.X + (r.Width / 2) - (size.X / 2), - Y: r.Y - 2, - } + pt := image.Pt(r.Min.X + (r.Min.X / 2) - (size.X / 2), r.Min.Y - 2) opencv3.PutText(img, "Human", pt, opencv3.FontHersheyPlain, 1.2, blue, 2) } diff --git a/examples/pvl/faceblur.go b/examples/pvl/faceblur.go index 040f68d1..df947633 100644 --- a/examples/pvl/faceblur.go +++ b/examples/pvl/faceblur.go @@ -33,13 +33,12 @@ func main() { deviceID, _ := strconv.Atoi(os.Args[1]) // open webcam - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) return - } + } + defer webcam.Close() // open display window window := opencv3.NewWindow("PVL Faceblur") diff --git a/examples/pvl/smiledetect.go b/examples/pvl/smiledetect.go index 031fe919..7d022a7e 100644 --- a/examples/pvl/smiledetect.go +++ b/examples/pvl/smiledetect.go @@ -35,13 +35,12 @@ func main() { deviceID, _ := strconv.Atoi(os.Args[1]) // open webcam - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) return - } + } + defer webcam.Close() // open display window window := opencv3.NewWindow("PVL Smile Detect") diff --git a/examples/saveimage.go b/examples/saveimage.go index c80662ed..d346af36 100644 --- a/examples/saveimage.go +++ b/examples/saveimage.go @@ -30,13 +30,13 @@ func main() { deviceID, _ := strconv.Atoi(os.Args[1]) saveFile := os.Args[2] - webcam := opencv3.NewVideoCapture() + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) + return + } defer webcam.Close() - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) - } - img := opencv3.NewMat() defer img.Close() diff --git a/examples/savevideo.go b/examples/savevideo.go index fb63e308..6495aabe 100644 --- a/examples/savevideo.go +++ b/examples/savevideo.go @@ -30,16 +30,13 @@ func main() { deviceID, _ := strconv.Atoi(os.Args[1]) saveFile := os.Args[2] - webcam := opencv3.NewVideoCapture() + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) + return + } defer webcam.Close() - writer := opencv3.NewVideoWriter() - defer writer.Close() - - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) - } - img := opencv3.NewMat() defer img.Close() @@ -48,7 +45,12 @@ func main() { return } - writer.OpenWithMat(saveFile, 25, img) + writer, err := opencv3.VideoWriterFileMat(saveFile, 25, img) + if err != nil { + fmt.Printf("error opening video writer device: %v\n", saveFile) + return + } + defer writer.Close() for i := 0; i < 100; i++ { if ok := webcam.Read(img); !ok { diff --git a/pvl/README.md b/pvl/README.md index 659b8d29..a79fb0d4 100644 --- a/pvl/README.md +++ b/pvl/README.md @@ -18,13 +18,12 @@ func main() { deviceID := 0 // open webcam - webcam := opencv3.NewVideoCapture() - defer webcam.Close() - - if ok := webcam.OpenDevice(deviceID); !ok { - fmt.Printf("error opening device: %v\n", deviceID) + webcam, err := opencv3.VideoCaptureDevice(int(deviceID)) + if err != nil { + fmt.Printf("error opening video capture device: %v\n", deviceID) return - } + } + defer webcam.Close() // open display window window := opencv3.NewWindow("PVL") diff --git a/videoio.go b/videoio.go index a251fdd4..fc573a57 100644 --- a/videoio.go +++ b/videoio.go @@ -10,14 +10,27 @@ import ( "unsafe" ) -// VideoCapture is a bind of `cv::VideoCapture`. +// VideoCapture is a wrapper around the cv::VideoCapture class. type VideoCapture struct { p C.VideoCapture } -// NewVideoCapture returns a new video capture. -func NewVideoCapture() VideoCapture { - return VideoCapture{p: C.VideoCapture_New()} +// VideoCaptureFile opens a VideoCapture from a file and prepares to start capturing +func VideoCaptureFile(uri string) (vc VideoCapture, err error) { + vc = VideoCapture{p: C.VideoCapture_New()} + + cURI := C.CString(uri) + defer C.free(unsafe.Pointer(cURI)) + + C.VideoCapture_Open(vc.p, cURI) + return vc, nil +} + +// VideoCaptureDevice opens a VideoCapture from a device and prepares to start capturing +func VideoCaptureDevice(device int) (vc VideoCapture, err error) { + vc = VideoCapture{p: C.VideoCapture_New()} + C.VideoCapture_OpenDevice(vc.p, C.int(device)) + return vc, nil } // Close VideoCapture object. @@ -27,18 +40,6 @@ func (v *VideoCapture) Close() error { return nil } -// Open a video data and prepares to start capturing. -func (v *VideoCapture) Open(uri string) bool { - cURI := C.CString(uri) - defer C.free(unsafe.Pointer(cURI)) - return C.VideoCapture_Open(v.p, cURI) != 0 -} - -// OpenDevice opens a video device and prepares to start capturing. -func (v *VideoCapture) OpenDevice(device int) bool { - return C.VideoCapture_OpenDevice(v.p, C.int(device)) != 0 -} - // Release video capture object. func (v *VideoCapture) Release() { C.VideoCapture_Release(v.p) @@ -72,31 +73,32 @@ type VideoWriter struct { p C.VideoWriter } -// NewVideoWriter returns a new video writer. -func NewVideoWriter() VideoWriter { - return VideoWriter{p: C.VideoWriter_New()} -} +// VideoWriterFile opens a VideoWriter with a specific output file. +func VideoWriterFile(name string, fps float64, width int, height int) (vw VideoWriter, err error) { + vw = VideoWriter{p: C.VideoWriter_New()} -// Close VideoWriter object. -func (vw *VideoWriter) Close() error { - C.VideoWriter_Close(vw.p) - vw.p = nil - return nil -} - -// Open a VideoWriter with a specific output file. -func (vw *VideoWriter) Open(name string, fps float64, width int, height int) { cName := C.CString(name) defer C.free(unsafe.Pointer(cName)) C.VideoWriter_Open(vw.p, cName, C.double(fps), C.int(width), C.int(height)) + return vw, nil } -// OpenWithMat opens a VideoWriter with a specific output file +// VideoWriterFileMat opens a VideoWriter with a specific output file, // using the dimensions from a specific Mat. -func (vw *VideoWriter) OpenWithMat(name string, fps float64, img Mat) { +func VideoWriterFileMat(name string, fps float64, img Mat) (vw VideoWriter, err error) { + vw = VideoWriter{p: C.VideoWriter_New()} + cName := C.CString(name) defer C.free(unsafe.Pointer(cName)) C.VideoWriter_OpenWithMat(vw.p, cName, C.double(fps), img.p) + return vw, nil +} + +// Close VideoWriter object. +func (vw *VideoWriter) Close() error { + C.VideoWriter_Close(vw.p) + vw.p = nil + return nil } // IsOpened checks if the VideoWriter is open and ready to be written to. @@ -106,8 +108,9 @@ func (vw *VideoWriter) IsOpened() bool { } // Write a single Mat image to the open VideoWriter. -func (vw *VideoWriter) Write(img Mat) { +func (vw *VideoWriter) Write(img Mat) error { vw.mu.Lock() defer vw.mu.Unlock() C.VideoWriter_Write(vw.p, img.p) + return nil }