Skip to content

Commit

Permalink
opencv: Switchover to use GoCV and OpenCV 3.3
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 5, 2017
1 parent e333001 commit 3113178
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 69 deletions.
35 changes: 30 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,46 @@ matrix:
addons:
apt:
packages:
- libcv-dev
- libcvaux-dev
- libhighgui-dev
- libopencv-dev
- libgmp-dev
- build-essential
- cmake
- git
- libgtk2.0-dev
- pkg-config
- libavcodec-dev
- libavformat-dev
- libswscale-dev
- libtbb2
- libtbb-dev
- libjpeg-dev
- libpng-dev
- libtiff-dev
- libjasper-dev
- libdc1394-22-dev
- libsdl2-dev
- libsdl2-image-dev
- libsdl2-2.0.0
- libusb-dev
- xvfb
- unzip
- libgtk2.0-0
before_install:
- ./travis_build_opencv.sh
- export PKG_CONFIG_PATH=$(pkg-config --variable pc_path pkg-config):$HOME/usr/lib/pkgconfig
- export INCLUDE_PATH=$HOME/usr/include:${INCLUDE_PATH}
- export LD_LIBRARY_PATH=$HOME/usr/lib:${LD_LIBRARY_PATH}
- sudo ln /dev/null /dev/raw1394
- cd $HOME/gopath/src/gobot.io/x/gobot
- go get github.com/axw/gocov/gocov
before_cache:
- rm -f $HOME/fresh-cache
install:
- make deps
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
script:
- export CGO_CPPFLAGS="-I${HOME}/usr/include"
- export CGO_LDFLAGS="-L${HOME}/usr/lib -lopencv_core -lopencv_videoio -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_objdetect -lopencv_calib3d"
- echo "Ensuring code is well formatted"; ! gofmt -s -d . | read
- bash -c 'set -e; echo "" > coverage.txt; for d in $(go list ./...); do go test -covermode=set -coverprofile=p.out $d; if [ -f p.out ]; then cat p.out >> coverage.txt; rm p.out; fi; done'
after_success:
Expand All @@ -42,3 +62,8 @@ branches:
- gobot.io
- /^gobot-.*$/
secure: "HggklzWOwKqImvjQe1yvojCoTaUBDrOVXRjsrZaoTaKpUtmho1tpCMtKF1dbyT0T5Y68P6f9e/XyANWVeziJNZ+YmNkY+CNdNYHiNnpl8att3MuL4hpwKDPAqLK8H2G+71j3O/rBvf6oIJHtSEesR5Sbb+2fSmhNFtLrDgp5FZA="
# Caching so the next build will be fast as possible.
cache:
timeout: 1000
directories:
- $HOME/usr
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ deps:
github.com/eclipse/paho.mqtt.golang \
github.com/hashicorp/go-multierror \
github.com/hybridgroup/go-ardrone/client \
github.com/lazywei/go-opencv \
github.com/hybridgroup/gocv \
github.com/mgutz/logxi/v1 \
github.com/nats-io/nats \
github.com/sigurn/crc8 \
Expand Down
22 changes: 7 additions & 15 deletions examples/opencv_face_detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ package main
import (
"path"
"runtime"
"time"
//"time"

cv "github.com/lazywei/go-opencv/opencv"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/opencv"
"github.com/hybridgroup/gocv"
)

