如何通过TCP套接字从raspberry pi(客户端)向服务器(笔记本电脑)发送图像?

时间:2018-04-01 15:50:23

标签: python-3.x sockets opencv3.1

我正在使用树莓派3型号B和Rpi相机B.( OS是raspbian stretch ) 我想通过TCP将Rasberry pi拍摄的图像发送到我的笔记本电脑( ubuntu 16.04 ).Raspberry pi和笔记本电脑通过路由器与wifi连接。但是当我收到数据时,处理它并保存它。然后结果不正确。

这是我的client.py

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import socket

TCP_IP = '192.168.0.109'
TCP_PORT = 5000
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (1280,720)
rawCapture = PiRGBArray(camera)

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((TCP_IP,TCP_PORT))
print('Connected to server')
# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
encoded = cv2.imencode('.jpg',image)[1]


s.sendall(encoded)
print('Done')
s.close()

这是我的server.py

import socket
import struct
import cv2
import numpy as np
TCP_IP = '0.0.0.0'
TCP_PORT = 5000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((TCP_IP,TCP_PORT))
s.listen(1)

con = s.accept()[0]
print('Connected to client....')
count = 0

name = str(count)+'.jpg'
data = con.recv(4096)

encoded_array = np.fromstring(data,np.uint8)
decoded_image = cv2.imdecode(encoded_array,cv2.IMREAD_COLOR)
cv2.imwrite(name,decoded_image)
s.close()
con.close()

Here is my result image

注意: Raspberry pi版本 - > opencv 3.4.0,python 3.5.3
笔记本电脑版本 - > opencv 3.1.0,python 3.5.2

0 个答案:

没有答案