Send video over TCP using OpenCV and sockets in Raspberry Pi

I am trying to send a live video frame from my client (Raspberry Pi) to a server hosted on a laptop. Both of these devices are connected to the same network.

Server.py

import socket import sys import cv2 import pickle import numpy as np import struct HOST = '192.168.1.3' PORT = 8083 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() data = "" payload_size = struct.calcsize("L") while True: 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) print frame.size # cv2.imshow('frame', frame) # cv2.waitKey(10) 

Client.py

 import cv2 import numpy as np import socket import sys import pickle import struct cap = cv2.VideoCapture(0) clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clientsocket.connect(('192.168.1.3', 8081)) while True: ret,frame = cap.read() data = pickle.dumps(frame) clientsocket.sendall(struct.pack("L", len(data)) + data) 

My server on the laptop is not receiving any data . But when I run this client and the server on the same devices (for example, the server and the client on the laptop), it works fine.

I can send data from raspberries to a laptop (tested on the echo application).

Can anyone help me with this?

+5
source share
2 answers

In my opinion, you should use the same ports for client and server, but you are using two different options: 8083 and 8081.

Have you verified that the firewall is open for the port you are using?

+1
source

After several hours of analyzing why the .py server and client.py did not work (even after the firewall was turned off), an error occurred in the server.py file: "The word should not be a string." Therefore, the word = "" (which is a string) should be replaced with word = b '' (these are two single quotes for representing a byte).

+1
source

Source: https://habr.com/ru/post/1246715/


All Articles