Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

sending opencv frames through udp socket python

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