├── .gitattributes ├── .gitignore ├── Readme.docx ├── client.py ├── server.py ├── videofeed.py └── videosocket.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Readme.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhinav124/Video-Chat-App-Python/dc4f7852879d8e53b0cfc37828daea8b37ead4de/Readme.docx -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import socket, videosocket 3 | import StringIO 4 | from videofeed import VideoFeed 5 | 6 | class Client: 7 | def __init__(self): 8 | self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 | self.client_socket.connect(("10.3.42.55", 6000)) 10 | self.vsock = videosocket.videosocket (self.client_socket) 11 | self.videofeed = VideoFeed(1,"client",1) 12 | self.data=StringIO.StringIO() 13 | 14 | def connect(self): 15 | while True: 16 | frame=self.videofeed.get_frame() 17 | self.vsock.vsend(frame) 18 | frame = self.vsock.vreceive() 19 | self.videofeed.set_frame(frame) 20 | 21 | # print "RECIEVED:" , frame 22 | """if (data <> 'Q' and data <> 'q'): 23 | self.client_socket.send(data) 24 | else: 25 | self.client_socket.send(data) 26 | self.client_socket.close() 27 | break; 28 | """ 29 | 30 | if __name__ == "__main__": 31 | client = Client() 32 | client.connect() 33 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import socket, videosocket 3 | from videofeed import VideoFeed 4 | import time 5 | 6 | class Server: 7 | def __init__(self): 8 | self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 | self.server_socket.bind(("", 6000)) 10 | self.server_socket.listen(5) 11 | self.videofeed = VideoFeed(1,"server",1) 12 | print "TCPServer Waiting for client on port 5000" 13 | 14 | 15 | def start(self): 16 | while 1: 17 | client_socket, address = self.server_socket.accept() 18 | print "I got a connection from ", address 19 | vsock = videosocket.videosocket(client_socket) 20 | while True: 21 | 22 | frame=vsock.vreceive() 23 | self.videofeed.set_frame(frame) 24 | frame=self.videofeed.get_frame() 25 | vsock.vsend(frame) 26 | #print frame 27 | # self.videofeed.set_frame(frame) 28 | 29 | #data = client_socket.recv(921600) 30 | # print "RECIEVED:" , data 31 | 32 | if __name__ == "__main__": 33 | server = Server() 34 | server.start() 35 | -------------------------------------------------------------------------------- /videofeed.py: -------------------------------------------------------------------------------- 1 | import cv 2 | import Image 3 | 4 | class VideoFeed: 5 | 6 | def __init__(self,mode=1,name="w1",capture=1): 7 | print name 8 | if mode == 1: 9 | cv.StartWindowThread() 10 | cv.NamedWindow(name, cv.CV_WINDOW_AUTOSIZE) 11 | self.camera_index = 0 12 | self.name=name 13 | if capture == 1: 14 | self.capture = cv.CaptureFromCAM(self.camera_index) 15 | 16 | def get_frame(self): 17 | self.frame = cv.QueryFrame(self.capture) 18 | self.c = cv.WaitKey(1) 19 | if(self.c=="n"): #in "n" key is pressed while the popup window is in focus 20 | self.camera_index += 1 #try the next camera index 21 | self.capture = cv.CaptureFromCAM(camera_index) 22 | if not self.capture: #if the next camera index didn't work, reset to 0. 23 | self.camera_index = 0 24 | self.capture = cv.CaptureFromCAM(camera_index) 25 | jpegImg= Image.fromstring("RGB",cv.GetSize(self.frame),self.frame.tostring()) 26 | retStr=jpegImg.tostring("jpeg","RGB") 27 | print "Compressed Size = ",len(retStr) 28 | return retStr 29 | 30 | #jpeg.compress(self.frame,640,480,8) 31 | 32 | def set_frame(self, frame): 33 | #im image("RGB",(640,480)) 34 | jpegPIL = Image.fromstring("RGB",(640,480),frame,"jpeg","RGB","raw") 35 | cv_im = cv.CreateImage((640,480), cv.IPL_DEPTH_8U, 3) 36 | cv.SetData(cv_im,jpegPIL.tostring()) 37 | cv.ShowImage(self.name, cv_im) 38 | if __name__=="__main__": 39 | vf = VideoFeed(1,"test",1) 40 | while 1: 41 | m = vf.get_frame() 42 | vf.set_frame(m) 43 | 44 | -------------------------------------------------------------------------------- /videosocket.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | class videosocket: 4 | '''A special type of socket to handle the sending and receiveing of fixed 5 | size frame strings over ususal sockets 6 | Size of a packet or whatever is assumed to be less than 100MB 7 | ''' 8 | 9 | def __init__(self , sock=None): 10 | if sock is None: 11 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 12 | else: 13 | self.sock= sock 14 | 15 | def connect(self,host,port): 16 | self.sock.connect((host,port)) 17 | 18 | def vsend(self, framestring): 19 | totalsent = 0 20 | metasent = 0 21 | length =len(framestring) 22 | lengthstr=str(length).zfill(8) 23 | 24 | while metasent < 8 : 25 | sent = self.sock.send(lengthstr[metasent:]) 26 | if sent == 0: 27 | raise RuntimeError("Socket connection broken") 28 | metasent += sent 29 | 30 | 31 | while totalsent < length : 32 | sent = self.sock.send(framestring[totalsent:]) 33 | if sent == 0: 34 | raise RuntimeError("Socket connection broken") 35 | totalsent += sent 36 | 37 | def vreceive(self): 38 | totrec=0 39 | metarec=0 40 | msgArray = [] 41 | metaArray = [] 42 | while metarec < 8: 43 | chunk = self.sock.recv(8 - metarec) 44 | if chunk == '': 45 | raise RuntimeError("Socket connection broken") 46 | metaArray.append(chunk) 47 | metarec += len(chunk) 48 | lengthstr= ''.join(metaArray) 49 | length=int(lengthstr) 50 | 51 | while totrec