I have streaming video UDP, but the packets are delayed and disordered.
Here is my code for reference which I have taken from opencv website.
But when I give Socket.Stream to use TCP connection it streams fine and the frames are continuous.
Server.py
import socket
import numpy
import time
import cv2
UDP_IP = "127.0.0.1"
UDP_PORT = 999
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
s=b''
while True:
data, addr = sock.recvfrom(46080)
s += data
if len(s) == (46080*20):
frame = numpy.fromstring (s,dtype=numpy.uint8)
frame = frame.reshape (480,640,3)
cv2.imshow('frame',frame)
s=b''
if cv2.waitKey(1) & 0xFF == ord ('q'):
break
Client.py
import socket
import numpy as np
import cv2
UDP_IP = '127.0.0.1'
UDP_PORT = 999
cap = cv2.VideoCapture(0)
#cap.set(cv2.CAP_PROP_FRAME_WIDTH,320)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT,240)
def xrange(x):
return iter(range(x))
while (True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
d = frame.flatten()
s = d.tostring()
for i in xrange(20):
sock.sendto(s[i * 46080:(i + 1) * 46080], (UDP_IP, UDP_PORT))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It looks like the client.py script is sending out UDP frames (much) faster than the server.py is piecing them together. I am not sure why this is the case. Perhaps after increasing the size of byte s after every iteration, Python needs to find a new place to store s, which causes it to miss frames sent from client.py. However, one way to sync them is to tell the client.py to send after server.py acknowledges receipt of the frame.
In the below code, server.py will listen for a start-of-image transmission via b'eframe'. server.py would the consecutively reply client.py with b'n' to cue sending of the next frame. client.py would notify the end-of-image by sending b'eframe'
server.py
import socket
import numpy
import time
import cv2
UDP_IP = "127.0.0.1"
UDP_PORT = 999
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
s=b''
while True:
data, addr = sock.recvfrom(6) # b'eframe' is 6 byte long
if data == b'sframe':
while True:
sock.sendto(b'n', addr)
data, addr = sock.recvfrom(46080)
if data == b'eframe':
break
s += data
if len(s) == (46080*20):
frame = numpy.fromstring (s,dtype=numpy.uint8)
frame = frame.reshape (480,640,3)
cv2.imshow('frame',frame)
s=b''
if cv2.waitKey(1) & 0xFF == ord ('q'):
break
client.py
import socket
import numpy as np
import cv2
UDP_IP = '127.0.0.1'
UDP_PORT = 999
cap = cv2.VideoCapture(0)
#cap.set(cv2.CAP_PROP_FRAME_WIDTH,320)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT,240)
def xrange(x):
return iter(range(x))
while (True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
d = frame.flatten()
s = d.tostring()
sock.sendto(b'sframe', (UDP_IP, UDP_PORT))
for i in xrange(20):
data, addr = sock.recvfrom(1) # b'n' is 1 byte long
if data == b'n':
sock.sendto(s[i * 46080:(i + 1) * 46080], (UDP_IP, UDP_PORT))
data, addr = sock.recvfrom(1)
sock.sendto(b'eframe', (UDP_IP, UDP_PORT))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Related
I am a newbie at python socket programming. I am trying to send video frames and two lists over the socket. But the problem is only one of the items is sent and the other one gets blocked. How can I send these two types of data simultaneously? Thank you for your help.
Server-side code:
import socket
import cv2, pickle,struct
import time
capture = cv2.VideoCapture(0)
port=5050
host=socket.gethostbyname(socket.gethostname())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(5)
while True:
# now our endpoint knows about the OTHER endpoint.
conn,add = s.accept()
print(f"Connection from {add} has been established.")
msg=conn.recv(1024)
print(msg)
if conn:
vid = cv2.VideoCapture(0)
X=[10,20,30]
Y=[]
for i in range(len(X)):
y=X[i]*10
Y.append(y)
print(Y)
X_data=pickle.dumps(X)
Y_data=pickle.dumps(Y)
conn.send(X_data)
conn.send(Y_data)
while(vid.isOpened()):
img,frame = vid.read()
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a
conn.sendall(message)
#cv2.imshow('TRANSMITTING VIDEO',frame)
key = cv2.waitKey(1) & 0xFF
if key ==ord('q'):
conn.close()
Client side Code: I haven't tried to use the lists in the client code. But if I send the lists first then the video portion does not work
import socket,pickle,struct
import numpy as np
import cv2
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port=5050
host=socket.gethostbyname(socket.gethostname())
s.connect((host,port))
data=b""
payload_size = struct.calcsize("Q")
while True:
msg=(bytes('Khan',"utf-8"))
s.sendall(msg)
while len(data) < payload_size:
packet =s.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 += 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()
In my experience, you need to send one item and then clear/flush the socket buffer on the server-side before you send the next item.
Do this in a while loop and you can do it continuously forever.
I did it like this:
def clear_buffer(sock):
try:
while sock.recv(1024): pass
except:
pass
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0",3389))
s.listen(10)
c,a = s.accept()
c.settimeout(1.0)
clear_buffer(c)
i'm trying to write a python script that streams video to browser using web socket. I'm using opencv as client to send frames via socket and browser script receive and display it on a browser. single image are displayed in browser but issue occurs while streaming video not able to display it on browser. python flask works fine but there are few issues so i have planned use web socket for browser display
client code sends frame to server using opencv and socket
import cv2
import numpy as np
import socket
import sys
import pickle
import struct ### new code
cap=cv2.VideoCapture("test.avi")
clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('0.0.0.0',8082))
while True:
ret,frame=cap.read()
data = pickle.dumps(frame) ### new code
clientsocket.sendall(struct.pack("L", len(data))+data)
browser code
import socket #for sockets handling
import time #for time functions
import sys
import cv2
import pickle
import numpy as np
import struct ##
hostIP = '127.0.0.1'
SourcePort = 8082 #client socket
PlayerPort = 8081 #Internet Browser
def gen_headers():
# determine response code
h = ''
h = 'HTTP/1.1 200 OK\n'
# write further headers
current_date = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
h += 'Date: ' + current_date +'\n'
h += 'Content-Type: image/jpeg\n\n'
return h
def start_server():
socketFFMPEG = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this is for easy starting/killing the app
socketFFMPEG.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print('Socket created')
### new
data = b""
payload_size = struct.calcsize("L")
try:
socketFFMPEG.bind((hostIP, SourcePort))
print('Socket bind complete')
except socket.error as msg:
print('Bind failed. Error : ' + str(sys.exc_info()))
sys.exit()
#Start listening on socketFFMPEG
socketFFMPEG.listen(10)
print('Socket now listening. Waiting for video source from client socket on port', SourcePort)
conn, addr = socketFFMPEG.accept()
ip, port = str(addr[0]), str(addr[1])
print('Accepting connection from ' + ip + ':' + port)
socketPlayer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketPlayer.bind((hostIP, PlayerPort))
socketPlayer.listen(1) #listen just 1 petition
print('Waiting for Internet Browser')
conn2, addr2 = socketPlayer.accept()
#conn2.sendall(gen_headers().encode())
while True:
try :
while len(data) < payload_size:
data += conn.recv(4096)
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
###
frame=pickle.loads(frame_data)
#send data to internet browser
print(frame)
ret, frame = cv2.imencode('.jpg', frame)
frames = frame.tobytes()
conn2.sendall( gen_headers().encode()+frames)
except socket.error:
print('Error data :' + str(frame))
print('send Error : ' + str(sys.exc_info()))
conn2.close()
sys.exit()
socketFFMPEG.close()
start_server()
Server.py
# This is server code to send video frames over UDP
import cv2, imutils, socket
import numpy as np
import time
import base64
BUFF_SIZE = 65536
server_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,BUFF_SIZE)
host_name = socket.gethostname()
host_ip = '192.168.1.102'# socket.gethostbyname(host_name)
print(host_ip)
port = 9999
socket_address = (host_ip,port)
server_socket.bind(socket_address)
print('Listening at:',socket_address)
vid = cv2.VideoCapture(0) # replace 'rocket.mp4' with 0 for webcam
fps,st,frames_to_count,cnt = (0,0,20,0)
while True:
msg,client_addr = server_socket.recvfrom(BUFF_SIZE)
print('GOT connection from ',client_addr)
WIDTH=400
while(vid.isOpened()):
_,frame = vid.read()
frame = imutils.resize(frame,width=WIDTH)
encoded,buffer = cv2.imencode('.jpg',frame,[cv2.IMWRITE_JPEG_QUALITY,80])
message = base64.b64encode(buffer)
server_socket.sendto(message,client_addr)
frame = cv2.putText(frame,'FPS: '+str(fps),(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
cv2.imshow('TRANSMITTING VIDEO',frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
server_socket.close()
break
if cnt == frames_to_count:
try:
fps = round(frames_to_count/(time.time()-st))
st=time.time()
cnt=0
except:
pass
cnt+=1
Client.py
# This is client code to receive video frames over UDP
import cv2, imutils, socket
import numpy as np
import time
import base64
BUFF_SIZE = 65536
client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
client_socket.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,BUFF_SIZE)
host_name = socket.gethostname()
host_ip = '192.168.1.102'# socket.gethostbyname(host_name)
print(host_ip)
port = 9999
message = b'Hello'
client_socket.sendto(message,(host_ip,port))
fps,st,frames_to_count,cnt = (0,0,20,0)
while True:
packet,_ = client_socket.recvfrom(BUFF_SIZE)
data = base64.b64decode(packet,' /')
npdata = np.fromstring(data,dtype=np.uint8)
frame = cv2.imdecode(npdata,1)
frame = cv2.putText(frame,'FPS: '+str(fps),(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
cv2.imshow("RECEIVING VIDEO",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
client_socket.close()
break
if cnt == frames_to_count:
try:
fps = round(frames_to_count/(time.time()-st))
st=time.time()
cnt=0
except:
pass
cnt+=1
I created a server and a client in python so I could send an image across two sockets but when I run the client it receives the data but once it reaches the end of the file it gets stuck. It raises no error and doesn't crash the terminal is just there stuck.
I have tried changing the code a bit to no avail. I am still a beginner.
client.py
import socket
import cv2
import numpy as np
import pickle
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_socket.bind(('127.0.0.1',5000))
host_ip = ('127.0.0.1',400)
client_socket.connect(host_ip)
serialized_img = b""
while True:
packet = client_socket.recv(1024)
if not packet :
break
serialized_img += packet
image = pickle.loads(serialized_img)
cv2.imshow("a",image)
server.py
import socket
import cv2
import numpy as np
import pickle
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1',400))
cap = cv2.VideoCapture(0)
ret,img = cap.read()
cv2.imshow('image',img)
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
serialized_img = pickle.dumps(img)
print(serialized_img)
while ret:
try:
server_socket.listen()
client_socket,client_address = server_socket.accept()
print(client_address)
client_socket.sendall(serialized_img)
except socket.timeout :
print("time out")
server_socket.close()
I want the client side to be able to show the image.
Close client_socket in server to inform client that it end of data.
client_socket.sendall(serialized_img)
client_socket.close()
In client you have to wait for key to keep window opened.
cv2.imshow("a", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Server:
import socket
import cv2
import pickle
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('127.0.0.1', 4000))
cap = cv2.VideoCapture(0)
ret, img = cap.read()
cap.release()
cv2.imshow("server", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
serialized_img = pickle.dumps(img)
while ret:
try:
server_socket.listen()
client_socket,client_address = server_socket.accept()
print(client_address)
client_socket.sendall(serialized_img)
client_socket.close()
print('closed')
except socket.timeout :
print("time out")
server_socket.close()
Client:
import socket
import cv2
import pickle
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 4000))
serialized_img = b""
while True:
packet = client_socket.recv(1024)
if not packet :
break
serialized_img += packet
image = pickle.loads(serialized_img)
cv2.imshow("client", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
If you want to send live video then server would have to run separate thread with camera or with while ret. And every client_socket run in separate thread in while True loop. Problem is how to inform client where is end of one frame and beginning of next frame. You couldn't use close() for this.
EDIT: this code streams images from camera so client can see on live - with small delay.
It sends image's size before image so client know how many bytes to receive to get full image. Serialized integer has always 8 bytes so I always receive 8 bytes before image.
I use cv2.waitKey(10) in client to check button not only to close window but it didn't display image without this. Maybe window has to receive events from system to work correctly (and refresh window) like in others modules - ie. PyGame - and waitKey() is checking events.
Server:
import socket
import cv2
import pickle
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('127.0.0.1', 4000))
cap = cv2.VideoCapture(0)
while True:
try:
server_socket.listen()
print('waiting ...')
client_socket,client_address = server_socket.accept()
print(client_address)
while True:
try:
ret, img = cap.read()
serialized_img = pickle.dumps(img)
print('serialized_len:', len(serialized_img))
serialized_len = pickle.dumps(len(serialized_img))
#print('len(serialized_len):', len(serialized_len)) # always length 8
client_socket.sendall(serialized_len) # always length 8
client_socket.sendall(serialized_img)
except Exception as ex:
print(ex)
# exit loop when errro, ie. when client close connection
break
client_socket.close()
print('closed')
except socket.timeout:
print('time out')
cap.release()
server_socket.close()
Client:
import socket
import cv2
import pickle
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 4000))
cv2.namedWindow('client')
while True:
serialized_image = b""
serialized_len = client_socket.recv(8) # always length 8
length = pickle.loads(serialized_len)
#print('length:', length)
while length > 0:
if length < 1024:
packet = client_socket.recv(length)
else:
packet = client_socket.recv(1024)
if not packet:
print('error: no data')
break
serialized_image += packet
length -= len(packet)
#print('received:', len(serialized_image))
image = pickle.loads(serialized_image)
cv2.imshow('client', image)
# it need it to display image (maybe it has to receive events from system)
# it `waitKey` waits 10ms so it doesn't block loop
key = cv2.waitKey(10) & 0XFF
if key == 27:
break
cv2.destroyAllWindows()
I am trying to create a simple application to send live stream video over the socket in Python 3 with OpenCV. I am new to OpenCV and socket programming so if you can provide answer in detail I will be very grateful. Thank you.
Here is sender.py
import socket
import time
import cv2
capture = cv2.VideoCapture(0)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.1.10', 50505))
while True:
ret, frame = capture.read()
data = cv2.imencode('.jpg', frame)[1].tostring()
sock.sendall(data)
time.sleep(2)
Here is receiver.py
import socket
import cv2
import numpy as np
import time
HOST = '192.168.1.10'
PORT = 50505
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
s.bind((HOST, PORT))
print('Socket bind complete')
s.listen(10)
print('Socket now listening')
conn, addr = s.accept()
while True:
data = conn.recv(8192)
nparr = np.fromstring(data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow('frame', frame)
time.sleep(2)
and this is the error
receiver.py", line 29, in <module>
cv2.imshow('frame', frame)
cv2.error: D:\Build\OpenCV\opencv-3.4.0\modules\highgui\src\window.cpp:339:
error: (-215) size.width>0 && size.height>0 in function cv::imshow
I'm the author of VidGear Video Processing python library that now also provides NetGear API, which is exclusively designed to transfer video frames synchronously between interconnecting systems over the network in real-time. You try it as follows:
A. Server End:(Bare-Minimum example)
Open your favorite terminal and execute the following python code:
Note: You can end streaming anytime on both server and client side by pressing [Ctrl+c] on your keyboard on server end!
# import libraries
from vidgear.gears import VideoGear
from vidgear.gears import NetGear
stream = VideoGear(source='test.mp4').start() #Open any video stream
server = NetGear() #Define netgear server with default settings
# infinite loop until [Ctrl+C] is pressed
while True:
try:
frame = stream.read()
# read frames
# check if frame is None
if frame is None:
#if True break the infinite loop
break
# do something with frame here
# send frame to server
server.send(frame)
except KeyboardInterrupt:
#break the infinite loop
break
# safely close video stream
stream.stop()
# safely close server
writer.close()
B. Client End:(Bare-Minimum example)
Then open another terminal on the same system and execute the following python code and see the output:
# import libraries
from vidgear.gears import NetGear
import cv2
#define netgear client with `receive_mode = True` and default settings
client = NetGear(receive_mode = True)
# infinite loop
while True:
# receive frames from network
frame = client.recv()
# check if frame is None
if frame is None:
#if True break the infinite loop
break
# do something with frame here
# Show output window
cv2.imshow("Output Frame", frame)
key = cv2.waitKey(1) & 0xFF
# check for 'q' key-press
if key == ord("q"):
#if 'q' key-pressed break out
break
# close output window
cv2.destroyAllWindows()
# safely close client
client.close()
NetGear as of now supports two ZeroMQ messaging patterns: i.e zmq.PAIR and zmq.REQ and zmq.REP and the supported protocol are: 'tcp' and 'ipc'
More advanced usage can be found here: https://abhitronix.github.io/vidgear/latest/gears/netgear/overview/
It is because you are receiving small amount of data, and image is not complete. 8192 bytes is not enough in 99.99% of the time, because every image is larger than 8Kb. You'll need to grab ALL data sent by sender in order to convert it to image.
You can take a look at my code on github and change it acording to your need.
Long story short, easy option is to first send number of bytes to the client, and then send an image itself. In client code, after receiving length of image, loop until all bytes are received.
for example:
...
img_len = 175428 # received by sender.py
e=0
data = ''
while e < img_len:
d = sock.recv(1024)
e += len(d)
data += d
nparr = np.fromstring(data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow('frame', frame)
Late answer, but for those looking for live video transmission and reception over socket:
Here is the snapshot of results:
server.py
import socket, cv2, pickle,struct
# 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()
a = pickle.dumps(frame)
message = struct.pack("Q",len(a))+a
client_socket.sendall(message)
cv2.imshow('TRANSMITTING VIDEO',frame)
key = cv2.waitKey(1) & 0xFF
if key ==ord('q'):
client_socket.close()
client.py
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)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
client_socket.close()
I'm trying to send video captured by webcam from a client to a server which should display the video.
Unfortunately, my server get the data (I think) but doesn't display it correctly and I don't unerstand why. I only get a small and grey window.
The client:
import numpy as np
import cv2, time
import speech_recognition as sr
from threading import Thread
import socket
import pickle
host = "localhost"
port = 8888
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect((host, port))
print("Connextion established on port {}".format(port))
cap = cv2.VideoCapture(0)
# while(True):
# Capture frame-by-frame
while 1:
ret, frame = cap.read()
ret = str(ret).encode()
connection.send(ret)
frame = pickle.dumps(frame)
connection.send(frame)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
And the server
#cd C:\Users\Clement\Desktop\pychat\example
import numpy as np
import cv2
import socket
host = ''
port = 8888
connexion_principale = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_principale.bind((host, port))
connexion_principale.listen(5)
print("The server is listening on port {}".format(port))
connexion_avec_client, infos_connexion = connexion_principale.accept()
cap = cv2.VideoCapture("localhost") #Maybe there is a problem here
msg_recu = b""
error = 0
while 1:
msg_recu1 = connexion_avec_client.recv(1024)
msg_recu2 = connexion_avec_client.recv(1024)
try:
if msg_recu1 != b"":
# Capture frame-by-frame
ret, frame = bool(msg_recu1.decode()), np.frombuffer(msg_recu2)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
error +=1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
print("Connection is now closed")
connexion_avec_client.close()
connexion_principale.close()