func main() {
Expand All @@ -22,20 +22,12 @@ func main() {
camera := opencv.NewCameraDriver(0)

work := func() {
var image *cv.IplImage

camera.On(opencv.Frame, func(data interface{}) {
image = data.(*cv.IplImage)
})

gobot.Every(500*time.Millisecond, func() {
if image != nil {
i := image.Clone()
faces := opencv.DetectFaces(cascade, i)
i = opencv.DrawRectangles(i, faces, 0, 255, 0, 5)
window.ShowImage(i)
}

i := data.(gocv.Mat)
faces := opencv.DetectFaces(cascade, i)
opencv.DrawRectangles(i, faces, 0, 255, 0, 5)
window.ShowImage(i)
window.WaitKey(1)
})
}

Expand Down
8 changes: 5 additions & 3 deletions examples/opencv_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package main

import (
cv "github.com/lazywei/go-opencv/opencv"
"github.com/hybridgroup/gocv"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/opencv"
)
Expand All @@ -15,8 +15,10 @@ func main() {
camera := opencv.NewCameraDriver(0)

work := func() {
camera.On(camera.Event("frame"), func(data interface{}) {
window.ShowImage(data.(*cv.IplImage))
camera.On(opencv.Frame, func(data interface{}) {
img := data.(gocv.Mat)
window.ShowImage(img)
window.WaitKey(1)
})
}

Expand Down
12 changes: 7 additions & 5 deletions platforms/opencv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ For more info about OpenCV click [here](http://opencv.org/)

## How to Install

This package requires OpenCV version 2.4 to be installed on your system. Please note that it is not compatible with OpenCV 3.x at this time.
This package requires OpenCV version 3.3 to be installed on your system.

### OSX

To install OpenCV on OSX using Homebrew:

```
$ brew tap homebrew/science && brew install opencv
$ brew install opencv
```

### Ubuntu
Expand Down Expand Up @@ -44,7 +44,7 @@ Example using the camera.
package main

import (
cv "github.com/lazywei/go-opencv/opencv"
"github.com/hybridgroup/gocv"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/opencv"
)
Expand All @@ -54,8 +54,10 @@ func main() {
camera := opencv.NewCameraDriver(0)

work := func() {
camera.On(camera.Event("frame"), func(data interface{}) {
window.ShowImage(data.(*cv.IplImage))
camera.On(opencv.Frame, func(data interface{}) {
img := data.(gocv.Mat)
window.ShowImage(img)
window.WaitKey(1)
})
}

Expand Down
17 changes: 7 additions & 10 deletions platforms/opencv/camera_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (

"time"

cv "github.com/lazywei/go-opencv/opencv"
"github.com/hybridgroup/gocv"
"gobot.io/x/gobot"
)

type capture interface {
RetrieveFrame(int) *cv.IplImage
GrabFrame() bool
Read(img gocv.Mat) bool
}

const (
Expand Down Expand Up @@ -40,9 +39,9 @@ func NewCameraDriver(source interface{}, v ...time.Duration) *CameraDriver {
start: func(c *CameraDriver) (err error) {
switch v := c.Source.(type) {
case string:
c.camera = cv.NewFileCapture(v)
c.camera, _ = gocv.VideoCaptureFile(v)
case int:
c.camera = cv.NewCameraCapture(v)
c.camera, _ = gocv.VideoCaptureDevice(v)
default:
return errors.New("Unknown camera source")
}
Expand Down Expand Up @@ -74,13 +73,11 @@ func (c *CameraDriver) Start() (err error) {
if err := c.start(c); err != nil {
return err
}
img := gocv.NewMat()
go func() {
for {
if c.camera.GrabFrame() {
image := c.camera.RetrieveFrame(1)
if image != nil {
c.Publish(Frame, image)
}
if ok := c.camera.Read(img); ok {
c.Publish(Frame, img)
}
time.Sleep(c.interval)
}
Expand Down
12 changes: 5 additions & 7 deletions platforms/opencv/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package opencv

import cv "github.com/lazywei/go-opencv/opencv"
import (
"github.com/hybridgroup/gocv"
)

type testCapture struct{}

func (c *testCapture) RetrieveFrame(i int) *cv.IplImage {
return &cv.IplImage{}
}

func (c *testCapture) GrabFrame() bool {
func (c *testCapture) Read(img gocv.Mat) bool {
return true
}

type testWindow struct{}

func (w *testWindow) ShowImage(i *cv.IplImage) { return }
func (w *testWindow) ShowImage(img gocv.Mat) { return }
33 changes: 21 additions & 12 deletions platforms/opencv/utils.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package opencv

import (
cv "github.com/lazywei/go-opencv/opencv"
"image"
"image/color"

"github.com/hybridgroup/gocv"
)

var classifier *gocv.CascadeClassifier

// loadHaarClassifierCascade returns open cv HaarCascade loaded
func loadHaarClassifierCascade(haar string) *cv.HaarCascade {
return cv.LoadHaarClassifierCascade(haar)
func loadCascadeClassifier(haar string) *gocv.CascadeClassifier {
if classifier != nil {
return classifier
}

c := gocv.NewCascadeClassifier()
c.Load(haar)
classifier = &c
return classifier
}

// DetectFaces loads Haar cascade to detect face objects in image
func DetectFaces(haar string, image *cv.IplImage) []*cv.Rect {
return loadHaarClassifierCascade(haar).DetectObjects(image)
func DetectFaces(haar string, img gocv.Mat) []image.Rectangle {
return loadCascadeClassifier(haar).DetectMultiScale(img)
}

// DrawRectangles uses Rect array values to return image with rectangles drawn.
func DrawRectangles(image *cv.IplImage, rect []*cv.Rect, r int, g int, b int, thickness int) *cv.IplImage {
for _, value := range rect {
cv.Rectangle(image,
cv.Point{value.X() + value.Width(), value.Y()},
cv.Point{value.X(), value.Y() + value.Height()},
cv.NewScalar(float64(b), float64(g), float64(r), 0), thickness, 1, 0)
func DrawRectangles(img gocv.Mat, rects []image.Rectangle, r int, g int, b int, thickness int) {
for _, rect := range rects {
gocv.Rectangle(img, rect, color.RGBA{uint8(r), uint8(g), uint8(b), 0}, thickness)
}
return image
return
}
6 changes: 3 additions & 3 deletions platforms/opencv/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"runtime"
"testing"

cv "github.com/lazywei/go-opencv/opencv"
"github.com/hybridgroup/gocv"
"gobot.io/x/gobot/gobottest"
)

func TestUtils(t *testing.T) {
_, currentfile, _, _ := runtime.Caller(0)
image := cv.LoadImage(path.Join(path.Dir(currentfile), "lena-256x256.jpg"))
image := gocv.IMRead(path.Join(path.Dir(currentfile), "lena-256x256.jpg"), gocv.IMReadColor)
rect := DetectFaces("haarcascade_frontalface_alt.xml", image)
gobottest.Refute(t, len(rect), 0)
gobottest.Refute(t, DrawRectangles(image, rect, 0, 0, 0, 0), nil)
DrawRectangles(image, rect, 0, 0, 0, 0)
}
16 changes: 10 additions & 6 deletions platforms/opencv/window_driver.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package opencv

import (
cv "github.com/lazywei/go-opencv/opencv"
"github.com/hybridgroup/gocv"
"gobot.io/x/gobot"
)

type window interface {
ShowImage(*cv.IplImage)
IMShow(gocv.Mat)
}

// WindowDriver is the Gobot Driver for the OpenCV window
Expand All @@ -22,7 +22,7 @@ func NewWindowDriver() *WindowDriver {
return &WindowDriver{
name: "Window",
start: func(w *WindowDriver) {
w.window = cv.NewWindow(w.Name(), cv.CV_WINDOW_NORMAL)
w.window = gocv.NewWindow(w.Name())
},
}
}
Expand All @@ -38,7 +38,6 @@ func (w *WindowDriver) Connection() gobot.Connection { return nil }

// Start starts window thread and driver
func (w *WindowDriver) Start() (err error) {
cv.StartWindowThread()
w.start(w)
return
}
Expand All @@ -47,6 +46,11 @@ func (w *WindowDriver) Start() (err error) {
func (w *WindowDriver) Halt() (err error) { return }

// ShowImage displays image in window
func (w *WindowDriver) ShowImage(image *cv.IplImage) {
w.window.ShowImage(image)
func (w *WindowDriver) ShowImage(img gocv.Mat) {
w.window.IMShow(img)
}

// WaitKey gives window a chance to redraw, and checks for keyboard input
func (w *WindowDriver) WaitKey(pause int) int {
return gocv.WaitKey(pause)
}
4 changes: 2 additions & 2 deletions platforms/opencv/window_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

cv "github.com/lazywei/go-opencv/opencv"
"github.com/hybridgroup/gocv"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestWindowDriverHalt(t *testing.T) {
func TestWindowDriverShowImage(t *testing.T) {
d := initTestWindowDriver()
_, currentfile, _, _ := runtime.Caller(0)
image := cv.LoadImage(path.Join(path.Dir(currentfile), "lena-256x256.jpg"))
image := gocv.IMRead(path.Join(path.Dir(currentfile), "lena-256x256.jpg"), gocv.IMReadColor)
d.Start()
d.ShowImage(image)
}
Loading

0 comments on commit 3113178

Please sign in to comment.