Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image send via TCP

I am trying to send data of the captured image via TCP Socket using python (raspberry PI ). TCP Server (QT) runs on other machine and tries to read the image and display in QLabel.

I have never programmed in python and totally don't understand what I am doing wrong. I have spend lots of time ( days ) to cover ongoing errors and finally got to the point that I can actually run it. But data what I am receiving is piece of junk.

Below program displays video in sep window on raspberry PI and should send the individual captured image via socket.

import cv2.cv as cv
import cv2
import time
from socket import socket
import sys
import numpy

cv.NamedWindow("camera",cv.CV_WINDOW_AUTOSIZE)

capture = cv.CaptureFromCAM(0)

sock = socket()
sock.connect(('192.168.0.2', 5001))
sock.send('Pi - Hallo')

while True:
    frame = cv.QueryFrame(capture)
    cv.ShowImage("camera", frame)

    mat = cv.GetMat(frame)
    buf = [1,90]

    image = cv.CreateImage (cv.GetSize (frame), 8, 3)
    nuImage = numpy.asarray(frame[:,:])
    imgencode = cv2.imencode('.png', nuImage, buf)
    data = numpy.array(imgencode)
    stringData = data.tostring()
    sock.send('Pi - Sending image data');
    sock.send( stringData );

    if cv.WaitKey(10) == 27:
        break

sock.send('Pi - closing connection')
sock.close()
like image 403
misiek303 Avatar asked Dec 28 '13 23:12

misiek303


1 Answers

The data you received is not junk. It is the send data. But it is send in bytes. So, at the receiving end, you need to convert the bytes in to string before processing the data. Read this for better understanding of sending and receiving data in socket programming and read this for methods used in current version of opencv.

Also your dont need to use cv to do this task. Current version of opencv provides methods to do so just by import cv2.

The following is a working code though it's communication is simplified.

client.py

#!/usr/bin/python
import socket
import cv2
import numpy

TCP_IP = 'localhost'
TCP_PORT = 5001

sock = socket.socket()
sock.connect((TCP_IP, TCP_PORT))

capture = cv2.VideoCapture(0)
ret, frame = capture.read()

encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),90]
result, imgencode = cv2.imencode('.jpg', frame, encode_param)
data = numpy.array(imgencode)
stringData = data.tostring()

sock.send( str(len(stringData)).ljust(16));
sock.send( stringData );
sock.close()

decimg=cv2.imdecode(data,1)
cv2.imshow('CLIENT',decimg)
cv2.waitKey(0)
cv2.destroyAllWindows() 

server.py

#!/usr/bin/python
import socket
import cv2
import numpy

def recvall(sock, count):
    buf = b''
    while count:
        newbuf = sock.recv(count)
        if not newbuf: return None
        buf += newbuf
        count -= len(newbuf)
    return buf

TCP_IP = 'localhost'
TCP_PORT = 5001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(True)
conn, addr = s.accept()

length = recvall(conn,16)
stringData = recvall(conn, int(length))
data = numpy.fromstring(stringData, dtype='uint8')
s.close()

decimg=cv2.imdecode(data,1)
cv2.imshow('SERVER',decimg)
cv2.waitKey(0)
cv2.destroyAllWindows() 
like image 198
NikzJon Avatar answered Sep 30 '22 00:09

NikzJon