J'ai ce code pour surveiller une webcam avec python et pygtk. La question est, comment prendre un instantané avec ce 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("Visionneuse de webcam")
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("Démarrer")
self.button.connect("clicked", self.start_stop)
hbox.pack_start(self.button, False)
self.button3 = gtk.Button("Instantané")
self.button3.connect("clicked", self.take_snapshot)
hbox.pack_start(self.button3, False)
self.button2 = gtk.Button("Quitter")
self.button2.connect("clicked", self.exit)
hbox.pack_start(self.button2, False)
hbox.add(gtk.Label())
# Mettre en place le pipeline gstreamer
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):
nom_fichier = str(time.time()) + ".jpg"
#self.movie_window.window.get_image(0, 0, 500, 400)
print "Instantané"
def start_stop(self, w):
if self.button.get_label() == "Démarrer":
self.button.set_label("Arrêter")
self.player.set_state(gst.STATE_PLAYING)
else:
self.player.set_state(gst.STATE_NULL)
self.button.set_label("Démarrer")
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("Démarrer")
elif t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
print "Erreur: %s" % err, debug
self.player.set_state(gst.STATE_NULL)
self.button.set_label("Démarrer")
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":
# Attribution du 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
Merci pour tout