qrreader.py 4.25 KB
Newer Older
Lars Beckers's avatar
Lars Beckers committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys, os, re, subprocess, string
from threading import Timer
import pygtk, gtk
import gst
import zbar, Image

#
#	required dependencies:
#		zbar (http://pypi.python.org/pypi/zbar)
#		Python Imaging Library (http://www.pythonware.com/products/pil/index.htm)
#		gstreamer python bindings (http://gstreamer.freedesktop.org/modules/gst-python.html)
#		firefox (http://www.mozilla.org/en-US/firefox/fx/#desktop)
#

class GTK_Main:

	def __init__(self):
		self.clean()
		window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		window.set_title("Webcam-Viewer")
		window.set_default_size(500, 400)
		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("Quit")
		self.button.connect("clicked", self.exit)
		hbox.pack_start(self.button, False)
		self.button_restart = gtk.Button('Restart')
		self.button_restart.connect('clicked',self.restart)
		hbox.pack_start(self.button_restart,True)
		hbox.add(gtk.Label())

		window.show_all()

		# Set up the gstreamer pipepile
		self.player = gst.parse_launch ("""v4l2src ! 
tee name=t ! queue ! autovideosink t.
! queue ! videorate ! video/x-raw-yuv, width=800, height=600, framerate=1/1
! jpegenc ! multifilesink location=snapshot-%03d.jpg""")

		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)
		
		self.player.set_state(gst.STATE_PLAYING)
		self.scan()

	def exit(self, widget, data=None):
		self.t.cancel()
		self.clean()
		gtk.main_quit()
	
	def restart(self, widget, data=None):
		self.clean()
		self.scan()

	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)
			
	def scan(self):
		files = os.listdir(".")
		files = filter(lambda x: re.search('^snapshot-[0-9]*.jpg$', x) != None, files)
	
		for f in files:
			print("processing image: "+str(f))
			self.process_qr(str(f))

			os.remove(f)
		self.t = Timer(3.0, self.scan)
		self.t.start()

	def process_qr(self, picture):
		scanner = zbar.ImageScanner()
		scanner.parse_config('enable')
		pil = Image.open(picture).convert('L')
		width,height = pil.size
		raw = pil.tostring()
		image = zbar.Image(width,height,'Y800',raw)
		scanner.scan(image)
		no_qr = True
		result = ''
		for symbol in image:
			qr = symbol.data
			print(qr)
			if qr.find('http://fsmpi.rwth-aachen.de/lip/view/') >= 0:
				os.system('firefox -new-tab '+qr)
			elif qr.find('pk: ') >= 0:
				pk = string.split(string.split(qr,'%')[0],': ')[1]
				os.system('firefox -new-tab http://fsmpi.rwth-aachen.de/lip/view/' + pk + '+0')
			else:
				self.invalid_code()
			no_qr = False
		del(image)
		if no_qr:
			print('\033[1;31mno QR code found\033[1;m')
		else:
			exit()

	def stop(self):
		exit()
	
	def clean(self):
		files = os.listdir(".")
		files = filter(lambda x: re.search('^snapshot-[0-9]*.jpg$', x) != None, files)
		for f in files:
			os.remove(f)

	def invalid_code(self):
		print('\033[1;41m  _  _  _  _ _  ___  _    _  ___    ___  ___     ___  ___  ___  ___  \033[1;m')
		print('\033[1;41m | || \| || | |/ - \| |  | ||   \  /   \| - |   /  _|/   \|   \| __| \033[1;m')
		print('\033[1;41m | ||  \ || | || | || |_ | || | |  | | ||   \  |  (_ | | || | || __| \033[1;m')
		print('\033[1;41m |_||_\__| \_/ |_|_||___||_||___/  \__\_|_|\_\  \___|\___/|___/|___| \033[1;m')
		print('\033[1;41m                                                                     \033[1;m')

g = GTK_Main()
gtk.gdk.threads_init()
gtk.main()