(Python2.7) OpenCV send frames through socket

0

I'm trying to send OpenCV frames through sockets, with the code below. The problem is that the video ends up splitting into several rows or columns.

I can only run with python2 (2.7)

obs: This code is not mine.

source: link

Client:

import socket
import numpy as np
import cv2


UDP_IP = "127.0.0.1"
UDP_PORT = 5005

cap = cv2.VideoCapture(0)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while 1:
    ret, frame = cap.read()
    cv2.imshow('frame', frame)

    d = frame.flatten()
    s = d.tostring()
    for i in range(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()

Server:

import socket
import numpy as np
import cv2


UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
s = ""
while True:
    data, addr = sock.recvfrom(46080)
    s += data
    if len(s) == (46080*20):
        frame = np.fromstring(s, dtype='uint8')
        frame = frame.reshape(480, 640, 3)
        cv2.imshow('frame', frame)
        s = ""
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
asked by anonymous 10.03.2018 / 02:18

0 answers