【问题标题】:webcam feed with opencv and tkinter带有 opencv 和 tkinter 的网络摄像头
【发布时间】:2018-02-22 05:18:11
【问题描述】:

我使用opencvtkinter 在python 中编写了一个程序,它提供显示视频网络摄像头供稿,我可以做快照。我有一个称为快照的按钮和保存为时间戳的文件名,但我想由我自己的(用户)保存文件名而不是时间戳。我不知道,我是 python 和 tkinter 的初学者。你能帮帮我吗?

以下是我的 photoboothapp 代码:

from __future__ import print_function
from PIL import Image
from PIL import ImageTk
import tkinter as tki
import threading
import datetime
import imutils
import cv2
import os

class PhotoBoothApp:
    def __init__(self, vs, outputPath):
        self.vs = vs
        self.outputPath = outputPath
        self.frame = None
        self.thread = None
        self.stopEvent = None

        # initialize the root window and image panel
        self.root = tki.Tk()
        self.panel = None

        # create a button, that when pressed, will take the current
        # frame and save it to file
                btm = tki.Button(self.root, text="name",command=callback)
        btn = tki.Button(self.root, text="Snapshot!",
            command=self.takeSnapshot)

                #btm = tki.Button(self.root, text="name",command=callback)

                #btm=tki.Button(self.root,text="name",command=callback)
        btn.pack(side="bottom", fill="both", expand="yes", padx=10,
            pady=10)
                #btm.pack(side="bottom",fill="both",expand="yes", padx=15,pady=15)
        # start a thread that constantly pools the video sensor for
        # the most recently read frame
        self.stopEvent = threading.Event()
        self.thread = threading.Thread(target=self.videoLoop, args=())
        self.thread.start()

        # set a callback to handle when the window is closed
        self.root.wm_title("PyImageSearch PhotoBooth")
        self.root.wm_protocol("WM_DELETE_WINDOW", self.onClose)

    def videoLoop(self):
        # DISCLAIMER:
        # I'm not a GUI developer, nor do I even pretend to be. This
        # try/except statement is a pretty ugly hack to get around
        # a RunTime error that Tkinter throws due to threading
        try:
            # keep looping over frames until we are instructed to stop
            while not self.stopEvent.is_set():
                # grab the frame from the video stream and resize it to
                # have a maximum width of 300 pixels
                self.frame = self.vs.read()
                self.frame = imutils.resize(self.frame, width=300)

                # OpenCV represents images in BGR order; however PIL
                # represents images in RGB order, so we need to swap
                # the channels, then convert to PIL and ImageTk format
                image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
                image = Image.fromarray(image)
                image = ImageTk.PhotoImage(image)

                # if the panel is not None, we need to initialize it
                if self.panel is None:
                    self.panel = tki.Label(image=image)
                    self.panel.image = image
                    self.panel.pack(side="left", padx=10, pady=10)

                # otherwise, simply update the panel
                else:
                    self.panel.configure(image=image)
                    self.panel.image = image

        except RuntimeError as e:
            print("[INFO] caught a RuntimeError")

    def takeSnapshot(self):
        # grab the current timestamp and use it to construct the
        # output path
        ts = datetime.datetime.now()
        filename = "{}.jpg".format(ts.strftime("%Y-%m-%d_%H-%M-%S"))
        p = os.path.sep.join((self.outputPath, filename))

        # save the file
        cv2.imwrite(p, self.frame.copy())
        print("[INFO] saved {}".format(filename))

    def onClose(self):
        # set the stop event, cleanup the camera, and allow the rest of
        # the quit process to continue
        print("[INFO] closing...")
        self.stopEvent.set()
        self.vs.stop()
        self.root.quit()

【问题讨论】:

  • 看看这个SO linkasksaveasfilename() 将打开一个对话框,该对话框将执行您想做的事情,然后您可以在任意位置添加一个按钮,然后单击该按钮可以在您的 takeSnapshot() 中调用此函数。
  • 我尝试过,但我无法解决这个问题..我是 python 的初学者
  • 您在尝试时遇到的错误是什么...您编写的程序是什么,请在您的帖子问题中更新它。别担心,一定会有人帮你的
  • 我的问题是如何在快照按钮下方添加按钮??我不知道我如何定义按钮!!
  • 在您的代码中,这一行创建了一个按钮btn = tki.Button(self.root, text="Snapshot!", command=self.takeSnapshot),这意味着它的parentroot,按钮上的text 将出现Snapshot!,它将调用@987654332 @点击时。

