我正在使用 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?
发布评论
评论(1)
cv2.imshow
可能不应该在GUIS中使用,但是对于快速和dirty的解决方案,请参见以下代码(在GTK 3.42 @ Python 3.10.1上测试):技巧是强制强制 - 在导入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):The trick is to force-initialize OpenCV before you import GTK stuff.