Video Streaming using Python

Python OpenCV

Socket Programming in Python

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

What is a Network Socket?

Types of Internet Sockets:

Client-Server Model in Network Programming

Video Chat App using Python

Server-side Python Code:

server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

server_socket.bind(socket_address)

server_socket.listen(5)
client_socket,addr = server_socket.accept()

client_socket.recv()

client_socket.sendall()

client_socket.close()

# This code is for the server 
# Lets import the libraries
import socket, cv2, pickle,struct,imutils

# Socket Create
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print('HOST IP:',host_ip)
port = 9999
socket_address = (host_ip,port)

# Socket Bind
server_socket.bind(socket_address)

# Socket Listen
server_socket.listen(5)
print("LISTENING AT:",socket_address)

# Socket Accept
while True:
client_socket,addr = server_socket.accept()
print('GOT CONNECTION FROM:',addr)
if client_socket:
vid = cv2.VideoCapture(0)

while(vid.isOpened()):
img,frame = vid.read()
frame = imutils.resize(frame,width=320)
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a
client_socket.sendall(message)

cv2.imshow('TRANSMITTING VIDEO',frame)
if cv2.waitKey(1) == '13':
client_socket.close()

Client-side Python Code:

server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

client_socket.connect((host_ip,port))

packet = client_socket.recv(1024)

client_socket.send(packet)

client_socket.close()

import socket,cv2, pickle,struct

# create socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '192.168.1.20' # paste your server ip address here
port = 9999
client_socket.connect((host_ip,port)) # a tuple
data = b""
payload_size = struct.calcsize("Q")
while True:
while len(data) < payload_size:
packet = client_socket.recv(4*1024) # 4K
if not packet: break
data+=packet
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("Q",packed_msg_size)[0]

while len(data) < msg_size:
data += client_socket.recv(4*1024)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow("RECEIVING VIDEO",frame)
if cv2.waitKey(1) == '13':
break
client_socket.close()

Thank You for reading!! 😇😇

--

--

--

I'm passionate learner diving into the concepts of computing 💻

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

Recommended from Medium

A Bit is a Terrible Thing to Waste

How do I participate in the Ambassador Programme? (eng)

Something different

Kubernetes Cluster on Multi-Cloud using Terraform and Ansible

Azure Functions written in Rust

TryHackMe OWASP Juice Shop | Write-up

Learning how to use Layouts in Kotlin.

MERN: How to deploy your web apps to Digital Ocean

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
Aditya Raj

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻

More from Medium

An Overview on Tenso

Google COLAB local runtime ASAP

How to Build Screen Recorder using python

Extracting Faces from Images Using Opencv Python

A futuristic image showing the facial keypoints