标签: python opencv tkinter


【解决方案1】:

您可以使用 tkinter python 的 Entry 小部件。

在按钮上方添加此行。

Text=Entry(self.root)
Text.pack()

并在 takeSnapshot() 函数中在编写时执行此操作。

p=os.path.sep.join((Text.get(), filename))

我已编辑您的代码以便更好地理解。

from __future__ import print_function
from PIL import Image
from PIL import ImageTk
import tkinter as tki
import threading
import datetime
import imutils
import cv2
import os

class PhotoBoothApp:
    def __init__(self, vs, outputPath):
        self.vs = vs
        self.outputPath = outputPath
        self.frame = None
        self.thread = None
        self.stopEvent = None

        # initialize the root window and image panel
        self.root = tki.Tk()
        self.panel = None

        # create a button, that when pressed, will take the current
        # frame and save it to file
        btm = tki.Button(self.root, text="name",command=callback)
        Text=Entry(self.root)
        Text.pack()
        btn = tki.Button(self.root, text="Snapshot!",
            command=self.takeSnapshot)

                #btm = tki.Button(self.root, text="name",command=callback)

                #btm=tki.Button(self.root,text="name",command=callback)
        btn.pack(side="bottom", fill="both", expand="yes", padx=10,
            pady=10)
                #btm.pack(side="bottom",fill="both",expand="yes", padx=15,pady=15)
        # start a thread that constantly pools the video sensor for
        # the most recently read frame
        self.stopEvent = threading.Event()
        self.thread = threading.Thread(target=self.videoLoop, args=())
        self.thread.start()

        # set a callback to handle when the window is closed
        self.root.wm_title("PyImageSearch PhotoBooth")
        self.root.wm_protocol("WM_DELETE_WINDOW", self.onClose)

    def videoLoop(self):
        # DISCLAIMER:
        # I'm not a GUI developer, nor do I even pretend to be. This
        # try/except statement is a pretty ugly hack to get around
        # a RunTime error that Tkinter throws due to threading
        try:
            # keep looping over frames until we are instructed to stop
            while not self.stopEvent.is_set():
                # grab the frame from the video stream and resize it to
                # have a maximum width of 300 pixels
                self.frame = self.vs.read()
                self.frame = imutils.resize(self.frame, width=300)

                # OpenCV represents images in BGR order; however PIL
                # represents images in RGB order, so we need to swap
                # the channels, then convert to PIL and ImageTk format
                image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
                image = Image.fromarray(image)
                image = ImageTk.PhotoImage(image)

                # if the panel is not None, we need to initialize it
                if self.panel is None:
                    self.panel = tki.Label(image=image)
                    self.panel.image = image
                    self.panel.pack(side="left", padx=10, pady=10)

                # otherwise, simply update the panel
                else:
                    self.panel.configure(image=image)
                    self.panel.image = image

        except RuntimeError as e:
            print("[INFO] caught a RuntimeError")

    def takeSnapshot(self):
        # grab the current timestamp and use it to construct the
        # output path
        ts = datetime.datetime.now()
        filename = "{}.jpg".format(ts.strftime("%Y-%m-%d_%H-%M-%S"))
        p = os.path.sep.join((Text.get(), filename))

        # save the file
        cv2.imwrite(p, self.frame.copy())
        print("[INFO] saved {}".format(filename))

    def onClose(self):
        # set the stop event, cleanup the camera, and allow the rest of
        # the quit process to continue
        print("[INFO] closing...")
        self.stopEvent.set()
        self.vs.stop()
        self.root.quit()

【讨论】:

  • 感谢您的帮助,此代码不起作用,第一个问题是我没有运行网络摄像头,但它之前工作过,其次当我使用名称时出现以下错误:p = os .path.sep.join((self.outputPath, filename)) NameError: name 'filename' is not defined
猜你喜欢
  • 2023-03-10
  • 2019-02-19
  • 1970-01-01
  • 2012-06-16
  • 2012-12-20
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
相关资源
最近更新 更多