Skip to content

Commit

Permalink
initializing
Browse files Browse the repository at this point in the history
  • Loading branch information
Anil Shanbhag committed Apr 1, 2012
0 parents commit 5469da5
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
Empty file added README.markdown
Empty file.
32 changes: 32 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/python
import socket, videosocket
import StringIO
from videofeed import VideoFeed

class Client:
def __init__(self):
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect(("10.3.42.55", 6000))
self.vsock = videosocket.videosocket (self.client_socket)
self.videofeed = VideoFeed(1,"client",1)
self.data=StringIO.StringIO()

def connect(self):
while True:
frame=self.videofeed.get_frame()
self.vsock.vsend(frame)
frame = self.vsock.vreceive()
self.videofeed.set_frame(frame)

# print "RECIEVED:" , frame
"""if (data <> 'Q' and data <> 'q'):
self.client_socket.send(data)
else:
self.client_socket.send(data)
self.client_socket.close()
break;
"""

if __name__ == "__main__":
client = Client()
client.connect()
34 changes: 34 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python
import socket, videosocket
from videofeed import VideoFeed
import time

class Server:
def __init__(self):
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind(("", 6000))
self.server_socket.listen(5)
self.videofeed = VideoFeed(1,"server",1)
print "TCPServer Waiting for client on port 5000"


def start(self):
while 1:
client_socket, address = self.server_socket.accept()
print "I got a connection from ", address
vsock = videosocket.videosocket(client_socket)
while True:

frame=vsock.vreceive()
self.videofeed.set_frame(frame)
frame=self.videofeed.get_frame()
vsock.vsend(frame)
#print frame
# self.videofeed.set_frame(frame)

#data = client_socket.recv(921600)
# print "RECIEVED:" , data

if __name__ == "__main__":
server = Server()
server.start()
43 changes: 43 additions & 0 deletions videofeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import cv
import Image

class VideoFeed:

def __init__(self,mode=1,name="w1",capture=1):
print name
if mode == 1:
cv.StartWindowThread()
cv.NamedWindow(name, cv.CV_WINDOW_AUTOSIZE)
self.camera_index = 0
self.name=name
if capture == 1:
self.capture = cv.CaptureFromCAM(self.camera_index)

def get_frame(self):
self.frame = cv.QueryFrame(self.capture)
self.c = cv.WaitKey(1)
if(self.c=="n"): #in "n" key is pressed while the popup window is in focus
self.camera_index += 1 #try the next camera index
self.capture = cv.CaptureFromCAM(camera_index)
if not self.capture: #if the next camera index didn't work, reset to 0.
self.camera_index = 0
self.capture = cv.CaptureFromCAM(camera_index)
jpegImg= Image.fromstring("RGB",cv.GetSize(self.frame),self.frame.tostring())
retStr=jpegImg.tostring("jpeg","RGB")
print "Compressed Size = ",len(retStr)
return retStr

#jpeg.compress(self.frame,640,480,8)

def set_frame(self, frame):
#im image("RGB",(640,480))
jpegPIL = Image.fromstring("RGB",(640,480),frame,"jpeg","RGB","raw")
cv_im = cv.CreateImage((640,480), cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im,jpegPIL.tostring())
cv.ShowImage(self.name, cv_im)
if __name__=="__main__":
vf = VideoFeed(1,"test",1)
while 1:
m = vf.get_frame()
vf.set_frame(m)

62 changes: 62 additions & 0 deletions videosocket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import socket

class videosocket:
'''A special type of socket to handle the sending and receiveing of fixed
size frame strings over ususal sockets
Size of a packet or whatever is assumed to be less than 100MB
'''

def __init__(self , sock=None):
if sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock= sock

def connect(self,host,port):
self.sock.connect((host,port))

def vsend(self, framestring):
totalsent = 0
metasent = 0
length =len(framestring)
lengthstr=str(length).zfill(8)

while metasent < 8 :
sent = self.sock.send(lengthstr[metasent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
metasent += sent


while totalsent < length :
sent = self.sock.send(framestring[totalsent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
totalsent += sent

def vreceive(self):
totrec=0
metarec=0
msgArray = []
metaArray = []
while metarec < 8:
chunk = self.sock.recv(8 - metarec)
if chunk == '':
raise RuntimeError("Socket connection broken")
metaArray.append(chunk)
metarec += len(chunk)
lengthstr= ''.join(metaArray)
length=int(lengthstr)

while totrec<length :
chunk = self.sock.recv(length - totrec)
if chunk == '':
raise RuntimeError("Socket connection broken")
msgArray.append(chunk)
totrec += len(chunk)
return ''.join(msgArray)





0 comments on commit 5469da5

Please sign in to comment.