Image send via TCP

Viewed 27878

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()
1 Answers
Related