在 macOS 上使用 gocv 时,偶尔会出现 NSInternalInconsistencyException 异常。

huangapple go评论107阅读模式
英文:

NSInternalInconsistencyException occurs intermittently when using gocv on macOS

问题

我有这段使用gocv(为OpenCV提供Go语言绑定)的代码。它在图像上简单地绘制了一些矩形并显示结果。

func main() {
    resp, err := http.Get("http://localhost:6060/template-match")
    if err != nil {
        panic(err)
    }

    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(body))
    var data Response
    err = json.Unmarshal(body, &data)
    if err != nil {
        panic(err)
    }
    srcImage := gocv.IMRead("./images/src1.jpg", gocv.IMReadColor)
    for i := 0; i < len(data.Data); i++ {
        gocv.Rectangle(&srcImage, data.Data[i], color.RGBA{R: 255}, 2)
    }
    window := gocv.NewWindow("match-result")
    window.IMShow(srcImage)
    gocv.WaitKey(0)
    fmt.Println(data.Data, data.Msg)
}

我遇到了这个错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'

我真的很困惑,因为这个错误并不是每次都发生。

英文:

I have this code using gocv(provides Go language bindings for OpenCV).
It simply draw some rectangles on the image and show the result.

func main() {
resp, err := http.Get(&quot;http://localhost:6060/template-match&quot;)
if err != nil {
	panic(err)
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
	panic(err)
}
fmt.Println(string(body))
var data Response
err = json.Unmarshal(body, &amp;data)
if err != nil {
	panic(err)
}
srcImage := gocv.IMRead(&quot;./images/src1.jpg&quot;, gocv.IMReadColor)
for i := 0; i &lt; len(data.Data); i++ {
	gocv.Rectangle(&amp;srcImage, data.Data[i], color.RGBA{R: 255}, 2)
}
window := gocv.NewWindow(&quot;match-result&quot;)
window.IMShow(srcImage)
gocv.WaitKey(0)
fmt.Println(data.Data, data.Msg)

I'm getting this error:

Terminating app due to uncaught exception &#39;NSInternalInconsistencyException&#39;, reason: &#39;NSWindow drag regions should only be invalidated on the Main Thread!&#39;

I'm really confused because this error does not occur every time

答案1

得分: 0

我还没有使用过这个库,但是看起来这是一个已知的问题(https://github.com/hybridgroup/gocv/issues/599#issuecomment-579112179)。由于你提到它只是偶尔发生,所以很可能是与线程上下文有关的错误。那里的答案(来自库的作者)指向了https://github.com/golang/go/wiki/LockOSThread,所以你的主包应该实现类似以下的代码:

func init() {
    runtime.LockOSThread()
}
英文:

I've not used this library, but it looks like this is a known issue – and, since you mentioned it happens only sometimes, it certainly sounds like the error is dependent upon thread context. The answer there (from the library author) points to https://github.com/golang/go/wiki/LockOSThread, so your main package should implement something like

func init() {
	runtime.LockOSThread()
}

huangapple
  • 本文由 发表于 2023年4月28日 21:13:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76129950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定