【发布时间】:2014-01-16 05:17:58
【问题描述】:
我正在尝试使用 python (raspberry PI) 通过 TCP Socket 发送捕获图像的数据。 TCP Server (QT) 在其他机器上运行并尝试读取图像并在 QLabel 中显示。
我从来没有在 python 中编程过,完全不明白我做错了什么。我花了很多时间(几天)来解决持续出现的错误,终于到了可以实际运行它的地步。但是我收到的数据是垃圾。
以下程序在 raspberry PI 的 sep 窗口中显示视频,并应通过套接字发送单个捕获的图像。
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()
【问题讨论】:
-
好吧,如果您收到“一块垃圾”,那只是编码和解码问题。尝试调试看看。
标签: python opencv tcp raspberry-pi