OPENCV和GTK&#x2B之间的不兼容问题3

发布于 2025-01-27 04:16:26 字数 977 浏览 3 评论 0 原文

我正在使用 Python 3.10.4 使用 OpenCV gtk+ 3 开发实时屏幕捕获程序。我遇到了一个问题,我无法解决 - gtk+ 3 似乎与 opencv 不兼容。为了给我的问题提供更多上下文,我使用 gtk+ 3 使用 gdkpixbuf 从任何选定的窗口(或最小化)捕获每个帧,并且我正在使用 OPENCV 有效地处理并实时显示每个捕获的帧。

这是问题。考虑以下简单示例:

import numpy as np
import cv2
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

img = np.zeros((100,100,3))
cv2.imshow('test', img) # ERROR happens here
cv2.waitKey(0)

以下代码产生分段故障(Sigsegv)(退出代码139,Signal 11)。当调用 cv2.imshow()时,分割故障完全发生。但是,如果我要通过写 gi.require_version(“ gtk”,“ 2.0”) gtk 版本更改为2,或者完全删除它,则sigsegv却不发生。这不是解决方案,因为我至少需要 gtk+ 3

到目前为止,我尝试降级 openCV ,但没有其他版本可行。我发现正在工作的是 pygtk 代替 gtk+ 3 ,但是 pygtk 非常旧且折旧 - 我不想使用它。

如何使 gtk+ 3 OpenCV 一起使用?有可能吗?

I am developing a real-time screen-capturing program using Python 3.10.4 with OpenCV and GTK+ 3. I ran into an issue I cannot solve - GTK+ 3 seems to be incompatible with OpenCV. To give a bit more context to my problem, I am using GTK+ 3 to capture each frame from any selected window (minimized or not) using GdkPixbuf and I am using OpenCV to efficiently process and show each captured frame in real-time.

Here's the issue. Consider the following simple example:

import numpy as np
import cv2
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

img = np.zeros((100,100,3))
cv2.imshow('test', img) # ERROR happens here
cv2.waitKey(0)

The following code produces a segmentation fault (SIGSEGV) (exit code 139, signal 11). The segmentation fault happens exactly when cv2.imshow() is called. If, however, I were to change GTK version to 2, by writting gi.require_version("Gtk", "2.0") or remove it altogether, the SIGSEGV does not occur. This is not a solution, since I need GTK+ 3 at the very least.

So far, I tried downgrading OpenCV, but no other version worked. What I found to be working is PyGTK in place of GTK+ 3, but PyGTK is very old and depreciated - I do not want to use it.

How can I make GTK+ 3 work with OpenCV? Is it even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ弥猫深巷。 2025-02-03 04:16:26

cv2.imshow 可能不应该在GUIS中使用,但是对于快速和dirty的解决方案,请参见以下代码(在GTK 3.42 @ Python 3.10.1上测试):

import time
import threading
import cv2
import numpy

cv2.imshow('warmup', numpy.zeros((100,100,3)))
time.sleep(0.5)
cv2.destroyAllWindows()

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

demo = Gtk.Window()
btn = Gtk.Button.new_with_label('Input test')
btn.connect('clicked', lambda _: print('Input works'))
demo.add(btn)

# Capturing from webcam here, but other sources will probably work too
cap = cv2.VideoCapture(0)
cap.open(0)

def cv2_loop():
  while cap.isOpened():
    ret, frame = cap.read()
    if not ret: break

    cv2.imshow('capture', frame)

cv2_thread = threading.Thread(target=cv2_loop)
cv2_thread.start()

def done(*args):
  cap.release()
  cv2.destroyAllWindows()
  cv2_thread.join()
  Gtk.main_quit()

demo.connect('destroy', done)
demo.show_all()

Gtk.main()
cv2_thread.join()

技巧是强制强制 - 在导入GTK东西之前进行启动OPENCV。

cv2.imshow is probably not supposed to be used in GUIs, but for a quick-and-dirty solution see the following code (tested on gtk 3.42 @ python 3.10.1):

import time
import threading
import cv2
import numpy

cv2.imshow('warmup', numpy.zeros((100,100,3)))
time.sleep(0.5)
cv2.destroyAllWindows()

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

demo = Gtk.Window()
btn = Gtk.Button.new_with_label('Input test')
btn.connect('clicked', lambda _: print('Input works'))
demo.add(btn)

# Capturing from webcam here, but other sources will probably work too
cap = cv2.VideoCapture(0)
cap.open(0)

def cv2_loop():
  while cap.isOpened():
    ret, frame = cap.read()
    if not ret: break

    cv2.imshow('capture', frame)

cv2_thread = threading.Thread(target=cv2_loop)
cv2_thread.start()

def done(*args):
  cap.release()
  cv2.destroyAllWindows()
  cv2_thread.join()
  Gtk.main_quit()

demo.connect('destroy', done)
demo.show_all()

Gtk.main()
cv2_thread.join()

The trick is to force-initialize OpenCV before you import GTK stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文