Module cam
[hide private]
[frames] | no frames]

Source Code for Module cam

  1  """ 
  2  [cam.py] 
  3  Modulo que utiliza la cam para captar las fotos del cubo. 
  4  """ 
  5   
  6  __author__ = "Victor Ramirez de la Corte" 
  7  __date__ = "23/04/2009" 
  8  __version__ = "PyRubik v0.6.5" 
  9   
 10  import pygame 
 11  from pygame.locals import * 
 12  import sys, os 
 13  import opencv 
 14  from opencv import highgui  
 15  import colorsys 
 16  from language import * 
 17  import AnimatedSprite 
 18   
 19  camera = highgui.cvCreateCameraCapture(0) 
20 -def get_image():
21 """devuelve la imagen obtenida desde la cam utilizando el modulo opencv""" 22 im = highgui.cvQueryFrame(camera) 23 im = opencv.cvGetMat(im) 24 return opencv.adaptors.Ipl2PIL(im)
25 26
27 -class CapturarDesdeCam:
28 """Clase que utiliza la cam para hacer 2 o 6 fotos del cubo, segun elijas"""
29 - def __init__(self, numMalla = 2):
30 fps = 30.0 31 pygame.init() 32 pygame.display.init() 33 window = pygame.display.set_mode((640,480)) 34 pygame.display.set_caption("PyRubik") 35 screen = pygame.display.get_surface() 36 if numMalla == 2: 37 malla = pygame.image.load("DATOS/malla2.png") 38 rutaCubo = "DATOS/cubo.png" 39 40 #me creo una animacion 41 animacion = AnimatedSprite.AnimatedSprite(rutaCubo, 5, 12, fps = 12) 42 ani = range(12,73,12) 43 animacion.rect.center = (300,100) 44 animacion.set_animation([0]) 45 aniCont = 0 46 47 posicionador = pygame.image.load("DATOS/posicionador.png") 48 cont = 6 49 square = pygame.Rect((0,0), (160,160)) 50 text = lll.press 51 pos = ((0,0),(0,160),(160,160),(320, 160),(480,160),(480,320)) 52 else: 53 malla = pygame.image.load("DATOS/malla1.png") 54 cont = 2 55 text = lll.press[:lll.press.index(" ")] + " 1(XXL), 2(XL), 3(L)" 56 57 font = pygame.font.Font(os.path.join( "FONT", "Times.ttf" ) , 30) 58 cut = 0 59 60 while 1: 61 im = get_image() 62 pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) 63 pg_img = pygame.transform.flip(pg_img, True, False) 64 fuente = font.render(text, True, (255, 255, 0)) 65 screen.blit(pg_img, (0,0)) 66 screen.blit(malla, (0,0)) 67 if numMalla == 2: 68 screen.blit(posicionador, pos[abs(cont-6)]) 69 #uso la animacion #TODO 70 if aniCont < 24: 71 animacion.animate(pygame.time.get_ticks()) 72 screen.blit(animacion.image, animacion.rect) 73 aniCont += 1 74 75 screen.blit(fuente, (300,0)) 76 pygame.display.flip() 77 pygame.time.delay(int(1000 * 1.0/fps)) 78 79 pygame.event.pump() 80 keypress = pygame.key.get_pressed() 81 82 if keypress[K_RETURN] and numMalla == 2: 83 pygame.image.save(pg_img, "capa%d.png" % abs(cont-6)) 84 animacion.set_animation(range(ani[abs(cont-6)]-12, ani[abs(cont-6)])) 85 aniCont = 0 86 cont -=1 87 if cont <= 0: break 88 89 elif (keypress[K_1] or keypress[K_2] or keypress[K_3]) and numMalla != 2: 90 if keypress[K_1]: cut = 442 #diametro del circulo 91 elif keypress[K_2]: cut = 338 92 elif keypress[K_3]: cut = 258 93 print cut 94 rect1 = pygame.Rect((640-(640-cut)/2, 480-(480-cut)/2), (640, 480)) 95 rect2 = pygame.Rect((0, 0), (0+(640-cut)/2, 0+(480-cut)/2)) 96 97 subScreen = pygame.transform.chop(pg_img, rect1) 98 subSubScreen = pygame.transform.chop(subScreen, rect2) 99 subSubScreen = pygame.transform.flip(subSubScreen, True, False) 100 pygame.display.flip() 101 pygame.image.save(pg_img, "capa%d.png" % abs(cont-2)) 102 cont -= 1 103 104 if cont <= 0: 105 break
106