Skip to content

Commit

Permalink
Refactor to use combined create/open functions for VideoCapture and V…
Browse files Browse the repository at this point in the history
…ideoWriter to simplify usage

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 1, 2017
1 parent 3db61c6 commit a47fcad
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 96 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions examples/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions examples/capwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 5 additions & 6 deletions examples/faceblur.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 6 additions & 10 deletions examples/facedetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}

Expand Down
11 changes: 5 additions & 6 deletions examples/pvl/faceblur.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 5 additions & 6 deletions examples/pvl/smiledetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions examples/saveimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
20 changes: 11 additions & 9 deletions examples/savevideo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions pvl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
67 changes: 35 additions & 32 deletions videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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
}

0 comments on commit a47fcad

Please sign in to comment.