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()