1
2 '''
3
4 import pilas
5 try:
6 import opencv
7 from opencv import highgui
8 except ImportError:
9 opencv = None
10
11 import os
12
13 try:
14 from PySFML import sf
15 except ImportError:
16 pass
17
18 class MissingOpencv(Exception):
19 def __init__(self):
20 self.value = "Open CV no esta instalado, obtengalo en http://opencv.willowgarage.com"
21
22 def __str__(self):
23 return repr(self.value)
24
25 def error(biblioteca, web):
26 print "Error, no ecuentra la biblioteca '%s' (de %s)" %(biblioteca, web)
27
28 def no_opencv():
29 from pilas.utils import esta_en_sesion_interactiva
30 if esta_en_sesion_interactiva():
31 error('opencv', 'http://opencv.willowgarage.com')
32 else:
33 raise MissingOpencv()
34
35 class DeCamara(pilas.actores.Actor):
36 """
37 Nos permite poner en pantalla el video proveniente de la camara web.
38
39 """
40 def __init__(self, ancho=640, alto=480):
41 if opencv is None:
42 no_opencv()
43 return
44 import webcam
45 self.camara = webcam.CamaraWeb
46 self.ultimo_numero_de_cuadro = 0
47 pilas.actores.Actor.__init__(self, 'fondos/pasto.png')
48 pilas.mundo.agregar_tarea_siempre(0.15,self.actualizar_video)
49
50 def actualizar_video(self):
51 cuadro, numero_de_cuadro = self.camara.obtener_imagen(self.ultimo_numero_de_cuadro)
52 self.ultimo_numero_de_cuadro = numero_de_cuadro
53 self.imagen.LoadFromPixels(640, 480, cuadro)
54 return True
55
56 class VideoDeArchivo(object):
57 def __init__(self, ruta):
58 if opencv is None:
59 no_opencv()
60 return
61 if not os.path.isfile(ruta):
62 raise IOError('El archiyo no existe')
63 self._camara = highgui.cvCreateFileCapture(ruta)
64 self.fps = highgui.cvGetCaptureProperty(self._camara, highgui.CV_CAP_PROP_FPS)
65 self.altura = highgui.cvGetCaptureProperty(self._camara, highgui.CV_CAP_PROP_FRAME_HEIGHT)
66 self.ancho =highgui.cvGetCaptureProperty(self._camara, highgui.CV_CAP_PROP_FRAME_WIDTH)
67 super(VideoDeArchivo, self).__init__()
68
69 def obtener_imagen(self):
70 imagen_ipl = highgui.cvQueryFrame(self._camara)
71 imagen_ipl = opencv.cvGetMat(imagen_ipl)
72 return opencv.adaptors.Ipl2PIL(imagen_ipl).convert('RGBA').tostring()
73
74
75 class DePelicula(pilas.actores.Actor):
76 """
77 Nos permite poner en pantalla un video desde un archivo.
78 Toma como parametro la ruta del video.
79 """
80 def __init__(self, path, ancho=640, alto=480):
81 self._camara = VideoDeArchivo(path)
82 pilas.actores.Actor.__init__(self)
83 self._altura_cuadro = self._camara.altura
84 self._ancho_cuadro = self._camara.ancho
85 subrect = self._actor.GetSubRect()
86 subrect.Right = self._ancho_cuadro
87 subrect.Bottom = self._altura_cuadro
88 self._actor.SetSubRect(subrect)
89 self.centro = ('centro', 'centro')
90 pilas.mundo.agregar_tarea_siempre(1/self._camara.fps,self.actualizar_video)
91
92 def actualizar_video(self):
93 self.imagen.LoadFromPixels(self._ancho_cuadro, self._altura_cuadro, self._camara.obtener_imagen())
94 return True
95 '''
96