gpt4 book ai didi

python - 如何使用 OpenCV 网络摄像头视频和其他 wx 组件(在同一框架/面板内)创建 wxPython 框架?

转载 作者:行者123 更新时间:2023-12-05 07:05:37 26 4
gpt4 key购买 nike

我正在尝试将 wxPython 用于 GUI,它一半是 OpenCV 网络摄像头视频,另一半是 wxPython 的其他组件(按钮、文本框等)。

我已经能够使用以下代码将 OpenCV 窗口放入我的 wxPython 框架中:

import wx
import cv2

vs = cv2.VideoCapture(0)

class ImagePanel(wx.Panel):

def __init__(self, parent, fps=15):
super().__init__(parent)

ret, frame = vs.read()
frame = cv2.flip(frame, 1)
height, width = frame.shape[:2]
parent.SetSize((width, height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

self.bmp = wx.Bitmap.FromBuffer(width, height, frame)

self.timer = wx.Timer(self)
self.timer.Start(1000. / fps)

self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_TIMER, self.NextFrame)

def OnPaint(self, evt):
dc = wx.BufferedPaintDC(self)
dc.DrawBitmap(self.bmp, 0, 0)

def NextFrame(self, event):
ret, frame = vs.read()
if ret:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp.CopyFromBuffer(frame)
self.Refresh()


class MainFrame(wx.Frame):

def __init__(self):
super().__init__(None, title='User Capture')
self.vs = vs
ImagePanel(self)
self.Show()


if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()

结果是 enter image description here

然后我尝试创建一个主面板来容纳 OpenCV 面板和其他组件(现在是一个按钮)

import wx
import cv2
import threading

vs = cv2.VideoCapture(0)

class ImagePanel(wx.Panel):

def __init__(self, parent, fps=15):
super().__init__(parent)

ret, frame = vs.read()
frame = cv2.flip(frame, 1)
height, width = frame.shape[:2]
parent.SetSize((width, height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

self.bmp = wx.Bitmap.FromBuffer(width, height, frame)

self.timer = wx.Timer(self)
self.timer.Start(1000. / fps)

self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_TIMER, self.NextFrame)

def OnPaint(self, evt):
dc = wx.BufferedPaintDC(self)
dc.DrawBitmap(self.bmp, 0, 0)

def NextFrame(self, event):
ret, frame = vs.read()
if ret:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp.CopyFromBuffer(frame)
self.Refresh()


class MainPanel(wx.Panel):

def __init__(self, frame):
wx.Panel.__init__(self, frame)
main_sizer = wx.BoxSizer(wx.VERTICAL)

button_sizer = self._button_sizer(frame)
img = ImagePanel(self)
main_sizer.Add(img)
main_sizer.Add(button_sizer)
self.SetSizer(main_sizer)
self.Show()

def _button_sizer(self, frame):
snap = wx.Button(self, label='Snap')
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add(snap)
button_sizer.Add((-1, -1), proportion=1)
return button_sizer


class MainFrame(wx.Frame):

def __init__(self):
super().__init__(None, title='User Capture')
self.vs = vs
MainPanel(self)
self.Show()


if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()

结果是:

enter image description here

我可以说我的 wxPython 实现显然不好,所以我需要一些帮助来正确地执行此操作

最佳答案

你已经很接近了。
尽量不要将 frame 用作 parent 或任何其他看起来像保留字的词。
这没有错,只是令人困惑。
这是您编辑的代码。 (注:给猫剥皮的方法有很多种)

import wx

class ImagePanel(wx.Panel):

def __init__(self, parent, fps=15):
super().__init__(parent)

self.bmp = wx.StaticBitmap(self, -1, wx.Bitmap("frame1.png"))

class MainPanel(wx.Panel):

def __init__(self, parent):
wx.Panel.__init__(self, parent)
main_sizer = wx.BoxSizer(wx.VERTICAL)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
img = ImagePanel(self)
snap = wx.Button(self, -1, label='Snap')

main_sizer.Add(img, 1, wx.EXPAND, 0)
button_sizer.Add(snap)
main_sizer.Add(button_sizer)
self.SetSizer(main_sizer)
snap.Bind(wx.EVT_BUTTON, self.OnButton)

def OnButton(self, event):
print("Button Pressed")

class MainFrame(wx.Frame):

def __init__(self):
super().__init__(None, title='User Capture')
MainPanel(self)
self.Show()

if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()

enter image description here

关于python - 如何使用 OpenCV 网络摄像头视频和其他 wx 组件(在同一框架/面板内)创建 wxPython 框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62678893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com