使用 python (pygtk) 从网络摄像头监视器中获取快照

发布于 2024-12-03 03:36:21 字数 3125 浏览 0 评论 0原文

我有这段代码用于使用 python 和 pygtk 监控网络摄像头。 问题是,我如何用这段代码拍摄快照?:

#!/usr/bin/env python

import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
import time

class WebCam:
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Webcam-Viewer")
        window.set_default_size(1024, 768)
        window.connect("destroy", gtk.main_quit, "WM destroy")
        vbox = gtk.VBox()
        window.add(vbox)
        self.movie_window = gtk.DrawingArea()
        vbox.add(self.movie_window)
        hbox = gtk.HBox()
        vbox.pack_start(hbox, False)
        hbox.set_border_width(10)
        hbox.pack_start(gtk.Label())
        self.button = gtk.Button("Start")
        self.button.connect("clicked", self.start_stop)
        hbox.pack_start(self.button, False)

        self.button3 = gtk.Button("SnapShot")
        self.button3.connect("clicked", self.take_snapshot)
        hbox.pack_start(self.button3, False)

        self.button2 = gtk.Button("Quit")
        self.button2.connect("clicked", self.exit)
        hbox.pack_start(self.button2, False)
        hbox.add(gtk.Label())

        # Set up the gstreamer pipeline
        self.player = gst.parse_launch ("v4l2src ! autovideosink")

        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        bus.connect("message", self.on_message)
        bus.connect("sync-message::element", self.on_sync_message)

        window.set_border_width(3)
        window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        window.show_all()

    def take_snapshot(self,widget):
        filename = str(time.time()) + ".jpg"     
        #self.movie_window.window.get_image(0, 0, 500, 400)
        print "Snapshot"

    def start_stop(self, w):
        if self.button.get_label() == "Start":
            self.button.set_label("Stop")
            self.player.set_state(gst.STATE_PLAYING)
        else:
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")

    def exit(self, widget, data=None):
        gtk.main_quit()

    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")
        elif t == gst.MESSAGE_ERROR:
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")

    def on_sync_message(self, bus, message):
        if message.structure is None:
            return
        message_name = message.structure.get_name()
        if message_name == "prepare-xwindow-id":
            # Assign the viewport
            imagesink = message.src
            imagesink.set_property("force-aspect-ratio", True)
            imagesink.set_xwindow_id(self.movie_window.window.xid)

if __name__ == "__main__":
    try:
        a = WebCam()
        gtk.gdk.threads_init()
        gtk.main()
    except KeyboardInterrupt:
        pass    

谢谢大家

I have this code for monitoring a webcam with python and pygtk.
The question is, how I take a snapshot with this code?:

#!/usr/bin/env python

import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
import time

class WebCam:
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Webcam-Viewer")
        window.set_default_size(1024, 768)
        window.connect("destroy", gtk.main_quit, "WM destroy")
        vbox = gtk.VBox()
        window.add(vbox)
        self.movie_window = gtk.DrawingArea()
        vbox.add(self.movie_window)
        hbox = gtk.HBox()
        vbox.pack_start(hbox, False)
        hbox.set_border_width(10)
        hbox.pack_start(gtk.Label())
        self.button = gtk.Button("Start")
        self.button.connect("clicked", self.start_stop)
        hbox.pack_start(self.button, False)

        self.button3 = gtk.Button("SnapShot")
        self.button3.connect("clicked", self.take_snapshot)
        hbox.pack_start(self.button3, False)

        self.button2 = gtk.Button("Quit")
        self.button2.connect("clicked", self.exit)
        hbox.pack_start(self.button2, False)
        hbox.add(gtk.Label())

        # Set up the gstreamer pipeline
        self.player = gst.parse_launch ("v4l2src ! autovideosink")

        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        bus.connect("message", self.on_message)
        bus.connect("sync-message::element", self.on_sync_message)

        window.set_border_width(3)
        window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        window.show_all()

    def take_snapshot(self,widget):
        filename = str(time.time()) + ".jpg"     
        #self.movie_window.window.get_image(0, 0, 500, 400)
        print "Snapshot"

    def start_stop(self, w):
        if self.button.get_label() == "Start":
            self.button.set_label("Stop")
            self.player.set_state(gst.STATE_PLAYING)
        else:
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")

    def exit(self, widget, data=None):
        gtk.main_quit()

    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")
        elif t == gst.MESSAGE_ERROR:
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")

    def on_sync_message(self, bus, message):
        if message.structure is None:
            return
        message_name = message.structure.get_name()
        if message_name == "prepare-xwindow-id":
            # Assign the viewport
            imagesink = message.src
            imagesink.set_property("force-aspect-ratio", True)
            imagesink.set_xwindow_id(self.movie_window.window.xid)

if __name__ == "__main__":
    try:
        a = WebCam()
        gtk.gdk.threads_init()
        gtk.main()
    except KeyboardInterrupt:
        pass    

Thanks for all

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

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

发布评论

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

评论(1

╰つ倒转 2024-12-10 03:36:21

我错过了问题的重点。有一个 take_snapshot 函数,其中实际拍摄快照的位已被注释掉。修改您的代码,如下所示

def take_snapshot(self,widget):
  filename = str(time.time()) + ".jpg"     
  pixbuf = gtk.gdk.Pixbuf.get_from_drawable(self.movie_window.window, self.movie_window.window.get_colormap(), 0, 0, 0,0 500, 400)
  pixbuf.save(filename, "jpeg", {"quality":"100"})

,应拍摄快照并使用当前时间保存图像

I'm missing the point of the question. There's a take_snapshot function with the bit that actually takes the snapshot commented out. Modify your code to look like the following

def take_snapshot(self,widget):
  filename = str(time.time()) + ".jpg"     
  pixbuf = gtk.gdk.Pixbuf.get_from_drawable(self.movie_window.window, self.movie_window.window.get_colormap(), 0, 0, 0,0 500, 400)
  pixbuf.save(filename, "jpeg", {"quality":"100"})

that should take a snapshot and save the image with the current time

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