#server.py
#!/usr/bin/python
#Este programa funciona recibiendo comandos
# var : -Indica que se deben preparar las variables para recibir un frame
# blk : -Indica que se empieza la transmision de los macrobloques
# -Ya se tienen contados cuantos macrobloques enviar
# - (3 bytes de cabecera) + (2 bytes de renglon) + (2 bytes de columna) + ( 16x16 pieles = 256 bytes ) = 263 bytes
# shw : -Muestra imagen generada
# eol : -Fin de comunicacion
import cv2
import socket
import numpy
import struct
#la red: se levanta primero el sistema de comunicaciones
socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_server.bind( ("0.0.0.0",9999) )
socket_server.listen(1)
socket_client, (host_client,port_client) = socket_server.accept()
print ("IP cliente ",host_client) # Imprime la IP del host
print ("Puerto TCP ",port_client)
#El visor de imagen
cv2.namedWindow("remoteView")
#Recepcion de comandos
while True:
str=socket_client.recv(263)
# Modo de inicializacion
if str[0:3]=="var":
rows,cols=struct.unpack('HH',str[3:7])
frame=numpy.zeros([rows,cols],dtype=numpy.uint8)
socket_client.send('ack')
# Modo de recepcion de bloques
if str[0:3]=='blk':
(r,c)=struct.unpack('HH',str[3:7])
frame[r:r+16,c:c+16]=numpy.array( struct.unpack(16*16*'B',str[7:]) ).reshape([16,16])
socket_client.send('ack')
# Modo de exhibicion
if str[0:3]=="shw":
cv2.imshow("remoteView",frame)
key=cv2.waitKey(5) # Esta linea es necesaria para mostrar la imagen
socket_client.send('ack')
# Modo de terminacion
if str[0:3]=="eol":
socket_client.send('ack')
break
#Cerrando todo
print("Presione una tecla")
cv2.waitKey(0)
print "Adios"
cv2.destroyWindow("remoteView")
socket_client.close()
socket_server.close()
|