Skip to content

Commit d5f669a

Browse files
cgojinSean-Der
authored andcommitted
Fix gocv-receive example
Error build: cannot range over contours (type gocv.PointsVector). Error run on macOS: NSWindow drag regions should only be invalidated on the Main Thread!
1 parent 18f92c0 commit d5f669a

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

gocv-receive/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This example requires you have GoCV and ffmpeg installed, these are the supporte
1111
#### Debian/Ubuntu
1212
* Follow the setup instructions for [GoCV](https://github.com/hybridgroup/gocv)
1313
* `sudo apt-get install ffmpeg`
14+
#### macOS
15+
* `brew install ffmpeg opencv`
1416

1517
### Build gocv-receive
1618
```

gocv-receive/main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"image/color"
1010
"io"
1111
"os/exec"
12+
"runtime"
1213
"strconv"
1314
"time"
1415

@@ -26,6 +27,11 @@ const (
2627
minimumArea = 3000
2728
)
2829

30+
func init() {
31+
// On macOS, NSWindow drag regions should only be invalidated on the Main Thread!
32+
runtime.LockOSThread()
33+
}
34+
2935
func main() {
3036
ffmpeg := exec.Command("ffmpeg", "-i", "pipe:0", "-pix_fmt", "bgr24", "-s", strconv.Itoa(frameX)+"x"+strconv.Itoa(frameY), "-f", "rawvideo", "pipe:1") //nolint
3137
ffmpegIn, _ := ffmpeg.StdinPipe()
@@ -93,8 +99,9 @@ func startGoCVMotionDetect(ffmpegOut io.Reader) {
9399

94100
// now find contours
95101
contours := gocv.FindContours(imgThresh, gocv.RetrievalExternal, gocv.ChainApproxSimple)
96-
for i, c := range contours {
97-
area := gocv.ContourArea(c)
102+
103+
for i := 0; i < contours.Size(); i++ {
104+
area := gocv.ContourArea(contours.At(i))
98105
if area < minimumArea {
99106
continue
100107
}
@@ -103,7 +110,7 @@ func startGoCVMotionDetect(ffmpegOut io.Reader) {
103110
statusColor = color.RGBA{255, 0, 0, 0}
104111
gocv.DrawContours(&img, contours, i, statusColor, 2)
105112

106-
rect := gocv.BoundingRect(c)
113+
rect := gocv.BoundingRect(contours.At(i))
107114
gocv.Rectangle(&img, rect, color.RGBA{0, 0, 255, 0}, 2)
108115
}
109116

0 commit comments

Comments
 (0)