sending opencv frames through udp socket python

asked 2015-08-21 10:33:18 -0600

prp gravatar image

I worte these code for a video chat app I am trying to make. When i receive the frames...they repeat themselves with a a certain amount of lag.

The problem can be be best understood if u run the codes and see.

Here is the code which send the video :

import numpy as np
import cv2
import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()

    cv2.imshow('frame',frame)


    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

    d = frame.flatten ()
    s = d.tostring ()



    for i in xrange(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()

Here is the code which receives the code :

import socket
import numpy
import time
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 = numpy.fromstring (s,dtype=numpy.uint8)
        frame = frame.reshape (480,640,3)

        cv2.imshow('frame',frame)

        s=""

    if cv2.waitKey(1) & 0xFF == ord ('q'):
        break
edit retag flag offensive close merge delete

Comments

i am not able to run this code on python 3.6...i keep getting the error "TypeError: must be str, not bytes" for the line "s += data" in the receiver. Am i missing something?

ritviker gravatar imageritviker ( 2018-11-19 05:01:46 -0600 )edit

okay i was able to solve it myself, it gets resolved when s is initialized as s=b'' instead of s="" but the frames received at the receiver aren't perfect, i presume this is due to lack of sequencing in UDP...?

ritviker gravatar imageritviker ( 2018-11-19 06:58:03 -0600 )edit

hi i have tried your code.. but I am getting the video streamed over server. but the frames are split in server side.. do you have any solutions to that issue

nanthini gravatar imagenanthini ( 2019-01-18 05:40:57 -0600 )edit

at server end the frame is split or what to say its overlapped

nanthini gravatar imagenanthini ( 2019-01-18 05:41:34 -0600 )edit