From 3c0c949f314ee56f20871ab99c04ace9510cc80a Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Wed, 14 Mar 2018 13:45:38 +0100 Subject: [PATCH] Add support for OpenCV 3 --- camera/camera.go | 49 +++-------------------------- camera/camera_const.go | 44 ++++++++++++++++++++++++++ camera/camera_cv3.go | 70 ++++++++++++++++++++++++++++++++++++++++++ make.bash | 1 + video/video.go | 2 ++ video/video_cv3.go | 60 ++++++++++++++++++++++++++++++++++++ 6 files changed, 181 insertions(+), 45 deletions(-) create mode 100644 camera/camera_const.go create mode 100644 camera/camera_cv3.go create mode 100644 video/video_cv3.go diff --git a/camera/camera.go b/camera/camera.go index 3ae335c..c7fae31 100644 --- a/camera/camera.go +++ b/camera/camera.go @@ -1,3 +1,5 @@ +// +build !cv3 + // Package camera. package camera @@ -8,49 +10,6 @@ import ( "github.com/lazywei/go-opencv/opencv" ) -// Property identifiers. -const ( - PropPosMsec = iota - PropPosFrames - PropPosAviRatio - PropFrameWidth - PropFrameHeight - PropFps - PropFourcc - PropFrameCount - PropFormat - PropMode - PropBrightness - PropContrast - PropSaturation - PropHue - PropGain - PropExposure - PropConvertRgb - PropWhiteBalanceU - PropRectification - PropMonocrome - PropSharpness - PropAutoExposure - PropGamma - PropTemperature - PropTrigger - PropTriggerDelay - PropWhiteBalanceV - PropZoom - PropFocus - PropGuid - PropIsoSpeed - PropMaxDc1394 - PropBacklight - PropPan - PropTilt - PropRoll - PropIris - PropSettings - PropBuffersize -) - // Camera represents camera. type Camera struct { camera *opencv.Capture @@ -86,8 +45,8 @@ func (c *Camera) GetProperty(id int) float64 { } // SetProperty sets a camera property. -func (c *Camera) SetProperty(id int, value float64) int { - return c.camera.SetProperty(id, value) +func (c *Camera) SetProperty(id int, value float64) { + c.camera.SetProperty(id, value) } // Close closes camera. diff --git a/camera/camera_const.go b/camera/camera_const.go new file mode 100644 index 0000000..036cd84 --- /dev/null +++ b/camera/camera_const.go @@ -0,0 +1,44 @@ +package camera + +// Property identifiers. +const ( + PropPosMsec = iota + PropPosFrames + PropPosAviRatio + PropFrameWidth + PropFrameHeight + PropFps + PropFourcc + PropFrameCount + PropFormat + PropMode + PropBrightness + PropContrast + PropSaturation + PropHue + PropGain + PropExposure + PropConvertRgb + PropWhiteBalanceU + PropRectification + PropMonocrome + PropSharpness + PropAutoExposure + PropGamma + PropTemperature + PropTrigger + PropTriggerDelay + PropWhiteBalanceV + PropZoom + PropFocus + PropGuid + PropIsoSpeed + PropMaxDc1394 + PropBacklight + PropPan + PropTilt + PropRoll + PropIris + PropSettings + PropBuffersize +) diff --git a/camera/camera_cv3.go b/camera/camera_cv3.go new file mode 100644 index 0000000..b6248ff --- /dev/null +++ b/camera/camera_cv3.go @@ -0,0 +1,70 @@ +// +build cv3 + +// Package camera. +package camera + +import ( + "fmt" + "image" + + "gocv.io/x/gocv" +) + +// Camera represents camera. +type Camera struct { + camera *gocv.VideoCapture +} + +// New returns new Camera for given camera index. +func New(index int) (camera *Camera, err error) { + camera = &Camera{} + + camera.camera, err = gocv.VideoCaptureDevice(index) + if err != nil { + err = fmt.Errorf("camera: can not open camera %d: %s", index, err.Error()) + } + + return +} + +// Read reads next frame from camera and returns image. +func (c *Camera) Read() (img image.Image, err error) { + mat := gocv.NewMat() + defer mat.Close() + + ok := c.camera.Read(mat) + if !ok { + err = fmt.Errorf("camera: can not grab frame") + return + } + + img, e := mat.ToImage() + if e != nil { + err = fmt.Errorf("camera: %v", e) + return + } + + return +} + +// GetProperty returns the specified camera property. +func (c *Camera) GetProperty(id int) float64 { + return c.camera.Get(gocv.VideoCaptureProperties(id)) +} + +// SetProperty sets a camera property. +func (c *Camera) SetProperty(id int, value float64) { + c.camera.Set(gocv.VideoCaptureProperties(id), value) +} + +// Close closes camera. +func (c *Camera) Close() (err error) { + if c.camera == nil { + err = fmt.Errorf("camera: camera is not opened") + return + } + + err = c.camera.Close() + c.camera = nil + return +} diff --git a/make.bash b/make.bash index d4cbf82..8244294 100755 --- a/make.bash +++ b/make.bash @@ -11,6 +11,7 @@ LIBRARY_PATH="$CHROOT/usr/lib:$CHROOT/lib" \ PKG_CONFIG_PATH="$CHROOT/usr/lib/pkgconfig" \ PKG_CONFIG_LIBDIR="$CHROOT/usr/lib/pkgconfig" \ CGO_LDFLAGS="-L$CHROOT/usr/lib -L$CHROOT/lib" \ +CGO_CFLAGS="-I$CHROOT/usr/include" \ CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v -x -o build/cam2ip.linux.amd64 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip diff --git a/video/video.go b/video/video.go index 878fbb9..1894360 100644 --- a/video/video.go +++ b/video/video.go @@ -1,3 +1,5 @@ +// +build !cv3 + // Package video. package video diff --git a/video/video_cv3.go b/video/video_cv3.go new file mode 100644 index 0000000..f507403 --- /dev/null +++ b/video/video_cv3.go @@ -0,0 +1,60 @@ +// +build cv3 + +// Package video. +package video + +import ( + "fmt" + "image" + + "gocv.io/x/gocv" +) + +// Video represents video. +type Video struct { + video *gocv.VideoCapture +} + +// New returns new Video for given path. +func New(filename string) (video *Video, err error) { + video = &Video{} + + video.video, err = gocv.VideoCaptureFile(filename) + if err != nil { + err = fmt.Errorf("video: can not open video %s: %s", filename, err.Error()) + } + + return +} + +// Read reads next frame from video and returns image. +func (v *Video) Read() (img image.Image, err error) { + mat := gocv.NewMat() + defer mat.Close() + + ok := v.video.Read(mat) + if !ok { + err = fmt.Errorf("video: can not grab frame") + return + } + + img, e := mat.ToImage() + if e != nil { + err = fmt.Errorf("video: %v", e) + return + } + + return +} + +// Close closes video. +func (v *Video) Close() (err error) { + if v.video == nil { + err = fmt.Errorf("video: video is not opened") + return + } + + err = v.video.Close() + v.video = nil + return +}