class Image_Send(object): def __init__(self): self.camara = Webcam() self.tcp = Comunicacion() self.host = "127.0.0.1" self.reintentos = 10 # cantidad de reintentos de conexion de la camara self.th_cam = ThreadAdmin() # Thread para la camara self.tiempo = Timer() # Control de fps self.log = self.__log_default # Para configurar log def config(self, host, port): self.host = host self.camara.config(Ancho=200, Alto=150) self.tcp.config(host, puerto=port, cliente=False, binario=True, callback=self.__call_conexion) def config_log(self, Log): '''posibilidad de configurar clase Log(Texto, Modulo)''' self.log = Log.log self.camara.config_log(Log) # enviamos el log configurado a la camara tambien def iniciar(self): self.camara.start() if self.camara.activo: # CONEXION EXITOSA self.log("CAMARA CONECTADA","IMAGE-SEND") self.tcp.iniciar() else: self.log("CAMARA SIN CONEXION", "IMAGE-SEND") # Recepcion de datos de la conexion def __call_conexion(self, codigo, mensaje): if codigo == 2: # Conexion self.th_cam.start(self.__th_camara, '', 'CAMARA ENVIO', callback=self.__log_th, enviar_ejecucion=True) elif codigo == 4: # Recepcion de datos self.log("RECEPCION INCORRECTA - COD: " + str(codigo) + " Men: " + str(mensaje), "IMAGE-SEND") elif codigo != 3: # 3 es envio de datos self.log("COD: " + str(codigo) + " Men: " + str(mensaje), "IMAGE-SEND") def __th_camara(self, run): self.tiempo.iniciar() while self.tcp.conexion and run.value: frame = self.camara.read() # Lectura de cuadro de camara self.tcp.enviar(frame) # Enviar cuadro # print(self.tiempo.fps()) # ver los fps que enviamos (solo envia 5 cuadros la raspberry) self.tiempo.delay(6) # Log por defecto def __log_default(self, Texto, Modulo): print(Texto) # Log que devuelve el thread def __log_th(self, Codigo, Mensaje): self.log(Mensaje, "IMAGE-SEND")
class Camara(object): def __init__(self): self.resolucion = (320, 240) self.frames = 10 self.windows = False self.visible = False self.pantalla = '' self.camara = '' self.captura = '' self.captura_final = '' self.numero_imagen = 0 self.pausa = 100 self.loop = True self.log = self.log_default self.visible = False self.conectado = False self.timer = Timer() def config(self, Resolucion=(320, 240), Frames=10, Windows=False, Visible=False): self.resolucion = Resolucion self.frames = Frames self.windows = Windows self.visible = Visible def config_log(self, Log): #posibilidad de configurar clase Log(Texto, Modulo) self.log = Log.log def iniciar(self): self.__inicializar() self.__loop() def __inicializar(self): self.log("Iniciado Camara", "CAMARA") #INICIALIZAR CAMARA pygame.init() pygame.camera.init() #Modo Windows o Linux if self.windows: self.log("Usando Windows", "CAMARA") cameras = pygame.camera.list_cameras() self.log("Usando camera %s ..." % cameras[0], "VIDEO") #self.camara = pygame.camera.Camera(cameras[0],(640,480),"RGBA") self.camara = pygame.camera.Camera(cameras[0], self.resolucion, "RGBA") else: self.log("Usando Linux", "VIDEO") #self.camara = pygame.camera.Camera("/dev/video0", (640,480),"RGBA") self.camara = pygame.camera.Camera("/dev/video0", self.resolucion, "RGBA") #Iniciamos la camara self.camara.start() #mostramos en pantalla (si es necesario) if self.windows or self.visible: self.pantalla = pygame.display.set_mode(self.resolucion, 0) #self.captura = pygame.surface.Surface((640,480), 0, self.pantalla) self.captura = pygame.surface.Surface(self.resolucion, 0, self.pantalla) def __loop(self): self.log("Iniciado el loop de captura", "CAMARA") numero_imagen = 0 self.timer.iniciar() while self.loop: self.__Capturar() self.__Almacenar() self.__Visualizar() self.__Interrupcion() delay = self.timer.delay(self.frames) pygame.time.delay(delay) tiempo = self.timer.fps() self.timer.iniciar() #print ("fps", tiempo) def __Capturar(self): # Si no esta conectado o no es visible no tiene sentido capturar if (self.conectado) or (self.visible): if self.windows: self.captura = self.camara.get_image(self.captura) else: # En linux podemos capturar la imagen sin un visualizador self.captura = self.camara.get_image() pass # Corregir en futuro si va a ser necesario la transformacion self.captura_final = pygame.transform.scale( self.captura, self.resolucion) def __Almacenar(self): # Grabamos imagenes si hay conexion if self.conectado == 1: nombre_imagen = "imagenes_tmp/img_tmp" + str( self.numero_imagen) + ".jpg" pygame.image.save(self.captura_final, nombre_imagen) #imagen guardada (incrementamos el contador) self.numero_imagen += 1 if self.numero_imagen == 10: self.numero_imagen = 0 def __Visualizar(self): # Mostramos imagen si esta visible if self.visible: self.pantalla.blit(self.captura_final, (0, 0)) pygame.display.update() def __Interrupcion(self): # Interupcion if self.windows or self.visible: for event in pygame.event.get(): if event.type == pygame.QUIT: self.camara.stop() pygame.quit() self.loop = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.camara.stop() pygame.quit() self.loop = False def log_default(self, Texto, Modulo): print(Texto)
# Esto es para poder hacer el from desde un nivel atras y funciona con launch.json import os, sys BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_PATH) import time from componentes.timer import Timer tiempo = Timer() tiempo.iniciar() while True: #time.sleep(0.01) print("FPS: ", tiempo.fps()) tiempo.delay(10)