LIVE VIDEO STREAMING using OPEN-CV and SOCKET_PROGRAMMING

OpenCV

OpenCV-Python

cv2

Syntax: cv2.imread(path, flag)Parameters:
path: A string representing the path of the image to be read.
flag: It specifies the way in which image should be read. It’s default value is cv2.IMREAD_COLOR
Return Value: This method returns an image that is loaded from the specified file.

SENDER.py

import socket,pickle,struct
import cv2
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #TCP connection
print("Enter Your IP")
ip=input()
print("Enter Your port")
port=int(input())
print("Enter Your Partner IP")
pip=input()
print("Enter Your Partner port")
pport=int(input())
s.bind((ip,port))
s.listen(5) #5 is backlog meaning it can accept 5 connections at a time
cap = cv2.VideoCapture(0)
while True:
client_socket,addr = s.accept()
print('GOT CONNECTION FROM:',addr)
if client_socket:
vid = cv2.VideoCapture(0)

while(vid.isOpened()):
img,frame = vid.read()
a = pickle.dumps(frame) #serialize frame to byte data
message = struct.pack("Q",len(a))+a #pack each frame #Q is 8 bytes
client_socket.sendall(message)

cv2.imshow('TRANSMITTING VIDEO',frame)
key = cv2.waitKey(1) & 0xFF
if key ==ord('q'): #press q to exit
client_socket.close()

RECEIVER.py

import socket
import cv2,pickle,struct
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #TCP connection
print("ENter your IP")
ip=input()
print("ENter your PORT")
port=int(input())
s.bind((ip,port))
print("ENter your streamer's IP")
ip1=input()
print("ENter streamer's PORT")
port1=int(input())
s.connect((ip1,port1))
cap=cv2.VideoCapture(0)
data = b""
payload_size = struct.calcsize("Q") #8 bytes
while True:
while len(data) < payload_size:
packet = s.recv(4*1024) #4k buffersize
if not packet: break #if no data, then break
data+=packet #if data comes,append to data until buffer full
packed_msg_size = data[:payload_size] #first 8 bytes have packed msg
data = data[payload_size:] #after initial 8 bytes, frame data present
msg_size = struct.unpack("Q",packed_msg_size)[0] #unpacking packed message and getting

while len(data) < msg_size:
data += s.recv(4*1024)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow("RECEIVING VIDEO",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
s.close()
Syntax: 
struct.pack(format, v1, v2, ...)
Syntax:
struct.pack(format, v1, v2, ...)

OUTPUT

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

GitHub and Git for beginners — Part2

Cloud Development: Does Your Business Need It?

The importance of errors and failing

Mininet installation on VM

Coding for kids: Top buzz in 2020

Lessons Learned from Our First Adventure in Software Engineering

Just Elasticsearch: 5/n. Search Basics

MacOS Calendar.app sync with Exchange365 keeps breaking

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adithya Gangadhar Shetty

Adithya Gangadhar Shetty

More from Medium

Building a Pokémon Card Detector using Mathematica

Create Custom Robot in ROS

Face Recognition

Image classification in the browser with TensorFlow.js, React and GitHub Pages