0

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()
  • Why do you have an instance of `cv2.VideoCapture` in the server script? Why do you transmit the success status flag returned by `cap.read()` along with a possibly empty frame, instead of handling that case properly on the client? You pickle the image on client, but never unpickle on server. Unless the image is a tiny icon, it will be much bigger than the 2x1024 bytes you attempt to receive. Your protocol lacks framing... – Dan Mašek Dec 26 '17 at 14:43
  • it's the first time i use network ,maybe it's clums. – Clément Picard Dec 26 '17 at 15:24
  • 2
    To be honest, there are a lot of errors in your code. And you don't follow suggestion in your another question. https://stackoverflow.com/questions/47977474/convert-str-to-numpy-ndarray/47977757#47977757 And this is my result https://i.stack.imgur.com/gfn9d.png Of course, I won't answer your question any more. – Kinght 金 Dec 26 '17 at 15:28
  • @ClémentPicard Well, in that case, simplify it a bit (instead of video, send a single image), and start from the beginning by defining a protocol. How does the client know how many bytes to receive to get all the data for one image? How does it interpret the linear array of bytes as an image? (How does it know what the width and height should be? How does it know the bit depth, the number of channels, and what those channels represent?) | Do more research on how `socket` works. I'm pretty sure that `recv` doesn't do exactly what you think it does. – Dan Mašek Dec 26 '17 at 16:51

0 Answers0