0

I've got client and server programs set up to work perfectly fine over localhost, that become nearly completely useless when piped through an ip. The server sends the video data and it is received flawlessly by the client through localhost, but once it's put into a real-life test it can only get about 1 useful frame in for every other second. The rest are heavily stretched upwards. Example of it at it's worst >> https://i.stack.imgur.com/WxoXg.jpg

Before the try/except was put into the client file it would play one frame of colorful vertical bars and then crash while complaining that the image given had no height or width.

cameraClient.py

import socket
import cv2
import numpy as np

ip = "insert ip"
port = 4001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(3)

sock.connect((ip, port))

while True:
    try:
        buff = sock.recv(921664)

        nparr = np.frombuffer(buff, np.uint8)
        newFrame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        cv2.imshow("s", newFrame)

    except:
        pass

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cameraServer.py

import cv2
import socket

host = ''
port = 4001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("Preparing webcam.")
vid = cv2.VideoCapture(0)
print("Webcam ready, waiting for client connection.")

sock.bind((host, port))
sock.listen(3)
conn, addr = sock.accept()

while True:
    if vid.isOpened():
        empty, frame = vid.read()
        data = cv2.imencode('.jpg', frame)[1].tostring()

        conn.send(data)

Surely this isn't an unsolvable issue. Even if I've got to completely revamp the way I send and receive data that's perfectly acceptable. If you can help me understand why it's doing this as well as how to fix it that would be great.

Pip
  • 21
  • 6
  • 2
    You seem to expect that all data is received at once which is not how networks work. – Klaus D. May 07 '19 at 03:48
  • I don't. The buffer's size is exactly that of a frame taken by my webcam, and the way I understand it, it won't store the data received through the sockets until it reaches that threshold which should be one frame. If it's not that, then where did I assume all data is received at once? – Pip May 07 '19 at 03:52
  • You also seem to assume that `send` will actually send all data which is not true either. It might work on a fast link with large MTU (i.e. localhost) but it will fail on LAN. – Steffen Ullrich May 07 '19 at 03:52
  • 4
    @Pip: `recv` will not wait until it has read all `921664` bytes. The number says **at most** this number of bytes and not exactly. Please have a look at [the documentation](https://docs.python.org/3/library/socket.html#socket.socket.recv): *The __maximum amount of data to be received__ at once is specified by bufsize.*. In other words: with both `send` and `recv` you cannot assume that the full data are send and received but you have to actually check the return values. – Steffen Ullrich May 07 '19 at 03:53
  • Ahh, then I just misunderstood how the function worked. In that case, would I just append data from the socket until it reaches that threshold and then call that a frame? – Pip May 07 '19 at 03:55
  • 1
    @Pip: yes, you have to call `recv` until you've got all data and concatenate these data into a single buffer for further processing. Similar you have to call `send` until everything is send or just use `sendall`. – Steffen Ullrich May 07 '19 at 03:57
  • @SteffenUllrich https://ghostbin.com/paste/wgcgs is the updated code, but I'm still getting the same issue, if not a little bit improved. What am I doing wrong? – Pip May 07 '19 at 04:10
  • @Pip: the code makes no sense: you read data into frameBuffer but never use frameBuffer and never clear it after the frame was received. I recommend that you extensively document your own code with what you are trying to do in order to better understand what you are doing and to detect such logic errors early yourself. – Steffen Ullrich May 07 '19 at 04:35
  • Possible duplicate of [Python Socket Receive Large Amount of Data](https://stackoverflow.com/questions/17667903/python-socket-receive-large-amount-of-data) – ivan_pozdeev May 07 '19 at 05:36

0 Answers0