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

Script Kiddie — HackTheBox Write-up (L-E)

Software Developers Always Want to Go Faster

What’s Next? Watching Netflix with Help from Accessibility Services

A Quick Guide to Rails System Tests in RSpec

This Sister Duo from Assam are Taking Food Aesthetics to Another Level Through Their Food Blog…

Dev 1.04 | Sprint 4 [11.26.2020–12.17.2020]

Launching GUI Application in Docker Container

KUBERNETES IN AIR-FORCE DEFENCE SYSTEM..!!

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

Catalog Image Classification— CNN Model

Interacting with Image-Based Element — Appium

Custom object detector with YOLOv5 and Roboflow

Deep Deformable Image Registration